167207b96SArnd Bergmann /* 267207b96SArnd Bergmann * SPU file system -- file contents 367207b96SArnd Bergmann * 467207b96SArnd Bergmann * (C) Copyright IBM Deutschland Entwicklung GmbH 2005 567207b96SArnd Bergmann * 667207b96SArnd Bergmann * Author: Arnd Bergmann <arndb@de.ibm.com> 767207b96SArnd Bergmann * 867207b96SArnd Bergmann * This program is free software; you can redistribute it and/or modify 967207b96SArnd Bergmann * it under the terms of the GNU General Public License as published by 1067207b96SArnd Bergmann * the Free Software Foundation; either version 2, or (at your option) 1167207b96SArnd Bergmann * any later version. 1267207b96SArnd Bergmann * 1367207b96SArnd Bergmann * This program is distributed in the hope that it will be useful, 1467207b96SArnd Bergmann * but WITHOUT ANY WARRANTY; without even the implied warranty of 1567207b96SArnd Bergmann * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1667207b96SArnd Bergmann * GNU General Public License for more details. 1767207b96SArnd Bergmann * 1867207b96SArnd Bergmann * You should have received a copy of the GNU General Public License 1967207b96SArnd Bergmann * along with this program; if not, write to the Free Software 2067207b96SArnd Bergmann * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 2167207b96SArnd Bergmann */ 2267207b96SArnd Bergmann 23a33a7d73SArnd Bergmann #undef DEBUG 24a33a7d73SArnd Bergmann 2567207b96SArnd Bergmann #include <linux/fs.h> 2667207b96SArnd Bergmann #include <linux/ioctl.h> 2767207b96SArnd Bergmann #include <linux/module.h> 28d88cfffaSArnd Bergmann #include <linux/pagemap.h> 2967207b96SArnd Bergmann #include <linux/poll.h> 305110459fSArnd Bergmann #include <linux/ptrace.h> 31cbe709c1SBenjamin Herrenschmidt #include <linux/seq_file.h> 32038200cfSChristoph Hellwig #include <linux/marker.h> 3367207b96SArnd Bergmann 3467207b96SArnd Bergmann #include <asm/io.h> 35dfe1e09fSFUJITA Tomonori #include <asm/time.h> 3667207b96SArnd Bergmann #include <asm/spu.h> 37b9e3bd77SDwayne Grant McConnell #include <asm/spu_info.h> 3867207b96SArnd Bergmann #include <asm/uaccess.h> 3967207b96SArnd Bergmann 4067207b96SArnd Bergmann #include "spufs.h" 4167207b96SArnd Bergmann 4227d5bf2aSBenjamin Herrenschmidt #define SPUFS_MMAP_4K (PAGE_SIZE == 0x1000) 4327d5bf2aSBenjamin Herrenschmidt 44197b1a82SChristoph Hellwig /* Simple attribute files */ 45197b1a82SChristoph Hellwig struct spufs_attr { 46197b1a82SChristoph Hellwig int (*get)(void *, u64 *); 47197b1a82SChristoph Hellwig int (*set)(void *, u64); 48197b1a82SChristoph Hellwig char get_buf[24]; /* enough to store a u64 and "\n\0" */ 49197b1a82SChristoph Hellwig char set_buf[24]; 50197b1a82SChristoph Hellwig void *data; 51197b1a82SChristoph Hellwig const char *fmt; /* format for read operation */ 52197b1a82SChristoph Hellwig struct mutex mutex; /* protects access to these buffers */ 53197b1a82SChristoph Hellwig }; 54197b1a82SChristoph Hellwig 55197b1a82SChristoph Hellwig static int spufs_attr_open(struct inode *inode, struct file *file, 56197b1a82SChristoph Hellwig int (*get)(void *, u64 *), int (*set)(void *, u64), 57197b1a82SChristoph Hellwig const char *fmt) 58197b1a82SChristoph Hellwig { 59197b1a82SChristoph Hellwig struct spufs_attr *attr; 60197b1a82SChristoph Hellwig 61197b1a82SChristoph Hellwig attr = kmalloc(sizeof(*attr), GFP_KERNEL); 62197b1a82SChristoph Hellwig if (!attr) 63197b1a82SChristoph Hellwig return -ENOMEM; 64197b1a82SChristoph Hellwig 65197b1a82SChristoph Hellwig attr->get = get; 66197b1a82SChristoph Hellwig attr->set = set; 67197b1a82SChristoph Hellwig attr->data = inode->i_private; 68197b1a82SChristoph Hellwig attr->fmt = fmt; 69197b1a82SChristoph Hellwig mutex_init(&attr->mutex); 70197b1a82SChristoph Hellwig file->private_data = attr; 71197b1a82SChristoph Hellwig 72197b1a82SChristoph Hellwig return nonseekable_open(inode, file); 73197b1a82SChristoph Hellwig } 74197b1a82SChristoph Hellwig 75197b1a82SChristoph Hellwig static int spufs_attr_release(struct inode *inode, struct file *file) 76197b1a82SChristoph Hellwig { 77197b1a82SChristoph Hellwig kfree(file->private_data); 78197b1a82SChristoph Hellwig return 0; 79197b1a82SChristoph Hellwig } 80197b1a82SChristoph Hellwig 81197b1a82SChristoph Hellwig static ssize_t spufs_attr_read(struct file *file, char __user *buf, 82197b1a82SChristoph Hellwig size_t len, loff_t *ppos) 83197b1a82SChristoph Hellwig { 84197b1a82SChristoph Hellwig struct spufs_attr *attr; 85197b1a82SChristoph Hellwig size_t size; 86197b1a82SChristoph Hellwig ssize_t ret; 87197b1a82SChristoph Hellwig 88197b1a82SChristoph Hellwig attr = file->private_data; 89197b1a82SChristoph Hellwig if (!attr->get) 90197b1a82SChristoph Hellwig return -EACCES; 91197b1a82SChristoph Hellwig 92197b1a82SChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex); 93197b1a82SChristoph Hellwig if (ret) 94197b1a82SChristoph Hellwig return ret; 95197b1a82SChristoph Hellwig 96197b1a82SChristoph Hellwig if (*ppos) { /* continued read */ 97197b1a82SChristoph Hellwig size = strlen(attr->get_buf); 98197b1a82SChristoph Hellwig } else { /* first read */ 99197b1a82SChristoph Hellwig u64 val; 100197b1a82SChristoph Hellwig ret = attr->get(attr->data, &val); 101197b1a82SChristoph Hellwig if (ret) 102197b1a82SChristoph Hellwig goto out; 103197b1a82SChristoph Hellwig 104197b1a82SChristoph Hellwig size = scnprintf(attr->get_buf, sizeof(attr->get_buf), 105197b1a82SChristoph Hellwig attr->fmt, (unsigned long long)val); 106197b1a82SChristoph Hellwig } 107197b1a82SChristoph Hellwig 108197b1a82SChristoph Hellwig ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size); 109197b1a82SChristoph Hellwig out: 110197b1a82SChristoph Hellwig mutex_unlock(&attr->mutex); 111197b1a82SChristoph Hellwig return ret; 112197b1a82SChristoph Hellwig } 113197b1a82SChristoph Hellwig 114197b1a82SChristoph Hellwig static ssize_t spufs_attr_write(struct file *file, const char __user *buf, 115197b1a82SChristoph Hellwig size_t len, loff_t *ppos) 116197b1a82SChristoph Hellwig { 117197b1a82SChristoph Hellwig struct spufs_attr *attr; 118197b1a82SChristoph Hellwig u64 val; 119197b1a82SChristoph Hellwig size_t size; 120197b1a82SChristoph Hellwig ssize_t ret; 121197b1a82SChristoph Hellwig 122197b1a82SChristoph Hellwig attr = file->private_data; 123197b1a82SChristoph Hellwig if (!attr->set) 124197b1a82SChristoph Hellwig return -EACCES; 125197b1a82SChristoph Hellwig 126197b1a82SChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex); 127197b1a82SChristoph Hellwig if (ret) 128197b1a82SChristoph Hellwig return ret; 129197b1a82SChristoph Hellwig 130197b1a82SChristoph Hellwig ret = -EFAULT; 131197b1a82SChristoph Hellwig size = min(sizeof(attr->set_buf) - 1, len); 132197b1a82SChristoph Hellwig if (copy_from_user(attr->set_buf, buf, size)) 133197b1a82SChristoph Hellwig goto out; 134197b1a82SChristoph Hellwig 135197b1a82SChristoph Hellwig ret = len; /* claim we got the whole input */ 136197b1a82SChristoph Hellwig attr->set_buf[size] = '\0'; 137197b1a82SChristoph Hellwig val = simple_strtol(attr->set_buf, NULL, 0); 138197b1a82SChristoph Hellwig attr->set(attr->data, val); 139197b1a82SChristoph Hellwig out: 140197b1a82SChristoph Hellwig mutex_unlock(&attr->mutex); 141197b1a82SChristoph Hellwig return ret; 142197b1a82SChristoph Hellwig } 143197b1a82SChristoph Hellwig 144197b1a82SChristoph Hellwig #define DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \ 145197b1a82SChristoph Hellwig static int __fops ## _open(struct inode *inode, struct file *file) \ 146197b1a82SChristoph Hellwig { \ 147197b1a82SChristoph Hellwig __simple_attr_check_format(__fmt, 0ull); \ 148197b1a82SChristoph Hellwig return spufs_attr_open(inode, file, __get, __set, __fmt); \ 149197b1a82SChristoph Hellwig } \ 150197b1a82SChristoph Hellwig static struct file_operations __fops = { \ 151197b1a82SChristoph Hellwig .owner = THIS_MODULE, \ 152197b1a82SChristoph Hellwig .open = __fops ## _open, \ 153197b1a82SChristoph Hellwig .release = spufs_attr_release, \ 154197b1a82SChristoph Hellwig .read = spufs_attr_read, \ 155197b1a82SChristoph Hellwig .write = spufs_attr_write, \ 156197b1a82SChristoph Hellwig }; 157197b1a82SChristoph Hellwig 158cbe709c1SBenjamin Herrenschmidt 15967207b96SArnd Bergmann static int 16067207b96SArnd Bergmann spufs_mem_open(struct inode *inode, struct file *file) 16167207b96SArnd Bergmann { 16267207b96SArnd Bergmann struct spufs_inode_info *i = SPUFS_I(inode); 1636df10a82SMark Nutter struct spu_context *ctx = i->i_ctx; 16443c2bbd9SChristoph Hellwig 16547d3a5faSChristoph Hellwig mutex_lock(&ctx->mapping_lock); 1666df10a82SMark Nutter file->private_data = ctx; 16743c2bbd9SChristoph Hellwig if (!i->i_openers++) 1686df10a82SMark Nutter ctx->local_store = inode->i_mapping; 16947d3a5faSChristoph Hellwig mutex_unlock(&ctx->mapping_lock); 17043c2bbd9SChristoph Hellwig return 0; 17143c2bbd9SChristoph Hellwig } 17243c2bbd9SChristoph Hellwig 17343c2bbd9SChristoph Hellwig static int 17443c2bbd9SChristoph Hellwig spufs_mem_release(struct inode *inode, struct file *file) 17543c2bbd9SChristoph Hellwig { 17643c2bbd9SChristoph Hellwig struct spufs_inode_info *i = SPUFS_I(inode); 17743c2bbd9SChristoph Hellwig struct spu_context *ctx = i->i_ctx; 17843c2bbd9SChristoph Hellwig 17947d3a5faSChristoph Hellwig mutex_lock(&ctx->mapping_lock); 18043c2bbd9SChristoph Hellwig if (!--i->i_openers) 18143c2bbd9SChristoph Hellwig ctx->local_store = NULL; 18247d3a5faSChristoph Hellwig mutex_unlock(&ctx->mapping_lock); 18367207b96SArnd Bergmann return 0; 18467207b96SArnd Bergmann } 18567207b96SArnd Bergmann 18667207b96SArnd Bergmann static ssize_t 187bf1ab978SDwayne Grant McConnell __spufs_mem_read(struct spu_context *ctx, char __user *buffer, 188bf1ab978SDwayne Grant McConnell size_t size, loff_t *pos) 189bf1ab978SDwayne Grant McConnell { 190bf1ab978SDwayne Grant McConnell char *local_store = ctx->ops->get_ls(ctx); 191bf1ab978SDwayne Grant McConnell return simple_read_from_buffer(buffer, size, pos, local_store, 192bf1ab978SDwayne Grant McConnell LS_SIZE); 193bf1ab978SDwayne Grant McConnell } 194bf1ab978SDwayne Grant McConnell 195bf1ab978SDwayne Grant McConnell static ssize_t 19667207b96SArnd Bergmann spufs_mem_read(struct file *file, char __user *buffer, 19767207b96SArnd Bergmann size_t size, loff_t *pos) 19867207b96SArnd Bergmann { 199bf1ab978SDwayne Grant McConnell struct spu_context *ctx = file->private_data; 200aa0ed2bdSArnd Bergmann ssize_t ret; 20167207b96SArnd Bergmann 202c9101bdbSChristoph Hellwig ret = spu_acquire(ctx); 203c9101bdbSChristoph Hellwig if (ret) 204c9101bdbSChristoph Hellwig return ret; 205bf1ab978SDwayne Grant McConnell ret = __spufs_mem_read(ctx, buffer, size, pos); 2068b3d6663SArnd Bergmann spu_release(ctx); 207c9101bdbSChristoph Hellwig 20867207b96SArnd Bergmann return ret; 20967207b96SArnd Bergmann } 21067207b96SArnd Bergmann 21167207b96SArnd Bergmann static ssize_t 21267207b96SArnd Bergmann spufs_mem_write(struct file *file, const char __user *buffer, 213aa0ed2bdSArnd Bergmann size_t size, loff_t *ppos) 21467207b96SArnd Bergmann { 21567207b96SArnd Bergmann struct spu_context *ctx = file->private_data; 2168b3d6663SArnd Bergmann char *local_store; 217aa0ed2bdSArnd Bergmann loff_t pos = *ppos; 2188b3d6663SArnd Bergmann int ret; 21967207b96SArnd Bergmann 220aa0ed2bdSArnd Bergmann if (pos < 0) 221aa0ed2bdSArnd Bergmann return -EINVAL; 222aa0ed2bdSArnd Bergmann if (pos > LS_SIZE) 22367207b96SArnd Bergmann return -EFBIG; 224aa0ed2bdSArnd Bergmann if (size > LS_SIZE - pos) 225aa0ed2bdSArnd Bergmann size = LS_SIZE - pos; 2268b3d6663SArnd Bergmann 227c9101bdbSChristoph Hellwig ret = spu_acquire(ctx); 228c9101bdbSChristoph Hellwig if (ret) 229c9101bdbSChristoph Hellwig return ret; 230c9101bdbSChristoph Hellwig 2318b3d6663SArnd Bergmann local_store = ctx->ops->get_ls(ctx); 232aa0ed2bdSArnd Bergmann ret = copy_from_user(local_store + pos, buffer, size); 2338b3d6663SArnd Bergmann spu_release(ctx); 234aa0ed2bdSArnd Bergmann 235aa0ed2bdSArnd Bergmann if (ret) 236aa0ed2bdSArnd Bergmann return -EFAULT; 237aa0ed2bdSArnd Bergmann *ppos = pos + size; 238aa0ed2bdSArnd Bergmann return size; 23967207b96SArnd Bergmann } 24067207b96SArnd Bergmann 241b1e2270fSNick Piggin static int 242b1e2270fSNick Piggin spufs_mem_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 2438b3d6663SArnd Bergmann { 2448b3d6663SArnd Bergmann struct spu_context *ctx = vma->vm_file->private_data; 245b1e2270fSNick Piggin unsigned long address = (unsigned long)vmf->virtual_address; 246b1e2270fSNick Piggin unsigned long pfn, offset; 247b1e2270fSNick Piggin 248f1fa74f4SBenjamin Herrenschmidt #ifdef CONFIG_SPU_FS_64K_LS 249f1fa74f4SBenjamin Herrenschmidt struct spu_state *csa = &ctx->csa; 250f1fa74f4SBenjamin Herrenschmidt int psize; 25178bde53eSBenjamin Herrenschmidt 252f1fa74f4SBenjamin Herrenschmidt /* Check what page size we are using */ 253f1fa74f4SBenjamin Herrenschmidt psize = get_slice_psize(vma->vm_mm, address); 2548b3d6663SArnd Bergmann 255f1fa74f4SBenjamin Herrenschmidt /* Some sanity checking */ 256f1fa74f4SBenjamin Herrenschmidt BUG_ON(csa->use_big_pages != (psize == MMU_PAGE_64K)); 257f1fa74f4SBenjamin Herrenschmidt 258f1fa74f4SBenjamin Herrenschmidt /* Wow, 64K, cool, we need to align the address though */ 259f1fa74f4SBenjamin Herrenschmidt if (csa->use_big_pages) { 260f1fa74f4SBenjamin Herrenschmidt BUG_ON(vma->vm_start & 0xffff); 261f1fa74f4SBenjamin Herrenschmidt address &= ~0xfffful; 262f1fa74f4SBenjamin Herrenschmidt } 263f1fa74f4SBenjamin Herrenschmidt #endif /* CONFIG_SPU_FS_64K_LS */ 264f1fa74f4SBenjamin Herrenschmidt 265b1e2270fSNick Piggin offset = vmf->pgoff << PAGE_SHIFT; 266128b8546SMasato Noguchi if (offset >= LS_SIZE) 267b1e2270fSNick Piggin return VM_FAULT_SIGBUS; 268128b8546SMasato Noguchi 269b1e2270fSNick Piggin pr_debug("spufs_mem_mmap_fault address=0x%lx, offset=0x%lx\n", 270b1e2270fSNick Piggin address, offset); 271f1fa74f4SBenjamin Herrenschmidt 272c9101bdbSChristoph Hellwig if (spu_acquire(ctx)) 273b1e2270fSNick Piggin return VM_FAULT_NOPAGE; 2748b3d6663SArnd Bergmann 275ac91cb8dSArnd Bergmann if (ctx->state == SPU_STATE_SAVED) { 276ac91cb8dSArnd Bergmann vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 277932f535dSArnd Bergmann & ~_PAGE_NO_CACHE); 27878bde53eSBenjamin Herrenschmidt pfn = vmalloc_to_pfn(ctx->csa.lscsa->ls + offset); 279ac91cb8dSArnd Bergmann } else { 280ac91cb8dSArnd Bergmann vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 281932f535dSArnd Bergmann | _PAGE_NO_CACHE); 28278bde53eSBenjamin Herrenschmidt pfn = (ctx->spu->local_store_phys + offset) >> PAGE_SHIFT; 283ac91cb8dSArnd Bergmann } 28478bde53eSBenjamin Herrenschmidt vm_insert_pfn(vma, address, pfn); 28578bde53eSBenjamin Herrenschmidt 2868b3d6663SArnd Bergmann spu_release(ctx); 2878b3d6663SArnd Bergmann 288b1e2270fSNick Piggin return VM_FAULT_NOPAGE; 2898b3d6663SArnd Bergmann } 2908b3d6663SArnd Bergmann 291a352894dSBenjamin Herrenschmidt static int spufs_mem_mmap_access(struct vm_area_struct *vma, 292a352894dSBenjamin Herrenschmidt unsigned long address, 293a352894dSBenjamin Herrenschmidt void *buf, int len, int write) 294a352894dSBenjamin Herrenschmidt { 295a352894dSBenjamin Herrenschmidt struct spu_context *ctx = vma->vm_file->private_data; 296a352894dSBenjamin Herrenschmidt unsigned long offset = address - vma->vm_start; 297a352894dSBenjamin Herrenschmidt char *local_store; 298a352894dSBenjamin Herrenschmidt 299a352894dSBenjamin Herrenschmidt if (write && !(vma->vm_flags & VM_WRITE)) 300a352894dSBenjamin Herrenschmidt return -EACCES; 301a352894dSBenjamin Herrenschmidt if (spu_acquire(ctx)) 302a352894dSBenjamin Herrenschmidt return -EINTR; 303a352894dSBenjamin Herrenschmidt if ((offset + len) > vma->vm_end) 304a352894dSBenjamin Herrenschmidt len = vma->vm_end - offset; 305a352894dSBenjamin Herrenschmidt local_store = ctx->ops->get_ls(ctx); 306a352894dSBenjamin Herrenschmidt if (write) 307a352894dSBenjamin Herrenschmidt memcpy_toio(local_store + offset, buf, len); 308a352894dSBenjamin Herrenschmidt else 309a352894dSBenjamin Herrenschmidt memcpy_fromio(buf, local_store + offset, len); 310a352894dSBenjamin Herrenschmidt spu_release(ctx); 311a352894dSBenjamin Herrenschmidt return len; 312a352894dSBenjamin Herrenschmidt } 31378bde53eSBenjamin Herrenschmidt 3148b3d6663SArnd Bergmann static struct vm_operations_struct spufs_mem_mmap_vmops = { 315b1e2270fSNick Piggin .fault = spufs_mem_mmap_fault, 316a352894dSBenjamin Herrenschmidt .access = spufs_mem_mmap_access, 3178b3d6663SArnd Bergmann }; 3188b3d6663SArnd Bergmann 319f1fa74f4SBenjamin Herrenschmidt static int spufs_mem_mmap(struct file *file, struct vm_area_struct *vma) 32067207b96SArnd Bergmann { 321f1fa74f4SBenjamin Herrenschmidt #ifdef CONFIG_SPU_FS_64K_LS 322f1fa74f4SBenjamin Herrenschmidt struct spu_context *ctx = file->private_data; 323f1fa74f4SBenjamin Herrenschmidt struct spu_state *csa = &ctx->csa; 324f1fa74f4SBenjamin Herrenschmidt 325f1fa74f4SBenjamin Herrenschmidt /* Sanity check VMA alignment */ 326f1fa74f4SBenjamin Herrenschmidt if (csa->use_big_pages) { 327f1fa74f4SBenjamin Herrenschmidt pr_debug("spufs_mem_mmap 64K, start=0x%lx, end=0x%lx," 328f1fa74f4SBenjamin Herrenschmidt " pgoff=0x%lx\n", vma->vm_start, vma->vm_end, 329f1fa74f4SBenjamin Herrenschmidt vma->vm_pgoff); 330f1fa74f4SBenjamin Herrenschmidt if (vma->vm_start & 0xffff) 331f1fa74f4SBenjamin Herrenschmidt return -EINVAL; 332f1fa74f4SBenjamin Herrenschmidt if (vma->vm_pgoff & 0xf) 333f1fa74f4SBenjamin Herrenschmidt return -EINVAL; 334f1fa74f4SBenjamin Herrenschmidt } 335f1fa74f4SBenjamin Herrenschmidt #endif /* CONFIG_SPU_FS_64K_LS */ 336f1fa74f4SBenjamin Herrenschmidt 3378b3d6663SArnd Bergmann if (!(vma->vm_flags & VM_SHARED)) 3388b3d6663SArnd Bergmann return -EINVAL; 33967207b96SArnd Bergmann 34078bde53eSBenjamin Herrenschmidt vma->vm_flags |= VM_IO | VM_PFNMAP; 34167207b96SArnd Bergmann vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 34267207b96SArnd Bergmann | _PAGE_NO_CACHE); 3438b3d6663SArnd Bergmann 3448b3d6663SArnd Bergmann vma->vm_ops = &spufs_mem_mmap_vmops; 34567207b96SArnd Bergmann return 0; 34667207b96SArnd Bergmann } 34767207b96SArnd Bergmann 348f1fa74f4SBenjamin Herrenschmidt #ifdef CONFIG_SPU_FS_64K_LS 3491238819aSSebastian Siewior static unsigned long spufs_get_unmapped_area(struct file *file, 3501238819aSSebastian Siewior unsigned long addr, unsigned long len, unsigned long pgoff, 351f1fa74f4SBenjamin Herrenschmidt unsigned long flags) 352f1fa74f4SBenjamin Herrenschmidt { 353f1fa74f4SBenjamin Herrenschmidt struct spu_context *ctx = file->private_data; 354f1fa74f4SBenjamin Herrenschmidt struct spu_state *csa = &ctx->csa; 355f1fa74f4SBenjamin Herrenschmidt 356f1fa74f4SBenjamin Herrenschmidt /* If not using big pages, fallback to normal MM g_u_a */ 357f1fa74f4SBenjamin Herrenschmidt if (!csa->use_big_pages) 358f1fa74f4SBenjamin Herrenschmidt return current->mm->get_unmapped_area(file, addr, len, 359f1fa74f4SBenjamin Herrenschmidt pgoff, flags); 360f1fa74f4SBenjamin Herrenschmidt 361f1fa74f4SBenjamin Herrenschmidt /* Else, try to obtain a 64K pages slice */ 362f1fa74f4SBenjamin Herrenschmidt return slice_get_unmapped_area(addr, len, flags, 363f1fa74f4SBenjamin Herrenschmidt MMU_PAGE_64K, 1, 0); 364f1fa74f4SBenjamin Herrenschmidt } 365f1fa74f4SBenjamin Herrenschmidt #endif /* CONFIG_SPU_FS_64K_LS */ 366f1fa74f4SBenjamin Herrenschmidt 3675dfe4c96SArjan van de Ven static const struct file_operations spufs_mem_fops = { 36867207b96SArnd Bergmann .open = spufs_mem_open, 369ce92987bSChristoph Hellwig .release = spufs_mem_release, 37067207b96SArnd Bergmann .read = spufs_mem_read, 37167207b96SArnd Bergmann .write = spufs_mem_write, 3728b3d6663SArnd Bergmann .llseek = generic_file_llseek, 37367207b96SArnd Bergmann .mmap = spufs_mem_mmap, 374f1fa74f4SBenjamin Herrenschmidt #ifdef CONFIG_SPU_FS_64K_LS 375f1fa74f4SBenjamin Herrenschmidt .get_unmapped_area = spufs_get_unmapped_area, 376f1fa74f4SBenjamin Herrenschmidt #endif 3778b3d6663SArnd Bergmann }; 3788b3d6663SArnd Bergmann 379b1e2270fSNick Piggin static int spufs_ps_fault(struct vm_area_struct *vma, 380b1e2270fSNick Piggin struct vm_fault *vmf, 38178bde53eSBenjamin Herrenschmidt unsigned long ps_offs, 38227d5bf2aSBenjamin Herrenschmidt unsigned long ps_size) 3836df10a82SMark Nutter { 3846df10a82SMark Nutter struct spu_context *ctx = vma->vm_file->private_data; 385b1e2270fSNick Piggin unsigned long area, offset = vmf->pgoff << PAGE_SHIFT; 386eebead5bSChristoph Hellwig int ret = 0; 3876df10a82SMark Nutter 388b1e2270fSNick Piggin spu_context_nospu_trace(spufs_ps_fault__enter, ctx); 389038200cfSChristoph Hellwig 39027d5bf2aSBenjamin Herrenschmidt if (offset >= ps_size) 391b1e2270fSNick Piggin return VM_FAULT_SIGBUS; 3926df10a82SMark Nutter 39360657263SJeremy Kerr if (fatal_signal_pending(current)) 39460657263SJeremy Kerr return VM_FAULT_SIGBUS; 39560657263SJeremy Kerr 39633bfd7a7SArnd Bergmann /* 397d5883137SJeremy Kerr * Because we release the mmap_sem, the context may be destroyed while 398d5883137SJeremy Kerr * we're in spu_wait. Grab an extra reference so it isn't destroyed 399d5883137SJeremy Kerr * in the meantime. 400d5883137SJeremy Kerr */ 401d5883137SJeremy Kerr get_spu_context(ctx); 402d5883137SJeremy Kerr 403d5883137SJeremy Kerr /* 40433bfd7a7SArnd Bergmann * We have to wait for context to be loaded before we have 40533bfd7a7SArnd Bergmann * pages to hand out to the user, but we don't want to wait 40633bfd7a7SArnd Bergmann * with the mmap_sem held. 40733bfd7a7SArnd Bergmann * It is possible to drop the mmap_sem here, but then we need 408b1e2270fSNick Piggin * to return VM_FAULT_NOPAGE because the mappings may have 40933bfd7a7SArnd Bergmann * hanged. 41078bde53eSBenjamin Herrenschmidt */ 411c9101bdbSChristoph Hellwig if (spu_acquire(ctx)) 412d5883137SJeremy Kerr goto refault; 413c9101bdbSChristoph Hellwig 41433bfd7a7SArnd Bergmann if (ctx->state == SPU_STATE_SAVED) { 41533bfd7a7SArnd Bergmann up_read(¤t->mm->mmap_sem); 416b1e2270fSNick Piggin spu_context_nospu_trace(spufs_ps_fault__sleep, ctx); 417eebead5bSChristoph Hellwig ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE); 418b1e2270fSNick Piggin spu_context_trace(spufs_ps_fault__wake, ctx, ctx->spu); 41933bfd7a7SArnd Bergmann down_read(¤t->mm->mmap_sem); 420c9101bdbSChristoph Hellwig } else { 4216df10a82SMark Nutter area = ctx->spu->problem_phys + ps_offs; 422b1e2270fSNick Piggin vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, 423b1e2270fSNick Piggin (area + offset) >> PAGE_SHIFT); 424b1e2270fSNick Piggin spu_context_trace(spufs_ps_fault__insert, ctx, ctx->spu); 425c9101bdbSChristoph Hellwig } 42633bfd7a7SArnd Bergmann 427eebead5bSChristoph Hellwig if (!ret) 4286df10a82SMark Nutter spu_release(ctx); 429d5883137SJeremy Kerr 430d5883137SJeremy Kerr refault: 431d5883137SJeremy Kerr put_spu_context(ctx); 432b1e2270fSNick Piggin return VM_FAULT_NOPAGE; 4336df10a82SMark Nutter } 4346df10a82SMark Nutter 43527d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K 436b1e2270fSNick Piggin static int spufs_cntl_mmap_fault(struct vm_area_struct *vma, 437b1e2270fSNick Piggin struct vm_fault *vmf) 4386df10a82SMark Nutter { 43987ff6090SJeremy Kerr return spufs_ps_fault(vma, vmf, 0x4000, SPUFS_CNTL_MAP_SIZE); 4406df10a82SMark Nutter } 4416df10a82SMark Nutter 4426df10a82SMark Nutter static struct vm_operations_struct spufs_cntl_mmap_vmops = { 443b1e2270fSNick Piggin .fault = spufs_cntl_mmap_fault, 4446df10a82SMark Nutter }; 4456df10a82SMark Nutter 4466df10a82SMark Nutter /* 4476df10a82SMark Nutter * mmap support for problem state control area [0x4000 - 0x4fff]. 4486df10a82SMark Nutter */ 4496df10a82SMark Nutter static int spufs_cntl_mmap(struct file *file, struct vm_area_struct *vma) 4506df10a82SMark Nutter { 4516df10a82SMark Nutter if (!(vma->vm_flags & VM_SHARED)) 4526df10a82SMark Nutter return -EINVAL; 4536df10a82SMark Nutter 45478bde53eSBenjamin Herrenschmidt vma->vm_flags |= VM_IO | VM_PFNMAP; 4556df10a82SMark Nutter vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 45623cc7701SBenjamin Herrenschmidt | _PAGE_NO_CACHE | _PAGE_GUARDED); 4576df10a82SMark Nutter 4586df10a82SMark Nutter vma->vm_ops = &spufs_cntl_mmap_vmops; 4596df10a82SMark Nutter return 0; 4606df10a82SMark Nutter } 46127d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */ 46227d5bf2aSBenjamin Herrenschmidt #define spufs_cntl_mmap NULL 46327d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */ 4646df10a82SMark Nutter 465197b1a82SChristoph Hellwig static int spufs_cntl_get(void *data, u64 *val) 466e1dbff2bSArnd Bergmann { 467e1dbff2bSArnd Bergmann struct spu_context *ctx = data; 468c9101bdbSChristoph Hellwig int ret; 469e1dbff2bSArnd Bergmann 470c9101bdbSChristoph Hellwig ret = spu_acquire(ctx); 471c9101bdbSChristoph Hellwig if (ret) 472c9101bdbSChristoph Hellwig return ret; 473197b1a82SChristoph Hellwig *val = ctx->ops->status_read(ctx); 474e1dbff2bSArnd Bergmann spu_release(ctx); 475e1dbff2bSArnd Bergmann 476197b1a82SChristoph Hellwig return 0; 477e1dbff2bSArnd Bergmann } 478e1dbff2bSArnd Bergmann 479197b1a82SChristoph Hellwig static int spufs_cntl_set(void *data, u64 val) 480e1dbff2bSArnd Bergmann { 481e1dbff2bSArnd Bergmann struct spu_context *ctx = data; 482c9101bdbSChristoph Hellwig int ret; 483e1dbff2bSArnd Bergmann 484c9101bdbSChristoph Hellwig ret = spu_acquire(ctx); 485c9101bdbSChristoph Hellwig if (ret) 486c9101bdbSChristoph Hellwig return ret; 487e1dbff2bSArnd Bergmann ctx->ops->runcntl_write(ctx, val); 488e1dbff2bSArnd Bergmann spu_release(ctx); 489197b1a82SChristoph Hellwig 490197b1a82SChristoph Hellwig return 0; 491e1dbff2bSArnd Bergmann } 492e1dbff2bSArnd Bergmann 4936df10a82SMark Nutter static int spufs_cntl_open(struct inode *inode, struct file *file) 4946df10a82SMark Nutter { 4956df10a82SMark Nutter struct spufs_inode_info *i = SPUFS_I(inode); 4966df10a82SMark Nutter struct spu_context *ctx = i->i_ctx; 4976df10a82SMark Nutter 49847d3a5faSChristoph Hellwig mutex_lock(&ctx->mapping_lock); 4996df10a82SMark Nutter file->private_data = ctx; 50043c2bbd9SChristoph Hellwig if (!i->i_openers++) 5016df10a82SMark Nutter ctx->cntl = inode->i_mapping; 50247d3a5faSChristoph Hellwig mutex_unlock(&ctx->mapping_lock); 5038b88b099SChristoph Hellwig return simple_attr_open(inode, file, spufs_cntl_get, 504e1dbff2bSArnd Bergmann spufs_cntl_set, "0x%08lx"); 5056df10a82SMark Nutter } 5066df10a82SMark Nutter 50743c2bbd9SChristoph Hellwig static int 50843c2bbd9SChristoph Hellwig spufs_cntl_release(struct inode *inode, struct file *file) 50943c2bbd9SChristoph Hellwig { 51043c2bbd9SChristoph Hellwig struct spufs_inode_info *i = SPUFS_I(inode); 51143c2bbd9SChristoph Hellwig struct spu_context *ctx = i->i_ctx; 51243c2bbd9SChristoph Hellwig 51374bedc4dSChristoph Hellwig simple_attr_release(inode, file); 51443c2bbd9SChristoph Hellwig 51547d3a5faSChristoph Hellwig mutex_lock(&ctx->mapping_lock); 51643c2bbd9SChristoph Hellwig if (!--i->i_openers) 51743c2bbd9SChristoph Hellwig ctx->cntl = NULL; 51847d3a5faSChristoph Hellwig mutex_unlock(&ctx->mapping_lock); 51943c2bbd9SChristoph Hellwig return 0; 52043c2bbd9SChristoph Hellwig } 52143c2bbd9SChristoph Hellwig 5225dfe4c96SArjan van de Ven static const struct file_operations spufs_cntl_fops = { 5236df10a82SMark Nutter .open = spufs_cntl_open, 52443c2bbd9SChristoph Hellwig .release = spufs_cntl_release, 5258b88b099SChristoph Hellwig .read = simple_attr_read, 5268b88b099SChristoph Hellwig .write = simple_attr_write, 5276df10a82SMark Nutter .mmap = spufs_cntl_mmap, 5286df10a82SMark Nutter }; 5296df10a82SMark Nutter 5308b3d6663SArnd Bergmann static int 5318b3d6663SArnd Bergmann spufs_regs_open(struct inode *inode, struct file *file) 5328b3d6663SArnd Bergmann { 5338b3d6663SArnd Bergmann struct spufs_inode_info *i = SPUFS_I(inode); 5348b3d6663SArnd Bergmann file->private_data = i->i_ctx; 5358b3d6663SArnd Bergmann return 0; 5368b3d6663SArnd Bergmann } 5378b3d6663SArnd Bergmann 5388b3d6663SArnd Bergmann static ssize_t 539bf1ab978SDwayne Grant McConnell __spufs_regs_read(struct spu_context *ctx, char __user *buffer, 540bf1ab978SDwayne Grant McConnell size_t size, loff_t *pos) 541bf1ab978SDwayne Grant McConnell { 542bf1ab978SDwayne Grant McConnell struct spu_lscsa *lscsa = ctx->csa.lscsa; 543bf1ab978SDwayne Grant McConnell return simple_read_from_buffer(buffer, size, pos, 544bf1ab978SDwayne Grant McConnell lscsa->gprs, sizeof lscsa->gprs); 545bf1ab978SDwayne Grant McConnell } 546bf1ab978SDwayne Grant McConnell 547bf1ab978SDwayne Grant McConnell static ssize_t 5488b3d6663SArnd Bergmann spufs_regs_read(struct file *file, char __user *buffer, 5498b3d6663SArnd Bergmann size_t size, loff_t *pos) 5508b3d6663SArnd Bergmann { 5518b3d6663SArnd Bergmann int ret; 552bf1ab978SDwayne Grant McConnell struct spu_context *ctx = file->private_data; 5538b3d6663SArnd Bergmann 554f027faa2SJeremy Kerr /* pre-check for file position: if we'd return EOF, there's no point 555f027faa2SJeremy Kerr * causing a deschedule */ 556f027faa2SJeremy Kerr if (*pos >= sizeof(ctx->csa.lscsa->gprs)) 557f027faa2SJeremy Kerr return 0; 558f027faa2SJeremy Kerr 559c9101bdbSChristoph Hellwig ret = spu_acquire_saved(ctx); 560c9101bdbSChristoph Hellwig if (ret) 561c9101bdbSChristoph Hellwig return ret; 562bf1ab978SDwayne Grant McConnell ret = __spufs_regs_read(ctx, buffer, size, pos); 56327b1ea09SChristoph Hellwig spu_release_saved(ctx); 5648b3d6663SArnd Bergmann return ret; 5658b3d6663SArnd Bergmann } 5668b3d6663SArnd Bergmann 5678b3d6663SArnd Bergmann static ssize_t 5688b3d6663SArnd Bergmann spufs_regs_write(struct file *file, const char __user *buffer, 5698b3d6663SArnd Bergmann size_t size, loff_t *pos) 5708b3d6663SArnd Bergmann { 5718b3d6663SArnd Bergmann struct spu_context *ctx = file->private_data; 5728b3d6663SArnd Bergmann struct spu_lscsa *lscsa = ctx->csa.lscsa; 5738b3d6663SArnd Bergmann int ret; 5748b3d6663SArnd Bergmann 5758b3d6663SArnd Bergmann size = min_t(ssize_t, sizeof lscsa->gprs - *pos, size); 5768b3d6663SArnd Bergmann if (size <= 0) 5778b3d6663SArnd Bergmann return -EFBIG; 5788b3d6663SArnd Bergmann *pos += size; 5798b3d6663SArnd Bergmann 580c9101bdbSChristoph Hellwig ret = spu_acquire_saved(ctx); 581c9101bdbSChristoph Hellwig if (ret) 582c9101bdbSChristoph Hellwig return ret; 5838b3d6663SArnd Bergmann 5848b3d6663SArnd Bergmann ret = copy_from_user(lscsa->gprs + *pos - size, 5858b3d6663SArnd Bergmann buffer, size) ? -EFAULT : size; 5868b3d6663SArnd Bergmann 58727b1ea09SChristoph Hellwig spu_release_saved(ctx); 5888b3d6663SArnd Bergmann return ret; 5898b3d6663SArnd Bergmann } 5908b3d6663SArnd Bergmann 5915dfe4c96SArjan van de Ven static const struct file_operations spufs_regs_fops = { 5928b3d6663SArnd Bergmann .open = spufs_regs_open, 5938b3d6663SArnd Bergmann .read = spufs_regs_read, 5948b3d6663SArnd Bergmann .write = spufs_regs_write, 5958b3d6663SArnd Bergmann .llseek = generic_file_llseek, 5968b3d6663SArnd Bergmann }; 5978b3d6663SArnd Bergmann 5988b3d6663SArnd Bergmann static ssize_t 599bf1ab978SDwayne Grant McConnell __spufs_fpcr_read(struct spu_context *ctx, char __user * buffer, 600bf1ab978SDwayne Grant McConnell size_t size, loff_t * pos) 601bf1ab978SDwayne Grant McConnell { 602bf1ab978SDwayne Grant McConnell struct spu_lscsa *lscsa = ctx->csa.lscsa; 603bf1ab978SDwayne Grant McConnell return simple_read_from_buffer(buffer, size, pos, 604bf1ab978SDwayne Grant McConnell &lscsa->fpcr, sizeof(lscsa->fpcr)); 605bf1ab978SDwayne Grant McConnell } 606bf1ab978SDwayne Grant McConnell 607bf1ab978SDwayne Grant McConnell static ssize_t 6088b3d6663SArnd Bergmann spufs_fpcr_read(struct file *file, char __user * buffer, 6098b3d6663SArnd Bergmann size_t size, loff_t * pos) 6108b3d6663SArnd Bergmann { 6118b3d6663SArnd Bergmann int ret; 612bf1ab978SDwayne Grant McConnell struct spu_context *ctx = file->private_data; 6138b3d6663SArnd Bergmann 614c9101bdbSChristoph Hellwig ret = spu_acquire_saved(ctx); 615c9101bdbSChristoph Hellwig if (ret) 616c9101bdbSChristoph Hellwig return ret; 617bf1ab978SDwayne Grant McConnell ret = __spufs_fpcr_read(ctx, buffer, size, pos); 61827b1ea09SChristoph Hellwig spu_release_saved(ctx); 6198b3d6663SArnd Bergmann return ret; 6208b3d6663SArnd Bergmann } 6218b3d6663SArnd Bergmann 6228b3d6663SArnd Bergmann static ssize_t 6238b3d6663SArnd Bergmann spufs_fpcr_write(struct file *file, const char __user * buffer, 6248b3d6663SArnd Bergmann size_t size, loff_t * pos) 6258b3d6663SArnd Bergmann { 6268b3d6663SArnd Bergmann struct spu_context *ctx = file->private_data; 6278b3d6663SArnd Bergmann struct spu_lscsa *lscsa = ctx->csa.lscsa; 6288b3d6663SArnd Bergmann int ret; 6298b3d6663SArnd Bergmann 6308b3d6663SArnd Bergmann size = min_t(ssize_t, sizeof(lscsa->fpcr) - *pos, size); 6318b3d6663SArnd Bergmann if (size <= 0) 6328b3d6663SArnd Bergmann return -EFBIG; 633c9101bdbSChristoph Hellwig 634c9101bdbSChristoph Hellwig ret = spu_acquire_saved(ctx); 635c9101bdbSChristoph Hellwig if (ret) 636c9101bdbSChristoph Hellwig return ret; 637c9101bdbSChristoph Hellwig 6388b3d6663SArnd Bergmann *pos += size; 6398b3d6663SArnd Bergmann ret = copy_from_user((char *)&lscsa->fpcr + *pos - size, 6408b3d6663SArnd Bergmann buffer, size) ? -EFAULT : size; 6418b3d6663SArnd Bergmann 64227b1ea09SChristoph Hellwig spu_release_saved(ctx); 6438b3d6663SArnd Bergmann return ret; 6448b3d6663SArnd Bergmann } 6458b3d6663SArnd Bergmann 6465dfe4c96SArjan van de Ven static const struct file_operations spufs_fpcr_fops = { 6478b3d6663SArnd Bergmann .open = spufs_regs_open, 6488b3d6663SArnd Bergmann .read = spufs_fpcr_read, 6498b3d6663SArnd Bergmann .write = spufs_fpcr_write, 65067207b96SArnd Bergmann .llseek = generic_file_llseek, 65167207b96SArnd Bergmann }; 65267207b96SArnd Bergmann 65367207b96SArnd Bergmann /* generic open function for all pipe-like files */ 65467207b96SArnd Bergmann static int spufs_pipe_open(struct inode *inode, struct file *file) 65567207b96SArnd Bergmann { 65667207b96SArnd Bergmann struct spufs_inode_info *i = SPUFS_I(inode); 65767207b96SArnd Bergmann file->private_data = i->i_ctx; 65867207b96SArnd Bergmann 65967207b96SArnd Bergmann return nonseekable_open(inode, file); 66067207b96SArnd Bergmann } 66167207b96SArnd Bergmann 662cdcc89bbSArnd Bergmann /* 663cdcc89bbSArnd Bergmann * Read as many bytes from the mailbox as possible, until 664cdcc89bbSArnd Bergmann * one of the conditions becomes true: 665cdcc89bbSArnd Bergmann * 666cdcc89bbSArnd Bergmann * - no more data available in the mailbox 667cdcc89bbSArnd Bergmann * - end of the user provided buffer 668cdcc89bbSArnd Bergmann * - end of the mapped area 669cdcc89bbSArnd Bergmann */ 67067207b96SArnd Bergmann static ssize_t spufs_mbox_read(struct file *file, char __user *buf, 67167207b96SArnd Bergmann size_t len, loff_t *pos) 67267207b96SArnd Bergmann { 6738b3d6663SArnd Bergmann struct spu_context *ctx = file->private_data; 674cdcc89bbSArnd Bergmann u32 mbox_data, __user *udata; 675cdcc89bbSArnd Bergmann ssize_t count; 67667207b96SArnd Bergmann 67767207b96SArnd Bergmann if (len < 4) 67867207b96SArnd Bergmann return -EINVAL; 67967207b96SArnd Bergmann 680cdcc89bbSArnd Bergmann if (!access_ok(VERIFY_WRITE, buf, len)) 68167207b96SArnd Bergmann return -EFAULT; 68267207b96SArnd Bergmann 683cdcc89bbSArnd Bergmann udata = (void __user *)buf; 684cdcc89bbSArnd Bergmann 685c9101bdbSChristoph Hellwig count = spu_acquire(ctx); 686c9101bdbSChristoph Hellwig if (count) 687c9101bdbSChristoph Hellwig return count; 688c9101bdbSChristoph Hellwig 689274cef5eSArnd Bergmann for (count = 0; (count + 4) <= len; count += 4, udata++) { 690cdcc89bbSArnd Bergmann int ret; 691cdcc89bbSArnd Bergmann ret = ctx->ops->mbox_read(ctx, &mbox_data); 692cdcc89bbSArnd Bergmann if (ret == 0) 693cdcc89bbSArnd Bergmann break; 694cdcc89bbSArnd Bergmann 695cdcc89bbSArnd Bergmann /* 696cdcc89bbSArnd Bergmann * at the end of the mapped area, we can fault 697cdcc89bbSArnd Bergmann * but still need to return the data we have 698cdcc89bbSArnd Bergmann * read successfully so far. 699cdcc89bbSArnd Bergmann */ 700cdcc89bbSArnd Bergmann ret = __put_user(mbox_data, udata); 701cdcc89bbSArnd Bergmann if (ret) { 702cdcc89bbSArnd Bergmann if (!count) 703cdcc89bbSArnd Bergmann count = -EFAULT; 704cdcc89bbSArnd Bergmann break; 705cdcc89bbSArnd Bergmann } 706cdcc89bbSArnd Bergmann } 707cdcc89bbSArnd Bergmann spu_release(ctx); 708cdcc89bbSArnd Bergmann 709cdcc89bbSArnd Bergmann if (!count) 710cdcc89bbSArnd Bergmann count = -EAGAIN; 711cdcc89bbSArnd Bergmann 712cdcc89bbSArnd Bergmann return count; 71367207b96SArnd Bergmann } 71467207b96SArnd Bergmann 7155dfe4c96SArjan van de Ven static const struct file_operations spufs_mbox_fops = { 71667207b96SArnd Bergmann .open = spufs_pipe_open, 71767207b96SArnd Bergmann .read = spufs_mbox_read, 71867207b96SArnd Bergmann }; 71967207b96SArnd Bergmann 72067207b96SArnd Bergmann static ssize_t spufs_mbox_stat_read(struct file *file, char __user *buf, 72167207b96SArnd Bergmann size_t len, loff_t *pos) 72267207b96SArnd Bergmann { 7238b3d6663SArnd Bergmann struct spu_context *ctx = file->private_data; 724c9101bdbSChristoph Hellwig ssize_t ret; 72567207b96SArnd Bergmann u32 mbox_stat; 72667207b96SArnd Bergmann 72767207b96SArnd Bergmann if (len < 4) 72867207b96SArnd Bergmann return -EINVAL; 72967207b96SArnd Bergmann 730c9101bdbSChristoph Hellwig ret = spu_acquire(ctx); 731c9101bdbSChristoph Hellwig if (ret) 732c9101bdbSChristoph Hellwig return ret; 7338b3d6663SArnd Bergmann 7348b3d6663SArnd Bergmann mbox_stat = ctx->ops->mbox_stat_read(ctx) & 0xff; 7358b3d6663SArnd Bergmann 7368b3d6663SArnd Bergmann spu_release(ctx); 73767207b96SArnd Bergmann 73867207b96SArnd Bergmann if (copy_to_user(buf, &mbox_stat, sizeof mbox_stat)) 73967207b96SArnd Bergmann return -EFAULT; 74067207b96SArnd Bergmann 74167207b96SArnd Bergmann return 4; 74267207b96SArnd Bergmann } 74367207b96SArnd Bergmann 7445dfe4c96SArjan van de Ven static const struct file_operations spufs_mbox_stat_fops = { 74567207b96SArnd Bergmann .open = spufs_pipe_open, 74667207b96SArnd Bergmann .read = spufs_mbox_stat_read, 74767207b96SArnd Bergmann }; 74867207b96SArnd Bergmann 74967207b96SArnd Bergmann /* low-level ibox access function */ 7508b3d6663SArnd Bergmann size_t spu_ibox_read(struct spu_context *ctx, u32 *data) 75167207b96SArnd Bergmann { 7528b3d6663SArnd Bergmann return ctx->ops->ibox_read(ctx, data); 75367207b96SArnd Bergmann } 75467207b96SArnd Bergmann 75567207b96SArnd Bergmann static int spufs_ibox_fasync(int fd, struct file *file, int on) 75667207b96SArnd Bergmann { 7578b3d6663SArnd Bergmann struct spu_context *ctx = file->private_data; 7588b3d6663SArnd Bergmann 7598b3d6663SArnd Bergmann return fasync_helper(fd, file, on, &ctx->ibox_fasync); 7608b3d6663SArnd Bergmann } 7618b3d6663SArnd Bergmann 7628b3d6663SArnd Bergmann /* interrupt-level ibox callback function. */ 7638b3d6663SArnd Bergmann void spufs_ibox_callback(struct spu *spu) 7648b3d6663SArnd Bergmann { 7658b3d6663SArnd Bergmann struct spu_context *ctx = spu->ctx; 7668b3d6663SArnd Bergmann 767e65c2f6fSLuke Browning if (!ctx) 768e65c2f6fSLuke Browning return; 769e65c2f6fSLuke Browning 7708b3d6663SArnd Bergmann wake_up_all(&ctx->ibox_wq); 7718b3d6663SArnd Bergmann kill_fasync(&ctx->ibox_fasync, SIGIO, POLLIN); 77267207b96SArnd Bergmann } 77367207b96SArnd Bergmann 774cdcc89bbSArnd Bergmann /* 775cdcc89bbSArnd Bergmann * Read as many bytes from the interrupt mailbox as possible, until 776cdcc89bbSArnd Bergmann * one of the conditions becomes true: 777cdcc89bbSArnd Bergmann * 778cdcc89bbSArnd Bergmann * - no more data available in the mailbox 779cdcc89bbSArnd Bergmann * - end of the user provided buffer 780cdcc89bbSArnd Bergmann * - end of the mapped area 781cdcc89bbSArnd Bergmann * 782cdcc89bbSArnd Bergmann * If the file is opened without O_NONBLOCK, we wait here until 783cdcc89bbSArnd Bergmann * any data is available, but return when we have been able to 784cdcc89bbSArnd Bergmann * read something. 785cdcc89bbSArnd Bergmann */ 78667207b96SArnd Bergmann static ssize_t spufs_ibox_read(struct file *file, char __user *buf, 78767207b96SArnd Bergmann size_t len, loff_t *pos) 78867207b96SArnd Bergmann { 7898b3d6663SArnd Bergmann struct spu_context *ctx = file->private_data; 790cdcc89bbSArnd Bergmann u32 ibox_data, __user *udata; 791cdcc89bbSArnd Bergmann ssize_t count; 79267207b96SArnd Bergmann 79367207b96SArnd Bergmann if (len < 4) 79467207b96SArnd Bergmann return -EINVAL; 79567207b96SArnd Bergmann 796cdcc89bbSArnd Bergmann if (!access_ok(VERIFY_WRITE, buf, len)) 797cdcc89bbSArnd Bergmann return -EFAULT; 798cdcc89bbSArnd Bergmann 799cdcc89bbSArnd Bergmann udata = (void __user *)buf; 800cdcc89bbSArnd Bergmann 801c9101bdbSChristoph Hellwig count = spu_acquire(ctx); 802c9101bdbSChristoph Hellwig if (count) 803eebead5bSChristoph Hellwig goto out; 80467207b96SArnd Bergmann 805cdcc89bbSArnd Bergmann /* wait only for the first element */ 806cdcc89bbSArnd Bergmann count = 0; 80767207b96SArnd Bergmann if (file->f_flags & O_NONBLOCK) { 808eebead5bSChristoph Hellwig if (!spu_ibox_read(ctx, &ibox_data)) { 809cdcc89bbSArnd Bergmann count = -EAGAIN; 810eebead5bSChristoph Hellwig goto out_unlock; 811eebead5bSChristoph Hellwig } 81267207b96SArnd Bergmann } else { 813cdcc89bbSArnd Bergmann count = spufs_wait(ctx->ibox_wq, spu_ibox_read(ctx, &ibox_data)); 814cdcc89bbSArnd Bergmann if (count) 815cdcc89bbSArnd Bergmann goto out; 816eebead5bSChristoph Hellwig } 817cdcc89bbSArnd Bergmann 818cdcc89bbSArnd Bergmann /* if we can't write at all, return -EFAULT */ 819cdcc89bbSArnd Bergmann count = __put_user(ibox_data, udata); 820cdcc89bbSArnd Bergmann if (count) 821eebead5bSChristoph Hellwig goto out_unlock; 822cdcc89bbSArnd Bergmann 823cdcc89bbSArnd Bergmann for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) { 824cdcc89bbSArnd Bergmann int ret; 825cdcc89bbSArnd Bergmann ret = ctx->ops->ibox_read(ctx, &ibox_data); 826cdcc89bbSArnd Bergmann if (ret == 0) 827cdcc89bbSArnd Bergmann break; 828cdcc89bbSArnd Bergmann /* 829cdcc89bbSArnd Bergmann * at the end of the mapped area, we can fault 830cdcc89bbSArnd Bergmann * but still need to return the data we have 831cdcc89bbSArnd Bergmann * read successfully so far. 832cdcc89bbSArnd Bergmann */ 833cdcc89bbSArnd Bergmann ret = __put_user(ibox_data, udata); 834cdcc89bbSArnd Bergmann if (ret) 835cdcc89bbSArnd Bergmann break; 83667207b96SArnd Bergmann } 83767207b96SArnd Bergmann 838eebead5bSChristoph Hellwig out_unlock: 8398b3d6663SArnd Bergmann spu_release(ctx); 840eebead5bSChristoph Hellwig out: 841cdcc89bbSArnd Bergmann return count; 84267207b96SArnd Bergmann } 84367207b96SArnd Bergmann 84467207b96SArnd Bergmann static unsigned int spufs_ibox_poll(struct file *file, poll_table *wait) 84567207b96SArnd Bergmann { 8468b3d6663SArnd Bergmann struct spu_context *ctx = file->private_data; 84767207b96SArnd Bergmann unsigned int mask; 84867207b96SArnd Bergmann 8498b3d6663SArnd Bergmann poll_wait(file, &ctx->ibox_wq, wait); 85067207b96SArnd Bergmann 851c9101bdbSChristoph Hellwig /* 852c9101bdbSChristoph Hellwig * For now keep this uninterruptible and also ignore the rule 853c9101bdbSChristoph Hellwig * that poll should not sleep. Will be fixed later. 854c9101bdbSChristoph Hellwig */ 855c9101bdbSChristoph Hellwig mutex_lock(&ctx->state_mutex); 8563a843d7cSArnd Bergmann mask = ctx->ops->mbox_stat_poll(ctx, POLLIN | POLLRDNORM); 8573a843d7cSArnd Bergmann spu_release(ctx); 85867207b96SArnd Bergmann 85967207b96SArnd Bergmann return mask; 86067207b96SArnd Bergmann } 86167207b96SArnd Bergmann 8625dfe4c96SArjan van de Ven static const struct file_operations spufs_ibox_fops = { 86367207b96SArnd Bergmann .open = spufs_pipe_open, 86467207b96SArnd Bergmann .read = spufs_ibox_read, 86567207b96SArnd Bergmann .poll = spufs_ibox_poll, 86667207b96SArnd Bergmann .fasync = spufs_ibox_fasync, 86767207b96SArnd Bergmann }; 86867207b96SArnd Bergmann 86967207b96SArnd Bergmann static ssize_t spufs_ibox_stat_read(struct file *file, char __user *buf, 87067207b96SArnd Bergmann size_t len, loff_t *pos) 87167207b96SArnd Bergmann { 8728b3d6663SArnd Bergmann struct spu_context *ctx = file->private_data; 873c9101bdbSChristoph Hellwig ssize_t ret; 87467207b96SArnd Bergmann u32 ibox_stat; 87567207b96SArnd Bergmann 87667207b96SArnd Bergmann if (len < 4) 87767207b96SArnd Bergmann return -EINVAL; 87867207b96SArnd Bergmann 879c9101bdbSChristoph Hellwig ret = spu_acquire(ctx); 880c9101bdbSChristoph Hellwig if (ret) 881c9101bdbSChristoph Hellwig return ret; 8828b3d6663SArnd Bergmann ibox_stat = (ctx->ops->mbox_stat_read(ctx) >> 16) & 0xff; 8838b3d6663SArnd Bergmann spu_release(ctx); 88467207b96SArnd Bergmann 88567207b96SArnd Bergmann if (copy_to_user(buf, &ibox_stat, sizeof ibox_stat)) 88667207b96SArnd Bergmann return -EFAULT; 88767207b96SArnd Bergmann 88867207b96SArnd Bergmann return 4; 88967207b96SArnd Bergmann } 89067207b96SArnd Bergmann 8915dfe4c96SArjan van de Ven static const struct file_operations spufs_ibox_stat_fops = { 89267207b96SArnd Bergmann .open = spufs_pipe_open, 89367207b96SArnd Bergmann .read = spufs_ibox_stat_read, 89467207b96SArnd Bergmann }; 89567207b96SArnd Bergmann 89667207b96SArnd Bergmann /* low-level mailbox write */ 8978b3d6663SArnd Bergmann size_t spu_wbox_write(struct spu_context *ctx, u32 data) 89867207b96SArnd Bergmann { 8998b3d6663SArnd Bergmann return ctx->ops->wbox_write(ctx, data); 90067207b96SArnd Bergmann } 90167207b96SArnd Bergmann 90267207b96SArnd Bergmann static int spufs_wbox_fasync(int fd, struct file *file, int on) 90367207b96SArnd Bergmann { 9048b3d6663SArnd Bergmann struct spu_context *ctx = file->private_data; 9058b3d6663SArnd Bergmann int ret; 9068b3d6663SArnd Bergmann 9078b3d6663SArnd Bergmann ret = fasync_helper(fd, file, on, &ctx->wbox_fasync); 9088b3d6663SArnd Bergmann 9098b3d6663SArnd Bergmann return ret; 9108b3d6663SArnd Bergmann } 9118b3d6663SArnd Bergmann 9128b3d6663SArnd Bergmann /* interrupt-level wbox callback function. */ 9138b3d6663SArnd Bergmann void spufs_wbox_callback(struct spu *spu) 9148b3d6663SArnd Bergmann { 9158b3d6663SArnd Bergmann struct spu_context *ctx = spu->ctx; 9168b3d6663SArnd Bergmann 917e65c2f6fSLuke Browning if (!ctx) 918e65c2f6fSLuke Browning return; 919e65c2f6fSLuke Browning 9208b3d6663SArnd Bergmann wake_up_all(&ctx->wbox_wq); 9218b3d6663SArnd Bergmann kill_fasync(&ctx->wbox_fasync, SIGIO, POLLOUT); 92267207b96SArnd Bergmann } 92367207b96SArnd Bergmann 924cdcc89bbSArnd Bergmann /* 925cdcc89bbSArnd Bergmann * Write as many bytes to the interrupt mailbox as possible, until 926cdcc89bbSArnd Bergmann * one of the conditions becomes true: 927cdcc89bbSArnd Bergmann * 928cdcc89bbSArnd Bergmann * - the mailbox is full 929cdcc89bbSArnd Bergmann * - end of the user provided buffer 930cdcc89bbSArnd Bergmann * - end of the mapped area 931cdcc89bbSArnd Bergmann * 932cdcc89bbSArnd Bergmann * If the file is opened without O_NONBLOCK, we wait here until 933cdcc89bbSArnd Bergmann * space is availabyl, but return when we have been able to 934cdcc89bbSArnd Bergmann * write something. 935cdcc89bbSArnd Bergmann */ 93667207b96SArnd Bergmann static ssize_t spufs_wbox_write(struct file *file, const char __user *buf, 93767207b96SArnd Bergmann size_t len, loff_t *pos) 93867207b96SArnd Bergmann { 9398b3d6663SArnd Bergmann struct spu_context *ctx = file->private_data; 940cdcc89bbSArnd Bergmann u32 wbox_data, __user *udata; 941cdcc89bbSArnd Bergmann ssize_t count; 94267207b96SArnd Bergmann 94367207b96SArnd Bergmann if (len < 4) 94467207b96SArnd Bergmann return -EINVAL; 94567207b96SArnd Bergmann 946cdcc89bbSArnd Bergmann udata = (void __user *)buf; 947cdcc89bbSArnd Bergmann if (!access_ok(VERIFY_READ, buf, len)) 948cdcc89bbSArnd Bergmann return -EFAULT; 949cdcc89bbSArnd Bergmann 950cdcc89bbSArnd Bergmann if (__get_user(wbox_data, udata)) 95167207b96SArnd Bergmann return -EFAULT; 95267207b96SArnd Bergmann 953c9101bdbSChristoph Hellwig count = spu_acquire(ctx); 954c9101bdbSChristoph Hellwig if (count) 955eebead5bSChristoph Hellwig goto out; 9568b3d6663SArnd Bergmann 957cdcc89bbSArnd Bergmann /* 958cdcc89bbSArnd Bergmann * make sure we can at least write one element, by waiting 959cdcc89bbSArnd Bergmann * in case of !O_NONBLOCK 960cdcc89bbSArnd Bergmann */ 961cdcc89bbSArnd Bergmann count = 0; 96267207b96SArnd Bergmann if (file->f_flags & O_NONBLOCK) { 963eebead5bSChristoph Hellwig if (!spu_wbox_write(ctx, wbox_data)) { 964cdcc89bbSArnd Bergmann count = -EAGAIN; 965eebead5bSChristoph Hellwig goto out_unlock; 966eebead5bSChristoph Hellwig } 96767207b96SArnd Bergmann } else { 968cdcc89bbSArnd Bergmann count = spufs_wait(ctx->wbox_wq, spu_wbox_write(ctx, wbox_data)); 969cdcc89bbSArnd Bergmann if (count) 970cdcc89bbSArnd Bergmann goto out; 971eebead5bSChristoph Hellwig } 972eebead5bSChristoph Hellwig 9738b3d6663SArnd Bergmann 97496de0e25SJan Engelhardt /* write as much as possible */ 975cdcc89bbSArnd Bergmann for (count = 4, udata++; (count + 4) <= len; count += 4, udata++) { 976cdcc89bbSArnd Bergmann int ret; 977cdcc89bbSArnd Bergmann ret = __get_user(wbox_data, udata); 978cdcc89bbSArnd Bergmann if (ret) 979cdcc89bbSArnd Bergmann break; 980cdcc89bbSArnd Bergmann 981cdcc89bbSArnd Bergmann ret = spu_wbox_write(ctx, wbox_data); 982cdcc89bbSArnd Bergmann if (ret == 0) 983cdcc89bbSArnd Bergmann break; 984cdcc89bbSArnd Bergmann } 985cdcc89bbSArnd Bergmann 986eebead5bSChristoph Hellwig out_unlock: 987cdcc89bbSArnd Bergmann spu_release(ctx); 988eebead5bSChristoph Hellwig out: 989cdcc89bbSArnd Bergmann return count; 99067207b96SArnd Bergmann } 99167207b96SArnd Bergmann 99267207b96SArnd Bergmann static unsigned int spufs_wbox_poll(struct file *file, poll_table *wait) 99367207b96SArnd Bergmann { 9948b3d6663SArnd Bergmann struct spu_context *ctx = file->private_data; 99567207b96SArnd Bergmann unsigned int mask; 99667207b96SArnd Bergmann 9978b3d6663SArnd Bergmann poll_wait(file, &ctx->wbox_wq, wait); 99867207b96SArnd Bergmann 999c9101bdbSChristoph Hellwig /* 1000c9101bdbSChristoph Hellwig * For now keep this uninterruptible and also ignore the rule 1001c9101bdbSChristoph Hellwig * that poll should not sleep. Will be fixed later. 1002c9101bdbSChristoph Hellwig */ 1003c9101bdbSChristoph Hellwig mutex_lock(&ctx->state_mutex); 10043a843d7cSArnd Bergmann mask = ctx->ops->mbox_stat_poll(ctx, POLLOUT | POLLWRNORM); 10053a843d7cSArnd Bergmann spu_release(ctx); 100667207b96SArnd Bergmann 100767207b96SArnd Bergmann return mask; 100867207b96SArnd Bergmann } 100967207b96SArnd Bergmann 10105dfe4c96SArjan van de Ven static const struct file_operations spufs_wbox_fops = { 101167207b96SArnd Bergmann .open = spufs_pipe_open, 101267207b96SArnd Bergmann .write = spufs_wbox_write, 101367207b96SArnd Bergmann .poll = spufs_wbox_poll, 101467207b96SArnd Bergmann .fasync = spufs_wbox_fasync, 101567207b96SArnd Bergmann }; 101667207b96SArnd Bergmann 101767207b96SArnd Bergmann static ssize_t spufs_wbox_stat_read(struct file *file, char __user *buf, 101867207b96SArnd Bergmann size_t len, loff_t *pos) 101967207b96SArnd Bergmann { 10208b3d6663SArnd Bergmann struct spu_context *ctx = file->private_data; 1021c9101bdbSChristoph Hellwig ssize_t ret; 102267207b96SArnd Bergmann u32 wbox_stat; 102367207b96SArnd Bergmann 102467207b96SArnd Bergmann if (len < 4) 102567207b96SArnd Bergmann return -EINVAL; 102667207b96SArnd Bergmann 1027c9101bdbSChristoph Hellwig ret = spu_acquire(ctx); 1028c9101bdbSChristoph Hellwig if (ret) 1029c9101bdbSChristoph Hellwig return ret; 10308b3d6663SArnd Bergmann wbox_stat = (ctx->ops->mbox_stat_read(ctx) >> 8) & 0xff; 10318b3d6663SArnd Bergmann spu_release(ctx); 103267207b96SArnd Bergmann 103367207b96SArnd Bergmann if (copy_to_user(buf, &wbox_stat, sizeof wbox_stat)) 103467207b96SArnd Bergmann return -EFAULT; 103567207b96SArnd Bergmann 103667207b96SArnd Bergmann return 4; 103767207b96SArnd Bergmann } 103867207b96SArnd Bergmann 10395dfe4c96SArjan van de Ven static const struct file_operations spufs_wbox_stat_fops = { 104067207b96SArnd Bergmann .open = spufs_pipe_open, 104167207b96SArnd Bergmann .read = spufs_wbox_stat_read, 104267207b96SArnd Bergmann }; 104367207b96SArnd Bergmann 10446df10a82SMark Nutter static int spufs_signal1_open(struct inode *inode, struct file *file) 10456df10a82SMark Nutter { 10466df10a82SMark Nutter struct spufs_inode_info *i = SPUFS_I(inode); 10476df10a82SMark Nutter struct spu_context *ctx = i->i_ctx; 104843c2bbd9SChristoph Hellwig 104947d3a5faSChristoph Hellwig mutex_lock(&ctx->mapping_lock); 10506df10a82SMark Nutter file->private_data = ctx; 105143c2bbd9SChristoph Hellwig if (!i->i_openers++) 10526df10a82SMark Nutter ctx->signal1 = inode->i_mapping; 105347d3a5faSChristoph Hellwig mutex_unlock(&ctx->mapping_lock); 10546df10a82SMark Nutter return nonseekable_open(inode, file); 10556df10a82SMark Nutter } 10566df10a82SMark Nutter 105743c2bbd9SChristoph Hellwig static int 105843c2bbd9SChristoph Hellwig spufs_signal1_release(struct inode *inode, struct file *file) 105943c2bbd9SChristoph Hellwig { 106043c2bbd9SChristoph Hellwig struct spufs_inode_info *i = SPUFS_I(inode); 106143c2bbd9SChristoph Hellwig struct spu_context *ctx = i->i_ctx; 106243c2bbd9SChristoph Hellwig 106347d3a5faSChristoph Hellwig mutex_lock(&ctx->mapping_lock); 106443c2bbd9SChristoph Hellwig if (!--i->i_openers) 106543c2bbd9SChristoph Hellwig ctx->signal1 = NULL; 106647d3a5faSChristoph Hellwig mutex_unlock(&ctx->mapping_lock); 106743c2bbd9SChristoph Hellwig return 0; 106843c2bbd9SChristoph Hellwig } 106943c2bbd9SChristoph Hellwig 1070bf1ab978SDwayne Grant McConnell static ssize_t __spufs_signal1_read(struct spu_context *ctx, char __user *buf, 107167207b96SArnd Bergmann size_t len, loff_t *pos) 107267207b96SArnd Bergmann { 107317f88cebSDwayne Grant McConnell int ret = 0; 107467207b96SArnd Bergmann u32 data; 107567207b96SArnd Bergmann 107667207b96SArnd Bergmann if (len < 4) 107767207b96SArnd Bergmann return -EINVAL; 107867207b96SArnd Bergmann 107917f88cebSDwayne Grant McConnell if (ctx->csa.spu_chnlcnt_RW[3]) { 108017f88cebSDwayne Grant McConnell data = ctx->csa.spu_chnldata_RW[3]; 108117f88cebSDwayne Grant McConnell ret = 4; 108217f88cebSDwayne Grant McConnell } 10838b3d6663SArnd Bergmann 108417f88cebSDwayne Grant McConnell if (!ret) 108517f88cebSDwayne Grant McConnell goto out; 108617f88cebSDwayne Grant McConnell 108767207b96SArnd Bergmann if (copy_to_user(buf, &data, 4)) 108867207b96SArnd Bergmann return -EFAULT; 108967207b96SArnd Bergmann 109017f88cebSDwayne Grant McConnell out: 109117f88cebSDwayne Grant McConnell return ret; 109267207b96SArnd Bergmann } 109367207b96SArnd Bergmann 1094bf1ab978SDwayne Grant McConnell static ssize_t spufs_signal1_read(struct file *file, char __user *buf, 1095bf1ab978SDwayne Grant McConnell size_t len, loff_t *pos) 1096bf1ab978SDwayne Grant McConnell { 1097bf1ab978SDwayne Grant McConnell int ret; 1098bf1ab978SDwayne Grant McConnell struct spu_context *ctx = file->private_data; 1099bf1ab978SDwayne Grant McConnell 1100c9101bdbSChristoph Hellwig ret = spu_acquire_saved(ctx); 1101c9101bdbSChristoph Hellwig if (ret) 1102c9101bdbSChristoph Hellwig return ret; 1103bf1ab978SDwayne Grant McConnell ret = __spufs_signal1_read(ctx, buf, len, pos); 110427b1ea09SChristoph Hellwig spu_release_saved(ctx); 1105bf1ab978SDwayne Grant McConnell 1106bf1ab978SDwayne Grant McConnell return ret; 1107bf1ab978SDwayne Grant McConnell } 1108bf1ab978SDwayne Grant McConnell 110967207b96SArnd Bergmann static ssize_t spufs_signal1_write(struct file *file, const char __user *buf, 111067207b96SArnd Bergmann size_t len, loff_t *pos) 111167207b96SArnd Bergmann { 111267207b96SArnd Bergmann struct spu_context *ctx; 1113c9101bdbSChristoph Hellwig ssize_t ret; 111467207b96SArnd Bergmann u32 data; 111567207b96SArnd Bergmann 111667207b96SArnd Bergmann ctx = file->private_data; 111767207b96SArnd Bergmann 111867207b96SArnd Bergmann if (len < 4) 111967207b96SArnd Bergmann return -EINVAL; 112067207b96SArnd Bergmann 112167207b96SArnd Bergmann if (copy_from_user(&data, buf, 4)) 112267207b96SArnd Bergmann return -EFAULT; 112367207b96SArnd Bergmann 1124c9101bdbSChristoph Hellwig ret = spu_acquire(ctx); 1125c9101bdbSChristoph Hellwig if (ret) 1126c9101bdbSChristoph Hellwig return ret; 11278b3d6663SArnd Bergmann ctx->ops->signal1_write(ctx, data); 11288b3d6663SArnd Bergmann spu_release(ctx); 112967207b96SArnd Bergmann 113067207b96SArnd Bergmann return 4; 113167207b96SArnd Bergmann } 113267207b96SArnd Bergmann 1133b1e2270fSNick Piggin static int 1134b1e2270fSNick Piggin spufs_signal1_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 11356df10a82SMark Nutter { 113687ff6090SJeremy Kerr #if SPUFS_SIGNAL_MAP_SIZE == 0x1000 113787ff6090SJeremy Kerr return spufs_ps_fault(vma, vmf, 0x14000, SPUFS_SIGNAL_MAP_SIZE); 113887ff6090SJeremy Kerr #elif SPUFS_SIGNAL_MAP_SIZE == 0x10000 113927d5bf2aSBenjamin Herrenschmidt /* For 64k pages, both signal1 and signal2 can be used to mmap the whole 114027d5bf2aSBenjamin Herrenschmidt * signal 1 and 2 area 114127d5bf2aSBenjamin Herrenschmidt */ 114287ff6090SJeremy Kerr return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE); 114327d5bf2aSBenjamin Herrenschmidt #else 114427d5bf2aSBenjamin Herrenschmidt #error unsupported page size 114527d5bf2aSBenjamin Herrenschmidt #endif 11466df10a82SMark Nutter } 11476df10a82SMark Nutter 11486df10a82SMark Nutter static struct vm_operations_struct spufs_signal1_mmap_vmops = { 1149b1e2270fSNick Piggin .fault = spufs_signal1_mmap_fault, 11506df10a82SMark Nutter }; 11516df10a82SMark Nutter 11526df10a82SMark Nutter static int spufs_signal1_mmap(struct file *file, struct vm_area_struct *vma) 11536df10a82SMark Nutter { 11546df10a82SMark Nutter if (!(vma->vm_flags & VM_SHARED)) 11556df10a82SMark Nutter return -EINVAL; 11566df10a82SMark Nutter 115778bde53eSBenjamin Herrenschmidt vma->vm_flags |= VM_IO | VM_PFNMAP; 11586df10a82SMark Nutter vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 115923cc7701SBenjamin Herrenschmidt | _PAGE_NO_CACHE | _PAGE_GUARDED); 11606df10a82SMark Nutter 11616df10a82SMark Nutter vma->vm_ops = &spufs_signal1_mmap_vmops; 11626df10a82SMark Nutter return 0; 11636df10a82SMark Nutter } 11646df10a82SMark Nutter 11655dfe4c96SArjan van de Ven static const struct file_operations spufs_signal1_fops = { 11666df10a82SMark Nutter .open = spufs_signal1_open, 116743c2bbd9SChristoph Hellwig .release = spufs_signal1_release, 116867207b96SArnd Bergmann .read = spufs_signal1_read, 116967207b96SArnd Bergmann .write = spufs_signal1_write, 11706df10a82SMark Nutter .mmap = spufs_signal1_mmap, 117167207b96SArnd Bergmann }; 117267207b96SArnd Bergmann 1173d054b36fSJeremy Kerr static const struct file_operations spufs_signal1_nosched_fops = { 1174d054b36fSJeremy Kerr .open = spufs_signal1_open, 1175d054b36fSJeremy Kerr .release = spufs_signal1_release, 1176d054b36fSJeremy Kerr .write = spufs_signal1_write, 1177d054b36fSJeremy Kerr .mmap = spufs_signal1_mmap, 1178d054b36fSJeremy Kerr }; 1179d054b36fSJeremy Kerr 11806df10a82SMark Nutter static int spufs_signal2_open(struct inode *inode, struct file *file) 11816df10a82SMark Nutter { 11826df10a82SMark Nutter struct spufs_inode_info *i = SPUFS_I(inode); 11836df10a82SMark Nutter struct spu_context *ctx = i->i_ctx; 118443c2bbd9SChristoph Hellwig 118547d3a5faSChristoph Hellwig mutex_lock(&ctx->mapping_lock); 11866df10a82SMark Nutter file->private_data = ctx; 118743c2bbd9SChristoph Hellwig if (!i->i_openers++) 11886df10a82SMark Nutter ctx->signal2 = inode->i_mapping; 118947d3a5faSChristoph Hellwig mutex_unlock(&ctx->mapping_lock); 11906df10a82SMark Nutter return nonseekable_open(inode, file); 11916df10a82SMark Nutter } 11926df10a82SMark Nutter 119343c2bbd9SChristoph Hellwig static int 119443c2bbd9SChristoph Hellwig spufs_signal2_release(struct inode *inode, struct file *file) 119543c2bbd9SChristoph Hellwig { 119643c2bbd9SChristoph Hellwig struct spufs_inode_info *i = SPUFS_I(inode); 119743c2bbd9SChristoph Hellwig struct spu_context *ctx = i->i_ctx; 119843c2bbd9SChristoph Hellwig 119947d3a5faSChristoph Hellwig mutex_lock(&ctx->mapping_lock); 120043c2bbd9SChristoph Hellwig if (!--i->i_openers) 120143c2bbd9SChristoph Hellwig ctx->signal2 = NULL; 120247d3a5faSChristoph Hellwig mutex_unlock(&ctx->mapping_lock); 120343c2bbd9SChristoph Hellwig return 0; 120443c2bbd9SChristoph Hellwig } 120543c2bbd9SChristoph Hellwig 1206bf1ab978SDwayne Grant McConnell static ssize_t __spufs_signal2_read(struct spu_context *ctx, char __user *buf, 120767207b96SArnd Bergmann size_t len, loff_t *pos) 120867207b96SArnd Bergmann { 120917f88cebSDwayne Grant McConnell int ret = 0; 121067207b96SArnd Bergmann u32 data; 121167207b96SArnd Bergmann 121267207b96SArnd Bergmann if (len < 4) 121367207b96SArnd Bergmann return -EINVAL; 121467207b96SArnd Bergmann 121517f88cebSDwayne Grant McConnell if (ctx->csa.spu_chnlcnt_RW[4]) { 121617f88cebSDwayne Grant McConnell data = ctx->csa.spu_chnldata_RW[4]; 121717f88cebSDwayne Grant McConnell ret = 4; 121817f88cebSDwayne Grant McConnell } 12198b3d6663SArnd Bergmann 122017f88cebSDwayne Grant McConnell if (!ret) 122117f88cebSDwayne Grant McConnell goto out; 122217f88cebSDwayne Grant McConnell 122367207b96SArnd Bergmann if (copy_to_user(buf, &data, 4)) 122467207b96SArnd Bergmann return -EFAULT; 122567207b96SArnd Bergmann 122617f88cebSDwayne Grant McConnell out: 1227bf1ab978SDwayne Grant McConnell return ret; 1228bf1ab978SDwayne Grant McConnell } 1229bf1ab978SDwayne Grant McConnell 1230bf1ab978SDwayne Grant McConnell static ssize_t spufs_signal2_read(struct file *file, char __user *buf, 1231bf1ab978SDwayne Grant McConnell size_t len, loff_t *pos) 1232bf1ab978SDwayne Grant McConnell { 1233bf1ab978SDwayne Grant McConnell struct spu_context *ctx = file->private_data; 1234bf1ab978SDwayne Grant McConnell int ret; 1235bf1ab978SDwayne Grant McConnell 1236c9101bdbSChristoph Hellwig ret = spu_acquire_saved(ctx); 1237c9101bdbSChristoph Hellwig if (ret) 1238c9101bdbSChristoph Hellwig return ret; 1239bf1ab978SDwayne Grant McConnell ret = __spufs_signal2_read(ctx, buf, len, pos); 124027b1ea09SChristoph Hellwig spu_release_saved(ctx); 1241bf1ab978SDwayne Grant McConnell 1242bf1ab978SDwayne Grant McConnell return ret; 124367207b96SArnd Bergmann } 124467207b96SArnd Bergmann 124567207b96SArnd Bergmann static ssize_t spufs_signal2_write(struct file *file, const char __user *buf, 124667207b96SArnd Bergmann size_t len, loff_t *pos) 124767207b96SArnd Bergmann { 124867207b96SArnd Bergmann struct spu_context *ctx; 1249c9101bdbSChristoph Hellwig ssize_t ret; 125067207b96SArnd Bergmann u32 data; 125167207b96SArnd Bergmann 125267207b96SArnd Bergmann ctx = file->private_data; 125367207b96SArnd Bergmann 125467207b96SArnd Bergmann if (len < 4) 125567207b96SArnd Bergmann return -EINVAL; 125667207b96SArnd Bergmann 125767207b96SArnd Bergmann if (copy_from_user(&data, buf, 4)) 125867207b96SArnd Bergmann return -EFAULT; 125967207b96SArnd Bergmann 1260c9101bdbSChristoph Hellwig ret = spu_acquire(ctx); 1261c9101bdbSChristoph Hellwig if (ret) 1262c9101bdbSChristoph Hellwig return ret; 12638b3d6663SArnd Bergmann ctx->ops->signal2_write(ctx, data); 12648b3d6663SArnd Bergmann spu_release(ctx); 126567207b96SArnd Bergmann 126667207b96SArnd Bergmann return 4; 126767207b96SArnd Bergmann } 126867207b96SArnd Bergmann 126927d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K 1270b1e2270fSNick Piggin static int 1271b1e2270fSNick Piggin spufs_signal2_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 12726df10a82SMark Nutter { 127387ff6090SJeremy Kerr #if SPUFS_SIGNAL_MAP_SIZE == 0x1000 127487ff6090SJeremy Kerr return spufs_ps_fault(vma, vmf, 0x1c000, SPUFS_SIGNAL_MAP_SIZE); 127587ff6090SJeremy Kerr #elif SPUFS_SIGNAL_MAP_SIZE == 0x10000 127627d5bf2aSBenjamin Herrenschmidt /* For 64k pages, both signal1 and signal2 can be used to mmap the whole 127727d5bf2aSBenjamin Herrenschmidt * signal 1 and 2 area 127827d5bf2aSBenjamin Herrenschmidt */ 127987ff6090SJeremy Kerr return spufs_ps_fault(vma, vmf, 0x10000, SPUFS_SIGNAL_MAP_SIZE); 128027d5bf2aSBenjamin Herrenschmidt #else 128127d5bf2aSBenjamin Herrenschmidt #error unsupported page size 128227d5bf2aSBenjamin Herrenschmidt #endif 12836df10a82SMark Nutter } 12846df10a82SMark Nutter 12856df10a82SMark Nutter static struct vm_operations_struct spufs_signal2_mmap_vmops = { 1286b1e2270fSNick Piggin .fault = spufs_signal2_mmap_fault, 12876df10a82SMark Nutter }; 12886df10a82SMark Nutter 12896df10a82SMark Nutter static int spufs_signal2_mmap(struct file *file, struct vm_area_struct *vma) 12906df10a82SMark Nutter { 12916df10a82SMark Nutter if (!(vma->vm_flags & VM_SHARED)) 12926df10a82SMark Nutter return -EINVAL; 12936df10a82SMark Nutter 129478bde53eSBenjamin Herrenschmidt vma->vm_flags |= VM_IO | VM_PFNMAP; 12956df10a82SMark Nutter vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 129623cc7701SBenjamin Herrenschmidt | _PAGE_NO_CACHE | _PAGE_GUARDED); 12976df10a82SMark Nutter 12986df10a82SMark Nutter vma->vm_ops = &spufs_signal2_mmap_vmops; 12996df10a82SMark Nutter return 0; 13006df10a82SMark Nutter } 130127d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */ 130227d5bf2aSBenjamin Herrenschmidt #define spufs_signal2_mmap NULL 130327d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */ 13046df10a82SMark Nutter 13055dfe4c96SArjan van de Ven static const struct file_operations spufs_signal2_fops = { 13066df10a82SMark Nutter .open = spufs_signal2_open, 130743c2bbd9SChristoph Hellwig .release = spufs_signal2_release, 130867207b96SArnd Bergmann .read = spufs_signal2_read, 130967207b96SArnd Bergmann .write = spufs_signal2_write, 13106df10a82SMark Nutter .mmap = spufs_signal2_mmap, 131167207b96SArnd Bergmann }; 131267207b96SArnd Bergmann 1313d054b36fSJeremy Kerr static const struct file_operations spufs_signal2_nosched_fops = { 1314d054b36fSJeremy Kerr .open = spufs_signal2_open, 1315d054b36fSJeremy Kerr .release = spufs_signal2_release, 1316d054b36fSJeremy Kerr .write = spufs_signal2_write, 1317d054b36fSJeremy Kerr .mmap = spufs_signal2_mmap, 1318d054b36fSJeremy Kerr }; 1319d054b36fSJeremy Kerr 1320104f0cc2SMichael Ellerman /* 1321104f0cc2SMichael Ellerman * This is a wrapper around DEFINE_SIMPLE_ATTRIBUTE which does the 1322104f0cc2SMichael Ellerman * work of acquiring (or not) the SPU context before calling through 1323104f0cc2SMichael Ellerman * to the actual get routine. The set routine is called directly. 1324104f0cc2SMichael Ellerman */ 1325104f0cc2SMichael Ellerman #define SPU_ATTR_NOACQUIRE 0 1326104f0cc2SMichael Ellerman #define SPU_ATTR_ACQUIRE 1 1327104f0cc2SMichael Ellerman #define SPU_ATTR_ACQUIRE_SAVED 2 1328104f0cc2SMichael Ellerman 1329104f0cc2SMichael Ellerman #define DEFINE_SPUFS_ATTRIBUTE(__name, __get, __set, __fmt, __acquire) \ 1330197b1a82SChristoph Hellwig static int __##__get(void *data, u64 *val) \ 1331104f0cc2SMichael Ellerman { \ 1332104f0cc2SMichael Ellerman struct spu_context *ctx = data; \ 1333c9101bdbSChristoph Hellwig int ret = 0; \ 1334104f0cc2SMichael Ellerman \ 1335104f0cc2SMichael Ellerman if (__acquire == SPU_ATTR_ACQUIRE) { \ 1336c9101bdbSChristoph Hellwig ret = spu_acquire(ctx); \ 1337c9101bdbSChristoph Hellwig if (ret) \ 1338c9101bdbSChristoph Hellwig return ret; \ 1339197b1a82SChristoph Hellwig *val = __get(ctx); \ 1340104f0cc2SMichael Ellerman spu_release(ctx); \ 1341104f0cc2SMichael Ellerman } else if (__acquire == SPU_ATTR_ACQUIRE_SAVED) { \ 1342c9101bdbSChristoph Hellwig ret = spu_acquire_saved(ctx); \ 1343c9101bdbSChristoph Hellwig if (ret) \ 1344c9101bdbSChristoph Hellwig return ret; \ 1345197b1a82SChristoph Hellwig *val = __get(ctx); \ 1346104f0cc2SMichael Ellerman spu_release_saved(ctx); \ 1347104f0cc2SMichael Ellerman } else \ 1348197b1a82SChristoph Hellwig *val = __get(ctx); \ 1349104f0cc2SMichael Ellerman \ 1350197b1a82SChristoph Hellwig return 0; \ 1351104f0cc2SMichael Ellerman } \ 1352197b1a82SChristoph Hellwig DEFINE_SPUFS_SIMPLE_ATTRIBUTE(__name, __##__get, __set, __fmt); 1353104f0cc2SMichael Ellerman 1354197b1a82SChristoph Hellwig static int spufs_signal1_type_set(void *data, u64 val) 135567207b96SArnd Bergmann { 135667207b96SArnd Bergmann struct spu_context *ctx = data; 1357c9101bdbSChristoph Hellwig int ret; 135867207b96SArnd Bergmann 1359c9101bdbSChristoph Hellwig ret = spu_acquire(ctx); 1360c9101bdbSChristoph Hellwig if (ret) 1361c9101bdbSChristoph Hellwig return ret; 13628b3d6663SArnd Bergmann ctx->ops->signal1_type_set(ctx, val); 13638b3d6663SArnd Bergmann spu_release(ctx); 1364197b1a82SChristoph Hellwig 1365197b1a82SChristoph Hellwig return 0; 136667207b96SArnd Bergmann } 136767207b96SArnd Bergmann 1368104f0cc2SMichael Ellerman static u64 spufs_signal1_type_get(struct spu_context *ctx) 1369bf1ab978SDwayne Grant McConnell { 1370bf1ab978SDwayne Grant McConnell return ctx->ops->signal1_type_get(ctx); 1371bf1ab978SDwayne Grant McConnell } 1372104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_signal1_type, spufs_signal1_type_get, 1373af8b44e0SJeremy Kerr spufs_signal1_type_set, "%llu\n", SPU_ATTR_ACQUIRE); 1374bf1ab978SDwayne Grant McConnell 137567207b96SArnd Bergmann 1376197b1a82SChristoph Hellwig static int spufs_signal2_type_set(void *data, u64 val) 137767207b96SArnd Bergmann { 137867207b96SArnd Bergmann struct spu_context *ctx = data; 1379c9101bdbSChristoph Hellwig int ret; 138067207b96SArnd Bergmann 1381c9101bdbSChristoph Hellwig ret = spu_acquire(ctx); 1382c9101bdbSChristoph Hellwig if (ret) 1383c9101bdbSChristoph Hellwig return ret; 13848b3d6663SArnd Bergmann ctx->ops->signal2_type_set(ctx, val); 13858b3d6663SArnd Bergmann spu_release(ctx); 1386197b1a82SChristoph Hellwig 1387197b1a82SChristoph Hellwig return 0; 138867207b96SArnd Bergmann } 138967207b96SArnd Bergmann 1390104f0cc2SMichael Ellerman static u64 spufs_signal2_type_get(struct spu_context *ctx) 1391bf1ab978SDwayne Grant McConnell { 1392bf1ab978SDwayne Grant McConnell return ctx->ops->signal2_type_get(ctx); 1393bf1ab978SDwayne Grant McConnell } 1394104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_signal2_type, spufs_signal2_type_get, 1395af8b44e0SJeremy Kerr spufs_signal2_type_set, "%llu\n", SPU_ATTR_ACQUIRE); 139667207b96SArnd Bergmann 139727d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K 1398b1e2270fSNick Piggin static int 1399b1e2270fSNick Piggin spufs_mss_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 1400d9379c4bSarnd@arndb.de { 140187ff6090SJeremy Kerr return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_MSS_MAP_SIZE); 1402d9379c4bSarnd@arndb.de } 1403d9379c4bSarnd@arndb.de 1404d9379c4bSarnd@arndb.de static struct vm_operations_struct spufs_mss_mmap_vmops = { 1405b1e2270fSNick Piggin .fault = spufs_mss_mmap_fault, 1406d9379c4bSarnd@arndb.de }; 1407d9379c4bSarnd@arndb.de 1408d9379c4bSarnd@arndb.de /* 1409d9379c4bSarnd@arndb.de * mmap support for problem state MFC DMA area [0x0000 - 0x0fff]. 1410d9379c4bSarnd@arndb.de */ 1411d9379c4bSarnd@arndb.de static int spufs_mss_mmap(struct file *file, struct vm_area_struct *vma) 1412d9379c4bSarnd@arndb.de { 1413d9379c4bSarnd@arndb.de if (!(vma->vm_flags & VM_SHARED)) 1414d9379c4bSarnd@arndb.de return -EINVAL; 1415d9379c4bSarnd@arndb.de 141678bde53eSBenjamin Herrenschmidt vma->vm_flags |= VM_IO | VM_PFNMAP; 1417d9379c4bSarnd@arndb.de vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 141823cc7701SBenjamin Herrenschmidt | _PAGE_NO_CACHE | _PAGE_GUARDED); 1419d9379c4bSarnd@arndb.de 1420d9379c4bSarnd@arndb.de vma->vm_ops = &spufs_mss_mmap_vmops; 1421d9379c4bSarnd@arndb.de return 0; 1422d9379c4bSarnd@arndb.de } 142327d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */ 142427d5bf2aSBenjamin Herrenschmidt #define spufs_mss_mmap NULL 142527d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */ 1426d9379c4bSarnd@arndb.de 1427d9379c4bSarnd@arndb.de static int spufs_mss_open(struct inode *inode, struct file *file) 1428d9379c4bSarnd@arndb.de { 1429d9379c4bSarnd@arndb.de struct spufs_inode_info *i = SPUFS_I(inode); 143017e0e270SBenjamin Herrenschmidt struct spu_context *ctx = i->i_ctx; 1431d9379c4bSarnd@arndb.de 1432d9379c4bSarnd@arndb.de file->private_data = i->i_ctx; 143343c2bbd9SChristoph Hellwig 143447d3a5faSChristoph Hellwig mutex_lock(&ctx->mapping_lock); 143543c2bbd9SChristoph Hellwig if (!i->i_openers++) 143617e0e270SBenjamin Herrenschmidt ctx->mss = inode->i_mapping; 143747d3a5faSChristoph Hellwig mutex_unlock(&ctx->mapping_lock); 1438d9379c4bSarnd@arndb.de return nonseekable_open(inode, file); 1439d9379c4bSarnd@arndb.de } 1440d9379c4bSarnd@arndb.de 144143c2bbd9SChristoph Hellwig static int 144243c2bbd9SChristoph Hellwig spufs_mss_release(struct inode *inode, struct file *file) 144343c2bbd9SChristoph Hellwig { 144443c2bbd9SChristoph Hellwig struct spufs_inode_info *i = SPUFS_I(inode); 144543c2bbd9SChristoph Hellwig struct spu_context *ctx = i->i_ctx; 144643c2bbd9SChristoph Hellwig 144747d3a5faSChristoph Hellwig mutex_lock(&ctx->mapping_lock); 144843c2bbd9SChristoph Hellwig if (!--i->i_openers) 144943c2bbd9SChristoph Hellwig ctx->mss = NULL; 145047d3a5faSChristoph Hellwig mutex_unlock(&ctx->mapping_lock); 145143c2bbd9SChristoph Hellwig return 0; 145243c2bbd9SChristoph Hellwig } 145343c2bbd9SChristoph Hellwig 14545dfe4c96SArjan van de Ven static const struct file_operations spufs_mss_fops = { 1455d9379c4bSarnd@arndb.de .open = spufs_mss_open, 145643c2bbd9SChristoph Hellwig .release = spufs_mss_release, 1457d9379c4bSarnd@arndb.de .mmap = spufs_mss_mmap, 145827d5bf2aSBenjamin Herrenschmidt }; 145927d5bf2aSBenjamin Herrenschmidt 1460b1e2270fSNick Piggin static int 1461b1e2270fSNick Piggin spufs_psmap_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 146227d5bf2aSBenjamin Herrenschmidt { 146387ff6090SJeremy Kerr return spufs_ps_fault(vma, vmf, 0x0000, SPUFS_PS_MAP_SIZE); 146427d5bf2aSBenjamin Herrenschmidt } 146527d5bf2aSBenjamin Herrenschmidt 146627d5bf2aSBenjamin Herrenschmidt static struct vm_operations_struct spufs_psmap_mmap_vmops = { 1467b1e2270fSNick Piggin .fault = spufs_psmap_mmap_fault, 146827d5bf2aSBenjamin Herrenschmidt }; 146927d5bf2aSBenjamin Herrenschmidt 147027d5bf2aSBenjamin Herrenschmidt /* 147127d5bf2aSBenjamin Herrenschmidt * mmap support for full problem state area [0x00000 - 0x1ffff]. 147227d5bf2aSBenjamin Herrenschmidt */ 147327d5bf2aSBenjamin Herrenschmidt static int spufs_psmap_mmap(struct file *file, struct vm_area_struct *vma) 147427d5bf2aSBenjamin Herrenschmidt { 147527d5bf2aSBenjamin Herrenschmidt if (!(vma->vm_flags & VM_SHARED)) 147627d5bf2aSBenjamin Herrenschmidt return -EINVAL; 147727d5bf2aSBenjamin Herrenschmidt 147878bde53eSBenjamin Herrenschmidt vma->vm_flags |= VM_IO | VM_PFNMAP; 147927d5bf2aSBenjamin Herrenschmidt vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 148027d5bf2aSBenjamin Herrenschmidt | _PAGE_NO_CACHE | _PAGE_GUARDED); 148127d5bf2aSBenjamin Herrenschmidt 148227d5bf2aSBenjamin Herrenschmidt vma->vm_ops = &spufs_psmap_mmap_vmops; 148327d5bf2aSBenjamin Herrenschmidt return 0; 148427d5bf2aSBenjamin Herrenschmidt } 148527d5bf2aSBenjamin Herrenschmidt 148627d5bf2aSBenjamin Herrenschmidt static int spufs_psmap_open(struct inode *inode, struct file *file) 148727d5bf2aSBenjamin Herrenschmidt { 148827d5bf2aSBenjamin Herrenschmidt struct spufs_inode_info *i = SPUFS_I(inode); 148917e0e270SBenjamin Herrenschmidt struct spu_context *ctx = i->i_ctx; 149027d5bf2aSBenjamin Herrenschmidt 149147d3a5faSChristoph Hellwig mutex_lock(&ctx->mapping_lock); 149227d5bf2aSBenjamin Herrenschmidt file->private_data = i->i_ctx; 149343c2bbd9SChristoph Hellwig if (!i->i_openers++) 149417e0e270SBenjamin Herrenschmidt ctx->psmap = inode->i_mapping; 149547d3a5faSChristoph Hellwig mutex_unlock(&ctx->mapping_lock); 149627d5bf2aSBenjamin Herrenschmidt return nonseekable_open(inode, file); 149727d5bf2aSBenjamin Herrenschmidt } 149827d5bf2aSBenjamin Herrenschmidt 149943c2bbd9SChristoph Hellwig static int 150043c2bbd9SChristoph Hellwig spufs_psmap_release(struct inode *inode, struct file *file) 150143c2bbd9SChristoph Hellwig { 150243c2bbd9SChristoph Hellwig struct spufs_inode_info *i = SPUFS_I(inode); 150343c2bbd9SChristoph Hellwig struct spu_context *ctx = i->i_ctx; 150443c2bbd9SChristoph Hellwig 150547d3a5faSChristoph Hellwig mutex_lock(&ctx->mapping_lock); 150643c2bbd9SChristoph Hellwig if (!--i->i_openers) 150743c2bbd9SChristoph Hellwig ctx->psmap = NULL; 150847d3a5faSChristoph Hellwig mutex_unlock(&ctx->mapping_lock); 150943c2bbd9SChristoph Hellwig return 0; 151043c2bbd9SChristoph Hellwig } 151143c2bbd9SChristoph Hellwig 15125dfe4c96SArjan van de Ven static const struct file_operations spufs_psmap_fops = { 151327d5bf2aSBenjamin Herrenschmidt .open = spufs_psmap_open, 151443c2bbd9SChristoph Hellwig .release = spufs_psmap_release, 151527d5bf2aSBenjamin Herrenschmidt .mmap = spufs_psmap_mmap, 1516d9379c4bSarnd@arndb.de }; 1517d9379c4bSarnd@arndb.de 1518d9379c4bSarnd@arndb.de 151927d5bf2aSBenjamin Herrenschmidt #if SPUFS_MMAP_4K 1520b1e2270fSNick Piggin static int 1521b1e2270fSNick Piggin spufs_mfc_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 15226df10a82SMark Nutter { 152387ff6090SJeremy Kerr return spufs_ps_fault(vma, vmf, 0x3000, SPUFS_MFC_MAP_SIZE); 15246df10a82SMark Nutter } 15256df10a82SMark Nutter 15266df10a82SMark Nutter static struct vm_operations_struct spufs_mfc_mmap_vmops = { 1527b1e2270fSNick Piggin .fault = spufs_mfc_mmap_fault, 15286df10a82SMark Nutter }; 15296df10a82SMark Nutter 15306df10a82SMark Nutter /* 15316df10a82SMark Nutter * mmap support for problem state MFC DMA area [0x0000 - 0x0fff]. 15326df10a82SMark Nutter */ 15336df10a82SMark Nutter static int spufs_mfc_mmap(struct file *file, struct vm_area_struct *vma) 15346df10a82SMark Nutter { 15356df10a82SMark Nutter if (!(vma->vm_flags & VM_SHARED)) 15366df10a82SMark Nutter return -EINVAL; 15376df10a82SMark Nutter 153878bde53eSBenjamin Herrenschmidt vma->vm_flags |= VM_IO | VM_PFNMAP; 15396df10a82SMark Nutter vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) 154023cc7701SBenjamin Herrenschmidt | _PAGE_NO_CACHE | _PAGE_GUARDED); 15416df10a82SMark Nutter 15426df10a82SMark Nutter vma->vm_ops = &spufs_mfc_mmap_vmops; 15436df10a82SMark Nutter return 0; 15446df10a82SMark Nutter } 154527d5bf2aSBenjamin Herrenschmidt #else /* SPUFS_MMAP_4K */ 154627d5bf2aSBenjamin Herrenschmidt #define spufs_mfc_mmap NULL 154727d5bf2aSBenjamin Herrenschmidt #endif /* !SPUFS_MMAP_4K */ 1548a33a7d73SArnd Bergmann 1549a33a7d73SArnd Bergmann static int spufs_mfc_open(struct inode *inode, struct file *file) 1550a33a7d73SArnd Bergmann { 1551a33a7d73SArnd Bergmann struct spufs_inode_info *i = SPUFS_I(inode); 1552a33a7d73SArnd Bergmann struct spu_context *ctx = i->i_ctx; 1553a33a7d73SArnd Bergmann 1554a33a7d73SArnd Bergmann /* we don't want to deal with DMA into other processes */ 1555a33a7d73SArnd Bergmann if (ctx->owner != current->mm) 1556a33a7d73SArnd Bergmann return -EINVAL; 1557a33a7d73SArnd Bergmann 1558a33a7d73SArnd Bergmann if (atomic_read(&inode->i_count) != 1) 1559a33a7d73SArnd Bergmann return -EBUSY; 1560a33a7d73SArnd Bergmann 156147d3a5faSChristoph Hellwig mutex_lock(&ctx->mapping_lock); 1562a33a7d73SArnd Bergmann file->private_data = ctx; 156343c2bbd9SChristoph Hellwig if (!i->i_openers++) 156417e0e270SBenjamin Herrenschmidt ctx->mfc = inode->i_mapping; 156547d3a5faSChristoph Hellwig mutex_unlock(&ctx->mapping_lock); 1566a33a7d73SArnd Bergmann return nonseekable_open(inode, file); 1567a33a7d73SArnd Bergmann } 1568a33a7d73SArnd Bergmann 156943c2bbd9SChristoph Hellwig static int 157043c2bbd9SChristoph Hellwig spufs_mfc_release(struct inode *inode, struct file *file) 157143c2bbd9SChristoph Hellwig { 157243c2bbd9SChristoph Hellwig struct spufs_inode_info *i = SPUFS_I(inode); 157343c2bbd9SChristoph Hellwig struct spu_context *ctx = i->i_ctx; 157443c2bbd9SChristoph Hellwig 157547d3a5faSChristoph Hellwig mutex_lock(&ctx->mapping_lock); 157643c2bbd9SChristoph Hellwig if (!--i->i_openers) 157743c2bbd9SChristoph Hellwig ctx->mfc = NULL; 157847d3a5faSChristoph Hellwig mutex_unlock(&ctx->mapping_lock); 157943c2bbd9SChristoph Hellwig return 0; 158043c2bbd9SChristoph Hellwig } 158143c2bbd9SChristoph Hellwig 1582a33a7d73SArnd Bergmann /* interrupt-level mfc callback function. */ 1583a33a7d73SArnd Bergmann void spufs_mfc_callback(struct spu *spu) 1584a33a7d73SArnd Bergmann { 1585a33a7d73SArnd Bergmann struct spu_context *ctx = spu->ctx; 1586a33a7d73SArnd Bergmann 1587e65c2f6fSLuke Browning if (!ctx) 1588e65c2f6fSLuke Browning return; 1589e65c2f6fSLuke Browning 1590a33a7d73SArnd Bergmann wake_up_all(&ctx->mfc_wq); 1591a33a7d73SArnd Bergmann 1592e48b1b45SHarvey Harrison pr_debug("%s %s\n", __func__, spu->name); 1593a33a7d73SArnd Bergmann if (ctx->mfc_fasync) { 1594a33a7d73SArnd Bergmann u32 free_elements, tagstatus; 1595a33a7d73SArnd Bergmann unsigned int mask; 1596a33a7d73SArnd Bergmann 1597a33a7d73SArnd Bergmann /* no need for spu_acquire in interrupt context */ 1598a33a7d73SArnd Bergmann free_elements = ctx->ops->get_mfc_free_elements(ctx); 1599a33a7d73SArnd Bergmann tagstatus = ctx->ops->read_mfc_tagstatus(ctx); 1600a33a7d73SArnd Bergmann 1601a33a7d73SArnd Bergmann mask = 0; 1602a33a7d73SArnd Bergmann if (free_elements & 0xffff) 1603a33a7d73SArnd Bergmann mask |= POLLOUT; 1604a33a7d73SArnd Bergmann if (tagstatus & ctx->tagwait) 1605a33a7d73SArnd Bergmann mask |= POLLIN; 1606a33a7d73SArnd Bergmann 1607a33a7d73SArnd Bergmann kill_fasync(&ctx->mfc_fasync, SIGIO, mask); 1608a33a7d73SArnd Bergmann } 1609a33a7d73SArnd Bergmann } 1610a33a7d73SArnd Bergmann 1611a33a7d73SArnd Bergmann static int spufs_read_mfc_tagstatus(struct spu_context *ctx, u32 *status) 1612a33a7d73SArnd Bergmann { 1613a33a7d73SArnd Bergmann /* See if there is one tag group is complete */ 1614a33a7d73SArnd Bergmann /* FIXME we need locking around tagwait */ 1615a33a7d73SArnd Bergmann *status = ctx->ops->read_mfc_tagstatus(ctx) & ctx->tagwait; 1616a33a7d73SArnd Bergmann ctx->tagwait &= ~*status; 1617a33a7d73SArnd Bergmann if (*status) 1618a33a7d73SArnd Bergmann return 1; 1619a33a7d73SArnd Bergmann 1620a33a7d73SArnd Bergmann /* enable interrupt waiting for any tag group, 1621a33a7d73SArnd Bergmann may silently fail if interrupts are already enabled */ 1622a33a7d73SArnd Bergmann ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1); 1623a33a7d73SArnd Bergmann return 0; 1624a33a7d73SArnd Bergmann } 1625a33a7d73SArnd Bergmann 1626a33a7d73SArnd Bergmann static ssize_t spufs_mfc_read(struct file *file, char __user *buffer, 1627a33a7d73SArnd Bergmann size_t size, loff_t *pos) 1628a33a7d73SArnd Bergmann { 1629a33a7d73SArnd Bergmann struct spu_context *ctx = file->private_data; 1630a33a7d73SArnd Bergmann int ret = -EINVAL; 1631a33a7d73SArnd Bergmann u32 status; 1632a33a7d73SArnd Bergmann 1633a33a7d73SArnd Bergmann if (size != 4) 1634a33a7d73SArnd Bergmann goto out; 1635a33a7d73SArnd Bergmann 1636c9101bdbSChristoph Hellwig ret = spu_acquire(ctx); 1637c9101bdbSChristoph Hellwig if (ret) 1638c9101bdbSChristoph Hellwig return ret; 1639c9101bdbSChristoph Hellwig 1640c9101bdbSChristoph Hellwig ret = -EINVAL; 1641a33a7d73SArnd Bergmann if (file->f_flags & O_NONBLOCK) { 1642a33a7d73SArnd Bergmann status = ctx->ops->read_mfc_tagstatus(ctx); 1643a33a7d73SArnd Bergmann if (!(status & ctx->tagwait)) 1644a33a7d73SArnd Bergmann ret = -EAGAIN; 1645a33a7d73SArnd Bergmann else 1646c9101bdbSChristoph Hellwig /* XXX(hch): shouldn't we clear ret here? */ 1647a33a7d73SArnd Bergmann ctx->tagwait &= ~status; 1648a33a7d73SArnd Bergmann } else { 1649a33a7d73SArnd Bergmann ret = spufs_wait(ctx->mfc_wq, 1650a33a7d73SArnd Bergmann spufs_read_mfc_tagstatus(ctx, &status)); 1651a33a7d73SArnd Bergmann if (ret) 1652a33a7d73SArnd Bergmann goto out; 1653eebead5bSChristoph Hellwig } 1654eebead5bSChristoph Hellwig spu_release(ctx); 1655a33a7d73SArnd Bergmann 1656a33a7d73SArnd Bergmann ret = 4; 1657a33a7d73SArnd Bergmann if (copy_to_user(buffer, &status, 4)) 1658a33a7d73SArnd Bergmann ret = -EFAULT; 1659a33a7d73SArnd Bergmann 1660a33a7d73SArnd Bergmann out: 1661a33a7d73SArnd Bergmann return ret; 1662a33a7d73SArnd Bergmann } 1663a33a7d73SArnd Bergmann 1664a33a7d73SArnd Bergmann static int spufs_check_valid_dma(struct mfc_dma_command *cmd) 1665a33a7d73SArnd Bergmann { 1666a33a7d73SArnd Bergmann pr_debug("queueing DMA %x %lx %x %x %x\n", cmd->lsa, 1667a33a7d73SArnd Bergmann cmd->ea, cmd->size, cmd->tag, cmd->cmd); 1668a33a7d73SArnd Bergmann 1669a33a7d73SArnd Bergmann switch (cmd->cmd) { 1670a33a7d73SArnd Bergmann case MFC_PUT_CMD: 1671a33a7d73SArnd Bergmann case MFC_PUTF_CMD: 1672a33a7d73SArnd Bergmann case MFC_PUTB_CMD: 1673a33a7d73SArnd Bergmann case MFC_GET_CMD: 1674a33a7d73SArnd Bergmann case MFC_GETF_CMD: 1675a33a7d73SArnd Bergmann case MFC_GETB_CMD: 1676a33a7d73SArnd Bergmann break; 1677a33a7d73SArnd Bergmann default: 1678a33a7d73SArnd Bergmann pr_debug("invalid DMA opcode %x\n", cmd->cmd); 1679a33a7d73SArnd Bergmann return -EIO; 1680a33a7d73SArnd Bergmann } 1681a33a7d73SArnd Bergmann 1682a33a7d73SArnd Bergmann if ((cmd->lsa & 0xf) != (cmd->ea &0xf)) { 1683a33a7d73SArnd Bergmann pr_debug("invalid DMA alignment, ea %lx lsa %x\n", 1684a33a7d73SArnd Bergmann cmd->ea, cmd->lsa); 1685a33a7d73SArnd Bergmann return -EIO; 1686a33a7d73SArnd Bergmann } 1687a33a7d73SArnd Bergmann 1688a33a7d73SArnd Bergmann switch (cmd->size & 0xf) { 1689a33a7d73SArnd Bergmann case 1: 1690a33a7d73SArnd Bergmann break; 1691a33a7d73SArnd Bergmann case 2: 1692a33a7d73SArnd Bergmann if (cmd->lsa & 1) 1693a33a7d73SArnd Bergmann goto error; 1694a33a7d73SArnd Bergmann break; 1695a33a7d73SArnd Bergmann case 4: 1696a33a7d73SArnd Bergmann if (cmd->lsa & 3) 1697a33a7d73SArnd Bergmann goto error; 1698a33a7d73SArnd Bergmann break; 1699a33a7d73SArnd Bergmann case 8: 1700a33a7d73SArnd Bergmann if (cmd->lsa & 7) 1701a33a7d73SArnd Bergmann goto error; 1702a33a7d73SArnd Bergmann break; 1703a33a7d73SArnd Bergmann case 0: 1704a33a7d73SArnd Bergmann if (cmd->lsa & 15) 1705a33a7d73SArnd Bergmann goto error; 1706a33a7d73SArnd Bergmann break; 1707a33a7d73SArnd Bergmann error: 1708a33a7d73SArnd Bergmann default: 1709a33a7d73SArnd Bergmann pr_debug("invalid DMA alignment %x for size %x\n", 1710a33a7d73SArnd Bergmann cmd->lsa & 0xf, cmd->size); 1711a33a7d73SArnd Bergmann return -EIO; 1712a33a7d73SArnd Bergmann } 1713a33a7d73SArnd Bergmann 1714a33a7d73SArnd Bergmann if (cmd->size > 16 * 1024) { 1715a33a7d73SArnd Bergmann pr_debug("invalid DMA size %x\n", cmd->size); 1716a33a7d73SArnd Bergmann return -EIO; 1717a33a7d73SArnd Bergmann } 1718a33a7d73SArnd Bergmann 1719a33a7d73SArnd Bergmann if (cmd->tag & 0xfff0) { 1720a33a7d73SArnd Bergmann /* we reserve the higher tag numbers for kernel use */ 1721a33a7d73SArnd Bergmann pr_debug("invalid DMA tag\n"); 1722a33a7d73SArnd Bergmann return -EIO; 1723a33a7d73SArnd Bergmann } 1724a33a7d73SArnd Bergmann 1725a33a7d73SArnd Bergmann if (cmd->class) { 1726a33a7d73SArnd Bergmann /* not supported in this version */ 1727a33a7d73SArnd Bergmann pr_debug("invalid DMA class\n"); 1728a33a7d73SArnd Bergmann return -EIO; 1729a33a7d73SArnd Bergmann } 1730a33a7d73SArnd Bergmann 1731a33a7d73SArnd Bergmann return 0; 1732a33a7d73SArnd Bergmann } 1733a33a7d73SArnd Bergmann 1734a33a7d73SArnd Bergmann static int spu_send_mfc_command(struct spu_context *ctx, 1735a33a7d73SArnd Bergmann struct mfc_dma_command cmd, 1736a33a7d73SArnd Bergmann int *error) 1737a33a7d73SArnd Bergmann { 1738a33a7d73SArnd Bergmann *error = ctx->ops->send_mfc_command(ctx, &cmd); 1739a33a7d73SArnd Bergmann if (*error == -EAGAIN) { 1740a33a7d73SArnd Bergmann /* wait for any tag group to complete 1741a33a7d73SArnd Bergmann so we have space for the new command */ 1742a33a7d73SArnd Bergmann ctx->ops->set_mfc_query(ctx, ctx->tagwait, 1); 1743a33a7d73SArnd Bergmann /* try again, because the queue might be 1744a33a7d73SArnd Bergmann empty again */ 1745a33a7d73SArnd Bergmann *error = ctx->ops->send_mfc_command(ctx, &cmd); 1746a33a7d73SArnd Bergmann if (*error == -EAGAIN) 1747a33a7d73SArnd Bergmann return 0; 1748a33a7d73SArnd Bergmann } 1749a33a7d73SArnd Bergmann return 1; 1750a33a7d73SArnd Bergmann } 1751a33a7d73SArnd Bergmann 1752a33a7d73SArnd Bergmann static ssize_t spufs_mfc_write(struct file *file, const char __user *buffer, 1753a33a7d73SArnd Bergmann size_t size, loff_t *pos) 1754a33a7d73SArnd Bergmann { 1755a33a7d73SArnd Bergmann struct spu_context *ctx = file->private_data; 1756a33a7d73SArnd Bergmann struct mfc_dma_command cmd; 1757a33a7d73SArnd Bergmann int ret = -EINVAL; 1758a33a7d73SArnd Bergmann 1759a33a7d73SArnd Bergmann if (size != sizeof cmd) 1760a33a7d73SArnd Bergmann goto out; 1761a33a7d73SArnd Bergmann 1762a33a7d73SArnd Bergmann ret = -EFAULT; 1763a33a7d73SArnd Bergmann if (copy_from_user(&cmd, buffer, sizeof cmd)) 1764a33a7d73SArnd Bergmann goto out; 1765a33a7d73SArnd Bergmann 1766a33a7d73SArnd Bergmann ret = spufs_check_valid_dma(&cmd); 1767a33a7d73SArnd Bergmann if (ret) 1768a33a7d73SArnd Bergmann goto out; 1769a33a7d73SArnd Bergmann 1770c9101bdbSChristoph Hellwig ret = spu_acquire(ctx); 1771c9101bdbSChristoph Hellwig if (ret) 1772c9101bdbSChristoph Hellwig goto out; 1773c9101bdbSChristoph Hellwig 177433bfd7a7SArnd Bergmann ret = spufs_wait(ctx->run_wq, ctx->state == SPU_STATE_RUNNABLE); 1775577f8f10SAkinobu Mita if (ret) 1776577f8f10SAkinobu Mita goto out; 1777577f8f10SAkinobu Mita 1778a33a7d73SArnd Bergmann if (file->f_flags & O_NONBLOCK) { 1779a33a7d73SArnd Bergmann ret = ctx->ops->send_mfc_command(ctx, &cmd); 1780a33a7d73SArnd Bergmann } else { 1781a33a7d73SArnd Bergmann int status; 1782a33a7d73SArnd Bergmann ret = spufs_wait(ctx->mfc_wq, 1783a33a7d73SArnd Bergmann spu_send_mfc_command(ctx, cmd, &status)); 1784eebead5bSChristoph Hellwig if (ret) 1785eebead5bSChristoph Hellwig goto out; 1786a33a7d73SArnd Bergmann if (status) 1787a33a7d73SArnd Bergmann ret = status; 1788a33a7d73SArnd Bergmann } 1789a33a7d73SArnd Bergmann 1790a33a7d73SArnd Bergmann if (ret) 1791933b0e35SKazunori Asayama goto out_unlock; 1792a33a7d73SArnd Bergmann 1793a33a7d73SArnd Bergmann ctx->tagwait |= 1 << cmd.tag; 17943692dc66SMasato Noguchi ret = size; 1795a33a7d73SArnd Bergmann 1796933b0e35SKazunori Asayama out_unlock: 1797933b0e35SKazunori Asayama spu_release(ctx); 1798a33a7d73SArnd Bergmann out: 1799a33a7d73SArnd Bergmann return ret; 1800a33a7d73SArnd Bergmann } 1801a33a7d73SArnd Bergmann 1802a33a7d73SArnd Bergmann static unsigned int spufs_mfc_poll(struct file *file,poll_table *wait) 1803a33a7d73SArnd Bergmann { 1804a33a7d73SArnd Bergmann struct spu_context *ctx = file->private_data; 1805a33a7d73SArnd Bergmann u32 free_elements, tagstatus; 1806a33a7d73SArnd Bergmann unsigned int mask; 1807a33a7d73SArnd Bergmann 1808933b0e35SKazunori Asayama poll_wait(file, &ctx->mfc_wq, wait); 1809933b0e35SKazunori Asayama 1810c9101bdbSChristoph Hellwig /* 1811c9101bdbSChristoph Hellwig * For now keep this uninterruptible and also ignore the rule 1812c9101bdbSChristoph Hellwig * that poll should not sleep. Will be fixed later. 1813c9101bdbSChristoph Hellwig */ 1814c9101bdbSChristoph Hellwig mutex_lock(&ctx->state_mutex); 1815a33a7d73SArnd Bergmann ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2); 1816a33a7d73SArnd Bergmann free_elements = ctx->ops->get_mfc_free_elements(ctx); 1817a33a7d73SArnd Bergmann tagstatus = ctx->ops->read_mfc_tagstatus(ctx); 1818a33a7d73SArnd Bergmann spu_release(ctx); 1819a33a7d73SArnd Bergmann 1820a33a7d73SArnd Bergmann mask = 0; 1821a33a7d73SArnd Bergmann if (free_elements & 0xffff) 1822a33a7d73SArnd Bergmann mask |= POLLOUT | POLLWRNORM; 1823a33a7d73SArnd Bergmann if (tagstatus & ctx->tagwait) 1824a33a7d73SArnd Bergmann mask |= POLLIN | POLLRDNORM; 1825a33a7d73SArnd Bergmann 1826e48b1b45SHarvey Harrison pr_debug("%s: free %d tagstatus %d tagwait %d\n", __func__, 1827a33a7d73SArnd Bergmann free_elements, tagstatus, ctx->tagwait); 1828a33a7d73SArnd Bergmann 1829a33a7d73SArnd Bergmann return mask; 1830a33a7d73SArnd Bergmann } 1831a33a7d73SArnd Bergmann 183273b6af8aSAl Viro static int spufs_mfc_flush(struct file *file, fl_owner_t id) 1833a33a7d73SArnd Bergmann { 1834a33a7d73SArnd Bergmann struct spu_context *ctx = file->private_data; 1835a33a7d73SArnd Bergmann int ret; 1836a33a7d73SArnd Bergmann 1837c9101bdbSChristoph Hellwig ret = spu_acquire(ctx); 1838c9101bdbSChristoph Hellwig if (ret) 1839eebead5bSChristoph Hellwig goto out; 1840a33a7d73SArnd Bergmann #if 0 1841a33a7d73SArnd Bergmann /* this currently hangs */ 1842a33a7d73SArnd Bergmann ret = spufs_wait(ctx->mfc_wq, 1843a33a7d73SArnd Bergmann ctx->ops->set_mfc_query(ctx, ctx->tagwait, 2)); 1844a33a7d73SArnd Bergmann if (ret) 1845a33a7d73SArnd Bergmann goto out; 1846a33a7d73SArnd Bergmann ret = spufs_wait(ctx->mfc_wq, 1847a33a7d73SArnd Bergmann ctx->ops->read_mfc_tagstatus(ctx) == ctx->tagwait); 1848eebead5bSChristoph Hellwig if (ret) 1849eebead5bSChristoph Hellwig goto out; 1850a33a7d73SArnd Bergmann #else 1851a33a7d73SArnd Bergmann ret = 0; 1852a33a7d73SArnd Bergmann #endif 1853a33a7d73SArnd Bergmann spu_release(ctx); 1854eebead5bSChristoph Hellwig out: 1855a33a7d73SArnd Bergmann return ret; 1856a33a7d73SArnd Bergmann } 1857a33a7d73SArnd Bergmann 1858a33a7d73SArnd Bergmann static int spufs_mfc_fsync(struct file *file, struct dentry *dentry, 1859a33a7d73SArnd Bergmann int datasync) 1860a33a7d73SArnd Bergmann { 186173b6af8aSAl Viro return spufs_mfc_flush(file, NULL); 1862a33a7d73SArnd Bergmann } 1863a33a7d73SArnd Bergmann 1864a33a7d73SArnd Bergmann static int spufs_mfc_fasync(int fd, struct file *file, int on) 1865a33a7d73SArnd Bergmann { 1866a33a7d73SArnd Bergmann struct spu_context *ctx = file->private_data; 1867a33a7d73SArnd Bergmann 1868a33a7d73SArnd Bergmann return fasync_helper(fd, file, on, &ctx->mfc_fasync); 1869a33a7d73SArnd Bergmann } 1870a33a7d73SArnd Bergmann 18715dfe4c96SArjan van de Ven static const struct file_operations spufs_mfc_fops = { 1872a33a7d73SArnd Bergmann .open = spufs_mfc_open, 187343c2bbd9SChristoph Hellwig .release = spufs_mfc_release, 1874a33a7d73SArnd Bergmann .read = spufs_mfc_read, 1875a33a7d73SArnd Bergmann .write = spufs_mfc_write, 1876a33a7d73SArnd Bergmann .poll = spufs_mfc_poll, 1877a33a7d73SArnd Bergmann .flush = spufs_mfc_flush, 1878a33a7d73SArnd Bergmann .fsync = spufs_mfc_fsync, 1879a33a7d73SArnd Bergmann .fasync = spufs_mfc_fasync, 18806df10a82SMark Nutter .mmap = spufs_mfc_mmap, 1881a33a7d73SArnd Bergmann }; 1882a33a7d73SArnd Bergmann 1883197b1a82SChristoph Hellwig static int spufs_npc_set(void *data, u64 val) 188467207b96SArnd Bergmann { 188567207b96SArnd Bergmann struct spu_context *ctx = data; 1886c9101bdbSChristoph Hellwig int ret; 1887c9101bdbSChristoph Hellwig 1888c9101bdbSChristoph Hellwig ret = spu_acquire(ctx); 1889c9101bdbSChristoph Hellwig if (ret) 1890c9101bdbSChristoph Hellwig return ret; 18918b3d6663SArnd Bergmann ctx->ops->npc_write(ctx, val); 18928b3d6663SArnd Bergmann spu_release(ctx); 1893197b1a82SChristoph Hellwig 1894197b1a82SChristoph Hellwig return 0; 189567207b96SArnd Bergmann } 189667207b96SArnd Bergmann 1897104f0cc2SMichael Ellerman static u64 spufs_npc_get(struct spu_context *ctx) 189878810ff6SMichael Ellerman { 189978810ff6SMichael Ellerman return ctx->ops->npc_read(ctx); 190078810ff6SMichael Ellerman } 1901104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_npc_ops, spufs_npc_get, spufs_npc_set, 1902104f0cc2SMichael Ellerman "0x%llx\n", SPU_ATTR_ACQUIRE); 190367207b96SArnd Bergmann 1904197b1a82SChristoph Hellwig static int spufs_decr_set(void *data, u64 val) 19058b3d6663SArnd Bergmann { 19068b3d6663SArnd Bergmann struct spu_context *ctx = data; 19078b3d6663SArnd Bergmann struct spu_lscsa *lscsa = ctx->csa.lscsa; 1908c9101bdbSChristoph Hellwig int ret; 1909c9101bdbSChristoph Hellwig 1910c9101bdbSChristoph Hellwig ret = spu_acquire_saved(ctx); 1911c9101bdbSChristoph Hellwig if (ret) 1912c9101bdbSChristoph Hellwig return ret; 19138b3d6663SArnd Bergmann lscsa->decr.slot[0] = (u32) val; 191427b1ea09SChristoph Hellwig spu_release_saved(ctx); 1915197b1a82SChristoph Hellwig 1916197b1a82SChristoph Hellwig return 0; 19178b3d6663SArnd Bergmann } 19188b3d6663SArnd Bergmann 1919104f0cc2SMichael Ellerman static u64 spufs_decr_get(struct spu_context *ctx) 19208b3d6663SArnd Bergmann { 19218b3d6663SArnd Bergmann struct spu_lscsa *lscsa = ctx->csa.lscsa; 1922bf1ab978SDwayne Grant McConnell return lscsa->decr.slot[0]; 1923bf1ab978SDwayne Grant McConnell } 1924104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_decr_ops, spufs_decr_get, spufs_decr_set, 1925104f0cc2SMichael Ellerman "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED); 19268b3d6663SArnd Bergmann 1927197b1a82SChristoph Hellwig static int spufs_decr_status_set(void *data, u64 val) 19288b3d6663SArnd Bergmann { 19298b3d6663SArnd Bergmann struct spu_context *ctx = data; 1930c9101bdbSChristoph Hellwig int ret; 1931c9101bdbSChristoph Hellwig 1932c9101bdbSChristoph Hellwig ret = spu_acquire_saved(ctx); 1933c9101bdbSChristoph Hellwig if (ret) 1934c9101bdbSChristoph Hellwig return ret; 1935d40a01d4SMasato Noguchi if (val) 1936d40a01d4SMasato Noguchi ctx->csa.priv2.mfc_control_RW |= MFC_CNTL_DECREMENTER_RUNNING; 1937d40a01d4SMasato Noguchi else 1938d40a01d4SMasato Noguchi ctx->csa.priv2.mfc_control_RW &= ~MFC_CNTL_DECREMENTER_RUNNING; 193927b1ea09SChristoph Hellwig spu_release_saved(ctx); 1940197b1a82SChristoph Hellwig 1941197b1a82SChristoph Hellwig return 0; 19428b3d6663SArnd Bergmann } 19438b3d6663SArnd Bergmann 1944104f0cc2SMichael Ellerman static u64 spufs_decr_status_get(struct spu_context *ctx) 19458b3d6663SArnd Bergmann { 1946d40a01d4SMasato Noguchi if (ctx->csa.priv2.mfc_control_RW & MFC_CNTL_DECREMENTER_RUNNING) 1947d40a01d4SMasato Noguchi return SPU_DECR_STATUS_RUNNING; 1948d40a01d4SMasato Noguchi else 1949d40a01d4SMasato Noguchi return 0; 1950bf1ab978SDwayne Grant McConnell } 1951104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_decr_status_ops, spufs_decr_status_get, 1952104f0cc2SMichael Ellerman spufs_decr_status_set, "0x%llx\n", 1953104f0cc2SMichael Ellerman SPU_ATTR_ACQUIRE_SAVED); 19548b3d6663SArnd Bergmann 1955197b1a82SChristoph Hellwig static int spufs_event_mask_set(void *data, u64 val) 19568b3d6663SArnd Bergmann { 19578b3d6663SArnd Bergmann struct spu_context *ctx = data; 19588b3d6663SArnd Bergmann struct spu_lscsa *lscsa = ctx->csa.lscsa; 1959c9101bdbSChristoph Hellwig int ret; 1960c9101bdbSChristoph Hellwig 1961c9101bdbSChristoph Hellwig ret = spu_acquire_saved(ctx); 1962c9101bdbSChristoph Hellwig if (ret) 1963c9101bdbSChristoph Hellwig return ret; 19648b3d6663SArnd Bergmann lscsa->event_mask.slot[0] = (u32) val; 196527b1ea09SChristoph Hellwig spu_release_saved(ctx); 1966197b1a82SChristoph Hellwig 1967197b1a82SChristoph Hellwig return 0; 19688b3d6663SArnd Bergmann } 19698b3d6663SArnd Bergmann 1970104f0cc2SMichael Ellerman static u64 spufs_event_mask_get(struct spu_context *ctx) 19718b3d6663SArnd Bergmann { 19728b3d6663SArnd Bergmann struct spu_lscsa *lscsa = ctx->csa.lscsa; 1973bf1ab978SDwayne Grant McConnell return lscsa->event_mask.slot[0]; 1974bf1ab978SDwayne Grant McConnell } 1975bf1ab978SDwayne Grant McConnell 1976104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_event_mask_ops, spufs_event_mask_get, 1977104f0cc2SMichael Ellerman spufs_event_mask_set, "0x%llx\n", 1978104f0cc2SMichael Ellerman SPU_ATTR_ACQUIRE_SAVED); 19798b3d6663SArnd Bergmann 1980104f0cc2SMichael Ellerman static u64 spufs_event_status_get(struct spu_context *ctx) 1981b9e3bd77SDwayne Grant McConnell { 1982b9e3bd77SDwayne Grant McConnell struct spu_state *state = &ctx->csa; 1983b9e3bd77SDwayne Grant McConnell u64 stat; 1984b9e3bd77SDwayne Grant McConnell stat = state->spu_chnlcnt_RW[0]; 1985b9e3bd77SDwayne Grant McConnell if (stat) 1986bf1ab978SDwayne Grant McConnell return state->spu_chnldata_RW[0]; 1987bf1ab978SDwayne Grant McConnell return 0; 1988bf1ab978SDwayne Grant McConnell } 1989104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_event_status_ops, spufs_event_status_get, 1990104f0cc2SMichael Ellerman NULL, "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED) 1991b9e3bd77SDwayne Grant McConnell 1992197b1a82SChristoph Hellwig static int spufs_srr0_set(void *data, u64 val) 19938b3d6663SArnd Bergmann { 19948b3d6663SArnd Bergmann struct spu_context *ctx = data; 19958b3d6663SArnd Bergmann struct spu_lscsa *lscsa = ctx->csa.lscsa; 1996c9101bdbSChristoph Hellwig int ret; 1997c9101bdbSChristoph Hellwig 1998c9101bdbSChristoph Hellwig ret = spu_acquire_saved(ctx); 1999c9101bdbSChristoph Hellwig if (ret) 2000c9101bdbSChristoph Hellwig return ret; 20018b3d6663SArnd Bergmann lscsa->srr0.slot[0] = (u32) val; 200227b1ea09SChristoph Hellwig spu_release_saved(ctx); 2003197b1a82SChristoph Hellwig 2004197b1a82SChristoph Hellwig return 0; 20058b3d6663SArnd Bergmann } 20068b3d6663SArnd Bergmann 2007104f0cc2SMichael Ellerman static u64 spufs_srr0_get(struct spu_context *ctx) 20088b3d6663SArnd Bergmann { 20098b3d6663SArnd Bergmann struct spu_lscsa *lscsa = ctx->csa.lscsa; 2010104f0cc2SMichael Ellerman return lscsa->srr0.slot[0]; 20118b3d6663SArnd Bergmann } 2012104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_srr0_ops, spufs_srr0_get, spufs_srr0_set, 2013104f0cc2SMichael Ellerman "0x%llx\n", SPU_ATTR_ACQUIRE_SAVED) 20148b3d6663SArnd Bergmann 2015104f0cc2SMichael Ellerman static u64 spufs_id_get(struct spu_context *ctx) 20167b1a7014Sarnd@arndb.de { 20177b1a7014Sarnd@arndb.de u64 num; 20187b1a7014Sarnd@arndb.de 20197b1a7014Sarnd@arndb.de if (ctx->state == SPU_STATE_RUNNABLE) 20207b1a7014Sarnd@arndb.de num = ctx->spu->number; 20217b1a7014Sarnd@arndb.de else 20227b1a7014Sarnd@arndb.de num = (unsigned int)-1; 20237b1a7014Sarnd@arndb.de 20247b1a7014Sarnd@arndb.de return num; 20257b1a7014Sarnd@arndb.de } 2026104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_id_ops, spufs_id_get, NULL, "0x%llx\n", 2027104f0cc2SMichael Ellerman SPU_ATTR_ACQUIRE) 20287b1a7014Sarnd@arndb.de 2029104f0cc2SMichael Ellerman static u64 spufs_object_id_get(struct spu_context *ctx) 2030bf1ab978SDwayne Grant McConnell { 2031bf1ab978SDwayne Grant McConnell /* FIXME: Should there really be no locking here? */ 2032104f0cc2SMichael Ellerman return ctx->object_id; 2033bf1ab978SDwayne Grant McConnell } 2034bf1ab978SDwayne Grant McConnell 2035197b1a82SChristoph Hellwig static int spufs_object_id_set(void *data, u64 id) 203686767277SArnd Bergmann { 203786767277SArnd Bergmann struct spu_context *ctx = data; 203886767277SArnd Bergmann ctx->object_id = id; 2039197b1a82SChristoph Hellwig 2040197b1a82SChristoph Hellwig return 0; 204186767277SArnd Bergmann } 204286767277SArnd Bergmann 2043104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_object_id_ops, spufs_object_id_get, 2044104f0cc2SMichael Ellerman spufs_object_id_set, "0x%llx\n", SPU_ATTR_NOACQUIRE); 204586767277SArnd Bergmann 2046104f0cc2SMichael Ellerman static u64 spufs_lslr_get(struct spu_context *ctx) 2047bf1ab978SDwayne Grant McConnell { 2048bf1ab978SDwayne Grant McConnell return ctx->csa.priv2.spu_lslr_RW; 2049bf1ab978SDwayne Grant McConnell } 2050104f0cc2SMichael Ellerman DEFINE_SPUFS_ATTRIBUTE(spufs_lslr_ops, spufs_lslr_get, NULL, "0x%llx\n", 2051104f0cc2SMichael Ellerman SPU_ATTR_ACQUIRE_SAVED); 2052b9e3bd77SDwayne Grant McConnell 2053b9e3bd77SDwayne Grant McConnell static int spufs_info_open(struct inode *inode, struct file *file) 2054b9e3bd77SDwayne Grant McConnell { 2055b9e3bd77SDwayne Grant McConnell struct spufs_inode_info *i = SPUFS_I(inode); 2056b9e3bd77SDwayne Grant McConnell struct spu_context *ctx = i->i_ctx; 2057b9e3bd77SDwayne Grant McConnell file->private_data = ctx; 2058b9e3bd77SDwayne Grant McConnell return 0; 2059b9e3bd77SDwayne Grant McConnell } 2060b9e3bd77SDwayne Grant McConnell 2061cbe709c1SBenjamin Herrenschmidt static int spufs_caps_show(struct seq_file *s, void *private) 2062cbe709c1SBenjamin Herrenschmidt { 2063cbe709c1SBenjamin Herrenschmidt struct spu_context *ctx = s->private; 2064cbe709c1SBenjamin Herrenschmidt 2065cbe709c1SBenjamin Herrenschmidt if (!(ctx->flags & SPU_CREATE_NOSCHED)) 2066cbe709c1SBenjamin Herrenschmidt seq_puts(s, "sched\n"); 2067cbe709c1SBenjamin Herrenschmidt if (!(ctx->flags & SPU_CREATE_ISOLATE)) 2068cbe709c1SBenjamin Herrenschmidt seq_puts(s, "step\n"); 2069cbe709c1SBenjamin Herrenschmidt return 0; 2070cbe709c1SBenjamin Herrenschmidt } 2071cbe709c1SBenjamin Herrenschmidt 2072cbe709c1SBenjamin Herrenschmidt static int spufs_caps_open(struct inode *inode, struct file *file) 2073cbe709c1SBenjamin Herrenschmidt { 2074cbe709c1SBenjamin Herrenschmidt return single_open(file, spufs_caps_show, SPUFS_I(inode)->i_ctx); 2075cbe709c1SBenjamin Herrenschmidt } 2076cbe709c1SBenjamin Herrenschmidt 2077cbe709c1SBenjamin Herrenschmidt static const struct file_operations spufs_caps_fops = { 2078cbe709c1SBenjamin Herrenschmidt .open = spufs_caps_open, 2079cbe709c1SBenjamin Herrenschmidt .read = seq_read, 2080cbe709c1SBenjamin Herrenschmidt .llseek = seq_lseek, 2081cbe709c1SBenjamin Herrenschmidt .release = single_release, 2082cbe709c1SBenjamin Herrenschmidt }; 2083cbe709c1SBenjamin Herrenschmidt 2084bf1ab978SDwayne Grant McConnell static ssize_t __spufs_mbox_info_read(struct spu_context *ctx, 2085bf1ab978SDwayne Grant McConnell char __user *buf, size_t len, loff_t *pos) 2086bf1ab978SDwayne Grant McConnell { 2087bf1ab978SDwayne Grant McConnell u32 data; 2088bf1ab978SDwayne Grant McConnell 2089cbea9238SJeremy Kerr /* EOF if there's no entry in the mbox */ 2090cbea9238SJeremy Kerr if (!(ctx->csa.prob.mb_stat_R & 0x0000ff)) 2091cbea9238SJeremy Kerr return 0; 2092cbea9238SJeremy Kerr 2093bf1ab978SDwayne Grant McConnell data = ctx->csa.prob.pu_mb_R; 2094bf1ab978SDwayne Grant McConnell 2095bf1ab978SDwayne Grant McConnell return simple_read_from_buffer(buf, len, pos, &data, sizeof data); 2096bf1ab978SDwayne Grant McConnell } 2097bf1ab978SDwayne Grant McConnell 209869a2f00cSDwayne Grant McConnell static ssize_t spufs_mbox_info_read(struct file *file, char __user *buf, 209969a2f00cSDwayne Grant McConnell size_t len, loff_t *pos) 210069a2f00cSDwayne Grant McConnell { 2101bf1ab978SDwayne Grant McConnell int ret; 210269a2f00cSDwayne Grant McConnell struct spu_context *ctx = file->private_data; 210369a2f00cSDwayne Grant McConnell 210469a2f00cSDwayne Grant McConnell if (!access_ok(VERIFY_WRITE, buf, len)) 210569a2f00cSDwayne Grant McConnell return -EFAULT; 210669a2f00cSDwayne Grant McConnell 2107c9101bdbSChristoph Hellwig ret = spu_acquire_saved(ctx); 2108c9101bdbSChristoph Hellwig if (ret) 2109c9101bdbSChristoph Hellwig return ret; 211069a2f00cSDwayne Grant McConnell spin_lock(&ctx->csa.register_lock); 2111bf1ab978SDwayne Grant McConnell ret = __spufs_mbox_info_read(ctx, buf, len, pos); 211269a2f00cSDwayne Grant McConnell spin_unlock(&ctx->csa.register_lock); 211327b1ea09SChristoph Hellwig spu_release_saved(ctx); 211469a2f00cSDwayne Grant McConnell 2115bf1ab978SDwayne Grant McConnell return ret; 211669a2f00cSDwayne Grant McConnell } 211769a2f00cSDwayne Grant McConnell 21185dfe4c96SArjan van de Ven static const struct file_operations spufs_mbox_info_fops = { 211969a2f00cSDwayne Grant McConnell .open = spufs_info_open, 212069a2f00cSDwayne Grant McConnell .read = spufs_mbox_info_read, 212169a2f00cSDwayne Grant McConnell .llseek = generic_file_llseek, 212269a2f00cSDwayne Grant McConnell }; 212369a2f00cSDwayne Grant McConnell 2124bf1ab978SDwayne Grant McConnell static ssize_t __spufs_ibox_info_read(struct spu_context *ctx, 2125bf1ab978SDwayne Grant McConnell char __user *buf, size_t len, loff_t *pos) 2126bf1ab978SDwayne Grant McConnell { 2127bf1ab978SDwayne Grant McConnell u32 data; 2128bf1ab978SDwayne Grant McConnell 2129cbea9238SJeremy Kerr /* EOF if there's no entry in the ibox */ 2130cbea9238SJeremy Kerr if (!(ctx->csa.prob.mb_stat_R & 0xff0000)) 2131cbea9238SJeremy Kerr return 0; 2132cbea9238SJeremy Kerr 2133bf1ab978SDwayne Grant McConnell data = ctx->csa.priv2.puint_mb_R; 2134bf1ab978SDwayne Grant McConnell 2135bf1ab978SDwayne Grant McConnell return simple_read_from_buffer(buf, len, pos, &data, sizeof data); 2136bf1ab978SDwayne Grant McConnell } 2137bf1ab978SDwayne Grant McConnell 213869a2f00cSDwayne Grant McConnell static ssize_t spufs_ibox_info_read(struct file *file, char __user *buf, 213969a2f00cSDwayne Grant McConnell size_t len, loff_t *pos) 214069a2f00cSDwayne Grant McConnell { 214169a2f00cSDwayne Grant McConnell struct spu_context *ctx = file->private_data; 2142bf1ab978SDwayne Grant McConnell int ret; 214369a2f00cSDwayne Grant McConnell 214469a2f00cSDwayne Grant McConnell if (!access_ok(VERIFY_WRITE, buf, len)) 214569a2f00cSDwayne Grant McConnell return -EFAULT; 214669a2f00cSDwayne Grant McConnell 2147c9101bdbSChristoph Hellwig ret = spu_acquire_saved(ctx); 2148c9101bdbSChristoph Hellwig if (ret) 2149c9101bdbSChristoph Hellwig return ret; 215069a2f00cSDwayne Grant McConnell spin_lock(&ctx->csa.register_lock); 2151bf1ab978SDwayne Grant McConnell ret = __spufs_ibox_info_read(ctx, buf, len, pos); 215269a2f00cSDwayne Grant McConnell spin_unlock(&ctx->csa.register_lock); 215327b1ea09SChristoph Hellwig spu_release_saved(ctx); 215469a2f00cSDwayne Grant McConnell 2155bf1ab978SDwayne Grant McConnell return ret; 215669a2f00cSDwayne Grant McConnell } 215769a2f00cSDwayne Grant McConnell 21585dfe4c96SArjan van de Ven static const struct file_operations spufs_ibox_info_fops = { 215969a2f00cSDwayne Grant McConnell .open = spufs_info_open, 216069a2f00cSDwayne Grant McConnell .read = spufs_ibox_info_read, 216169a2f00cSDwayne Grant McConnell .llseek = generic_file_llseek, 216269a2f00cSDwayne Grant McConnell }; 216369a2f00cSDwayne Grant McConnell 2164bf1ab978SDwayne Grant McConnell static ssize_t __spufs_wbox_info_read(struct spu_context *ctx, 2165bf1ab978SDwayne Grant McConnell char __user *buf, size_t len, loff_t *pos) 2166bf1ab978SDwayne Grant McConnell { 2167bf1ab978SDwayne Grant McConnell int i, cnt; 2168bf1ab978SDwayne Grant McConnell u32 data[4]; 2169bf1ab978SDwayne Grant McConnell u32 wbox_stat; 2170bf1ab978SDwayne Grant McConnell 2171bf1ab978SDwayne Grant McConnell wbox_stat = ctx->csa.prob.mb_stat_R; 2172bf1ab978SDwayne Grant McConnell cnt = 4 - ((wbox_stat & 0x00ff00) >> 8); 2173bf1ab978SDwayne Grant McConnell for (i = 0; i < cnt; i++) { 2174bf1ab978SDwayne Grant McConnell data[i] = ctx->csa.spu_mailbox_data[i]; 2175bf1ab978SDwayne Grant McConnell } 2176bf1ab978SDwayne Grant McConnell 2177bf1ab978SDwayne Grant McConnell return simple_read_from_buffer(buf, len, pos, &data, 2178bf1ab978SDwayne Grant McConnell cnt * sizeof(u32)); 2179bf1ab978SDwayne Grant McConnell } 2180bf1ab978SDwayne Grant McConnell 218169a2f00cSDwayne Grant McConnell static ssize_t spufs_wbox_info_read(struct file *file, char __user *buf, 218269a2f00cSDwayne Grant McConnell size_t len, loff_t *pos) 218369a2f00cSDwayne Grant McConnell { 218469a2f00cSDwayne Grant McConnell struct spu_context *ctx = file->private_data; 2185bf1ab978SDwayne Grant McConnell int ret; 218669a2f00cSDwayne Grant McConnell 218769a2f00cSDwayne Grant McConnell if (!access_ok(VERIFY_WRITE, buf, len)) 218869a2f00cSDwayne Grant McConnell return -EFAULT; 218969a2f00cSDwayne Grant McConnell 2190c9101bdbSChristoph Hellwig ret = spu_acquire_saved(ctx); 2191c9101bdbSChristoph Hellwig if (ret) 2192c9101bdbSChristoph Hellwig return ret; 219369a2f00cSDwayne Grant McConnell spin_lock(&ctx->csa.register_lock); 2194bf1ab978SDwayne Grant McConnell ret = __spufs_wbox_info_read(ctx, buf, len, pos); 219569a2f00cSDwayne Grant McConnell spin_unlock(&ctx->csa.register_lock); 219627b1ea09SChristoph Hellwig spu_release_saved(ctx); 219769a2f00cSDwayne Grant McConnell 2198bf1ab978SDwayne Grant McConnell return ret; 219969a2f00cSDwayne Grant McConnell } 220069a2f00cSDwayne Grant McConnell 22015dfe4c96SArjan van de Ven static const struct file_operations spufs_wbox_info_fops = { 220269a2f00cSDwayne Grant McConnell .open = spufs_info_open, 220369a2f00cSDwayne Grant McConnell .read = spufs_wbox_info_read, 220469a2f00cSDwayne Grant McConnell .llseek = generic_file_llseek, 220569a2f00cSDwayne Grant McConnell }; 220669a2f00cSDwayne Grant McConnell 2207bf1ab978SDwayne Grant McConnell static ssize_t __spufs_dma_info_read(struct spu_context *ctx, 2208bf1ab978SDwayne Grant McConnell char __user *buf, size_t len, loff_t *pos) 2209b9e3bd77SDwayne Grant McConnell { 2210b9e3bd77SDwayne Grant McConnell struct spu_dma_info info; 2211b9e3bd77SDwayne Grant McConnell struct mfc_cq_sr *qp, *spuqp; 2212b9e3bd77SDwayne Grant McConnell int i; 2213b9e3bd77SDwayne Grant McConnell 2214b9e3bd77SDwayne Grant McConnell info.dma_info_type = ctx->csa.priv2.spu_tag_status_query_RW; 2215b9e3bd77SDwayne Grant McConnell info.dma_info_mask = ctx->csa.lscsa->tag_mask.slot[0]; 2216b9e3bd77SDwayne Grant McConnell info.dma_info_status = ctx->csa.spu_chnldata_RW[24]; 2217b9e3bd77SDwayne Grant McConnell info.dma_info_stall_and_notify = ctx->csa.spu_chnldata_RW[25]; 2218b9e3bd77SDwayne Grant McConnell info.dma_info_atomic_command_status = ctx->csa.spu_chnldata_RW[27]; 2219b9e3bd77SDwayne Grant McConnell for (i = 0; i < 16; i++) { 2220b9e3bd77SDwayne Grant McConnell qp = &info.dma_info_command_data[i]; 2221b9e3bd77SDwayne Grant McConnell spuqp = &ctx->csa.priv2.spuq[i]; 2222b9e3bd77SDwayne Grant McConnell 2223b9e3bd77SDwayne Grant McConnell qp->mfc_cq_data0_RW = spuqp->mfc_cq_data0_RW; 2224b9e3bd77SDwayne Grant McConnell qp->mfc_cq_data1_RW = spuqp->mfc_cq_data1_RW; 2225b9e3bd77SDwayne Grant McConnell qp->mfc_cq_data2_RW = spuqp->mfc_cq_data2_RW; 2226b9e3bd77SDwayne Grant McConnell qp->mfc_cq_data3_RW = spuqp->mfc_cq_data3_RW; 2227b9e3bd77SDwayne Grant McConnell } 2228b9e3bd77SDwayne Grant McConnell 2229b9e3bd77SDwayne Grant McConnell return simple_read_from_buffer(buf, len, pos, &info, 2230b9e3bd77SDwayne Grant McConnell sizeof info); 2231b9e3bd77SDwayne Grant McConnell } 2232b9e3bd77SDwayne Grant McConnell 2233bf1ab978SDwayne Grant McConnell static ssize_t spufs_dma_info_read(struct file *file, char __user *buf, 2234bf1ab978SDwayne Grant McConnell size_t len, loff_t *pos) 2235bf1ab978SDwayne Grant McConnell { 2236bf1ab978SDwayne Grant McConnell struct spu_context *ctx = file->private_data; 2237bf1ab978SDwayne Grant McConnell int ret; 2238bf1ab978SDwayne Grant McConnell 2239bf1ab978SDwayne Grant McConnell if (!access_ok(VERIFY_WRITE, buf, len)) 2240bf1ab978SDwayne Grant McConnell return -EFAULT; 2241bf1ab978SDwayne Grant McConnell 2242c9101bdbSChristoph Hellwig ret = spu_acquire_saved(ctx); 2243c9101bdbSChristoph Hellwig if (ret) 2244c9101bdbSChristoph Hellwig return ret; 2245bf1ab978SDwayne Grant McConnell spin_lock(&ctx->csa.register_lock); 2246bf1ab978SDwayne Grant McConnell ret = __spufs_dma_info_read(ctx, buf, len, pos); 2247bf1ab978SDwayne Grant McConnell spin_unlock(&ctx->csa.register_lock); 224827b1ea09SChristoph Hellwig spu_release_saved(ctx); 2249bf1ab978SDwayne Grant McConnell 2250bf1ab978SDwayne Grant McConnell return ret; 2251bf1ab978SDwayne Grant McConnell } 2252bf1ab978SDwayne Grant McConnell 22535dfe4c96SArjan van de Ven static const struct file_operations spufs_dma_info_fops = { 2254b9e3bd77SDwayne Grant McConnell .open = spufs_info_open, 2255b9e3bd77SDwayne Grant McConnell .read = spufs_dma_info_read, 2256b9e3bd77SDwayne Grant McConnell }; 2257b9e3bd77SDwayne Grant McConnell 2258bf1ab978SDwayne Grant McConnell static ssize_t __spufs_proxydma_info_read(struct spu_context *ctx, 2259bf1ab978SDwayne Grant McConnell char __user *buf, size_t len, loff_t *pos) 2260b9e3bd77SDwayne Grant McConnell { 2261b9e3bd77SDwayne Grant McConnell struct spu_proxydma_info info; 2262b9e3bd77SDwayne Grant McConnell struct mfc_cq_sr *qp, *puqp; 2263bf1ab978SDwayne Grant McConnell int ret = sizeof info; 2264b9e3bd77SDwayne Grant McConnell int i; 2265b9e3bd77SDwayne Grant McConnell 2266b9e3bd77SDwayne Grant McConnell if (len < ret) 2267b9e3bd77SDwayne Grant McConnell return -EINVAL; 2268b9e3bd77SDwayne Grant McConnell 2269b9e3bd77SDwayne Grant McConnell if (!access_ok(VERIFY_WRITE, buf, len)) 2270b9e3bd77SDwayne Grant McConnell return -EFAULT; 2271b9e3bd77SDwayne Grant McConnell 2272b9e3bd77SDwayne Grant McConnell info.proxydma_info_type = ctx->csa.prob.dma_querytype_RW; 2273b9e3bd77SDwayne Grant McConnell info.proxydma_info_mask = ctx->csa.prob.dma_querymask_RW; 2274b9e3bd77SDwayne Grant McConnell info.proxydma_info_status = ctx->csa.prob.dma_tagstatus_R; 2275b9e3bd77SDwayne Grant McConnell for (i = 0; i < 8; i++) { 2276b9e3bd77SDwayne Grant McConnell qp = &info.proxydma_info_command_data[i]; 2277b9e3bd77SDwayne Grant McConnell puqp = &ctx->csa.priv2.puq[i]; 2278b9e3bd77SDwayne Grant McConnell 2279b9e3bd77SDwayne Grant McConnell qp->mfc_cq_data0_RW = puqp->mfc_cq_data0_RW; 2280b9e3bd77SDwayne Grant McConnell qp->mfc_cq_data1_RW = puqp->mfc_cq_data1_RW; 2281b9e3bd77SDwayne Grant McConnell qp->mfc_cq_data2_RW = puqp->mfc_cq_data2_RW; 2282b9e3bd77SDwayne Grant McConnell qp->mfc_cq_data3_RW = puqp->mfc_cq_data3_RW; 2283b9e3bd77SDwayne Grant McConnell } 2284bf1ab978SDwayne Grant McConnell 2285bf1ab978SDwayne Grant McConnell return simple_read_from_buffer(buf, len, pos, &info, 2286bf1ab978SDwayne Grant McConnell sizeof info); 2287bf1ab978SDwayne Grant McConnell } 2288bf1ab978SDwayne Grant McConnell 2289bf1ab978SDwayne Grant McConnell static ssize_t spufs_proxydma_info_read(struct file *file, char __user *buf, 2290bf1ab978SDwayne Grant McConnell size_t len, loff_t *pos) 2291bf1ab978SDwayne Grant McConnell { 2292bf1ab978SDwayne Grant McConnell struct spu_context *ctx = file->private_data; 2293bf1ab978SDwayne Grant McConnell int ret; 2294bf1ab978SDwayne Grant McConnell 2295c9101bdbSChristoph Hellwig ret = spu_acquire_saved(ctx); 2296c9101bdbSChristoph Hellwig if (ret) 2297c9101bdbSChristoph Hellwig return ret; 2298bf1ab978SDwayne Grant McConnell spin_lock(&ctx->csa.register_lock); 2299bf1ab978SDwayne Grant McConnell ret = __spufs_proxydma_info_read(ctx, buf, len, pos); 2300b9e3bd77SDwayne Grant McConnell spin_unlock(&ctx->csa.register_lock); 230127b1ea09SChristoph Hellwig spu_release_saved(ctx); 2302b9e3bd77SDwayne Grant McConnell 2303b9e3bd77SDwayne Grant McConnell return ret; 2304b9e3bd77SDwayne Grant McConnell } 2305b9e3bd77SDwayne Grant McConnell 23065dfe4c96SArjan van de Ven static const struct file_operations spufs_proxydma_info_fops = { 2307b9e3bd77SDwayne Grant McConnell .open = spufs_info_open, 2308b9e3bd77SDwayne Grant McConnell .read = spufs_proxydma_info_read, 2309b9e3bd77SDwayne Grant McConnell }; 2310b9e3bd77SDwayne Grant McConnell 2311476273adSChristoph Hellwig static int spufs_show_tid(struct seq_file *s, void *private) 2312476273adSChristoph Hellwig { 2313476273adSChristoph Hellwig struct spu_context *ctx = s->private; 2314476273adSChristoph Hellwig 2315476273adSChristoph Hellwig seq_printf(s, "%d\n", ctx->tid); 2316476273adSChristoph Hellwig return 0; 2317476273adSChristoph Hellwig } 2318476273adSChristoph Hellwig 2319476273adSChristoph Hellwig static int spufs_tid_open(struct inode *inode, struct file *file) 2320476273adSChristoph Hellwig { 2321476273adSChristoph Hellwig return single_open(file, spufs_show_tid, SPUFS_I(inode)->i_ctx); 2322476273adSChristoph Hellwig } 2323476273adSChristoph Hellwig 2324476273adSChristoph Hellwig static const struct file_operations spufs_tid_fops = { 2325476273adSChristoph Hellwig .open = spufs_tid_open, 2326476273adSChristoph Hellwig .read = seq_read, 2327476273adSChristoph Hellwig .llseek = seq_lseek, 2328476273adSChristoph Hellwig .release = single_release, 2329476273adSChristoph Hellwig }; 2330476273adSChristoph Hellwig 2331e9f8a0b6SChristoph Hellwig static const char *ctx_state_names[] = { 2332e9f8a0b6SChristoph Hellwig "user", "system", "iowait", "loaded" 2333e9f8a0b6SChristoph Hellwig }; 2334e9f8a0b6SChristoph Hellwig 2335e9f8a0b6SChristoph Hellwig static unsigned long long spufs_acct_time(struct spu_context *ctx, 233627ec41d3SAndre Detsch enum spu_utilization_state state) 2337e9f8a0b6SChristoph Hellwig { 233827ec41d3SAndre Detsch struct timespec ts; 233927ec41d3SAndre Detsch unsigned long long time = ctx->stats.times[state]; 2340e9f8a0b6SChristoph Hellwig 234127ec41d3SAndre Detsch /* 234227ec41d3SAndre Detsch * In general, utilization statistics are updated by the controlling 234327ec41d3SAndre Detsch * thread as the spu context moves through various well defined 234427ec41d3SAndre Detsch * state transitions, but if the context is lazily loaded its 234527ec41d3SAndre Detsch * utilization statistics are not updated as the controlling thread 234627ec41d3SAndre Detsch * is not tightly coupled with the execution of the spu context. We 234727ec41d3SAndre Detsch * calculate and apply the time delta from the last recorded state 234827ec41d3SAndre Detsch * of the spu context. 234927ec41d3SAndre Detsch */ 235027ec41d3SAndre Detsch if (ctx->spu && ctx->stats.util_state == state) { 235127ec41d3SAndre Detsch ktime_get_ts(&ts); 235227ec41d3SAndre Detsch time += timespec_to_ns(&ts) - ctx->stats.tstamp; 235327ec41d3SAndre Detsch } 2354e9f8a0b6SChristoph Hellwig 235527ec41d3SAndre Detsch return time / NSEC_PER_MSEC; 2356e9f8a0b6SChristoph Hellwig } 2357e9f8a0b6SChristoph Hellwig 2358e9f8a0b6SChristoph Hellwig static unsigned long long spufs_slb_flts(struct spu_context *ctx) 2359e9f8a0b6SChristoph Hellwig { 2360e9f8a0b6SChristoph Hellwig unsigned long long slb_flts = ctx->stats.slb_flt; 2361e9f8a0b6SChristoph Hellwig 2362e9f8a0b6SChristoph Hellwig if (ctx->state == SPU_STATE_RUNNABLE) { 2363e9f8a0b6SChristoph Hellwig slb_flts += (ctx->spu->stats.slb_flt - 2364e9f8a0b6SChristoph Hellwig ctx->stats.slb_flt_base); 2365e9f8a0b6SChristoph Hellwig } 2366e9f8a0b6SChristoph Hellwig 2367e9f8a0b6SChristoph Hellwig return slb_flts; 2368e9f8a0b6SChristoph Hellwig } 2369e9f8a0b6SChristoph Hellwig 2370e9f8a0b6SChristoph Hellwig static unsigned long long spufs_class2_intrs(struct spu_context *ctx) 2371e9f8a0b6SChristoph Hellwig { 2372e9f8a0b6SChristoph Hellwig unsigned long long class2_intrs = ctx->stats.class2_intr; 2373e9f8a0b6SChristoph Hellwig 2374e9f8a0b6SChristoph Hellwig if (ctx->state == SPU_STATE_RUNNABLE) { 2375e9f8a0b6SChristoph Hellwig class2_intrs += (ctx->spu->stats.class2_intr - 2376e9f8a0b6SChristoph Hellwig ctx->stats.class2_intr_base); 2377e9f8a0b6SChristoph Hellwig } 2378e9f8a0b6SChristoph Hellwig 2379e9f8a0b6SChristoph Hellwig return class2_intrs; 2380e9f8a0b6SChristoph Hellwig } 2381e9f8a0b6SChristoph Hellwig 2382e9f8a0b6SChristoph Hellwig 2383e9f8a0b6SChristoph Hellwig static int spufs_show_stat(struct seq_file *s, void *private) 2384e9f8a0b6SChristoph Hellwig { 2385e9f8a0b6SChristoph Hellwig struct spu_context *ctx = s->private; 2386c9101bdbSChristoph Hellwig int ret; 2387e9f8a0b6SChristoph Hellwig 2388c9101bdbSChristoph Hellwig ret = spu_acquire(ctx); 2389c9101bdbSChristoph Hellwig if (ret) 2390c9101bdbSChristoph Hellwig return ret; 2391c9101bdbSChristoph Hellwig 2392e9f8a0b6SChristoph Hellwig seq_printf(s, "%s %llu %llu %llu %llu " 2393e9f8a0b6SChristoph Hellwig "%llu %llu %llu %llu %llu %llu %llu %llu\n", 239427ec41d3SAndre Detsch ctx_state_names[ctx->stats.util_state], 239527ec41d3SAndre Detsch spufs_acct_time(ctx, SPU_UTIL_USER), 239627ec41d3SAndre Detsch spufs_acct_time(ctx, SPU_UTIL_SYSTEM), 239727ec41d3SAndre Detsch spufs_acct_time(ctx, SPU_UTIL_IOWAIT), 239827ec41d3SAndre Detsch spufs_acct_time(ctx, SPU_UTIL_IDLE_LOADED), 2399e9f8a0b6SChristoph Hellwig ctx->stats.vol_ctx_switch, 2400e9f8a0b6SChristoph Hellwig ctx->stats.invol_ctx_switch, 2401e9f8a0b6SChristoph Hellwig spufs_slb_flts(ctx), 2402e9f8a0b6SChristoph Hellwig ctx->stats.hash_flt, 2403e9f8a0b6SChristoph Hellwig ctx->stats.min_flt, 2404e9f8a0b6SChristoph Hellwig ctx->stats.maj_flt, 2405e9f8a0b6SChristoph Hellwig spufs_class2_intrs(ctx), 2406e9f8a0b6SChristoph Hellwig ctx->stats.libassist); 2407e9f8a0b6SChristoph Hellwig spu_release(ctx); 2408e9f8a0b6SChristoph Hellwig return 0; 2409e9f8a0b6SChristoph Hellwig } 2410e9f8a0b6SChristoph Hellwig 2411e9f8a0b6SChristoph Hellwig static int spufs_stat_open(struct inode *inode, struct file *file) 2412e9f8a0b6SChristoph Hellwig { 2413e9f8a0b6SChristoph Hellwig return single_open(file, spufs_show_stat, SPUFS_I(inode)->i_ctx); 2414e9f8a0b6SChristoph Hellwig } 2415e9f8a0b6SChristoph Hellwig 2416e9f8a0b6SChristoph Hellwig static const struct file_operations spufs_stat_fops = { 2417e9f8a0b6SChristoph Hellwig .open = spufs_stat_open, 2418e9f8a0b6SChristoph Hellwig .read = seq_read, 2419e9f8a0b6SChristoph Hellwig .llseek = seq_lseek, 2420e9f8a0b6SChristoph Hellwig .release = single_release, 2421e9f8a0b6SChristoph Hellwig }; 2422e9f8a0b6SChristoph Hellwig 24235158e9b5SChristoph Hellwig static inline int spufs_switch_log_used(struct spu_context *ctx) 24245158e9b5SChristoph Hellwig { 24255158e9b5SChristoph Hellwig return (ctx->switch_log->head - ctx->switch_log->tail) % 24265158e9b5SChristoph Hellwig SWITCH_LOG_BUFSIZE; 24275158e9b5SChristoph Hellwig } 24285158e9b5SChristoph Hellwig 24295158e9b5SChristoph Hellwig static inline int spufs_switch_log_avail(struct spu_context *ctx) 24305158e9b5SChristoph Hellwig { 24315158e9b5SChristoph Hellwig return SWITCH_LOG_BUFSIZE - spufs_switch_log_used(ctx); 24325158e9b5SChristoph Hellwig } 24335158e9b5SChristoph Hellwig 24345158e9b5SChristoph Hellwig static int spufs_switch_log_open(struct inode *inode, struct file *file) 24355158e9b5SChristoph Hellwig { 24365158e9b5SChristoph Hellwig struct spu_context *ctx = SPUFS_I(inode)->i_ctx; 2437f5ed0eb6SJeremy Kerr int rc; 24385158e9b5SChristoph Hellwig 2439f5ed0eb6SJeremy Kerr rc = spu_acquire(ctx); 2440f5ed0eb6SJeremy Kerr if (rc) 2441f5ed0eb6SJeremy Kerr return rc; 2442f5ed0eb6SJeremy Kerr 24435158e9b5SChristoph Hellwig if (ctx->switch_log) { 2444f5ed0eb6SJeremy Kerr rc = -EBUSY; 2445f5ed0eb6SJeremy Kerr goto out; 2446f5ed0eb6SJeremy Kerr } 2447f5ed0eb6SJeremy Kerr 2448837ef884SJeremy Kerr ctx->switch_log = kmalloc(sizeof(struct switch_log) + 24495158e9b5SChristoph Hellwig SWITCH_LOG_BUFSIZE * sizeof(struct switch_log_entry), 24505158e9b5SChristoph Hellwig GFP_KERNEL); 2451f5ed0eb6SJeremy Kerr 2452f5ed0eb6SJeremy Kerr if (!ctx->switch_log) { 2453f5ed0eb6SJeremy Kerr rc = -ENOMEM; 24545158e9b5SChristoph Hellwig goto out; 24555158e9b5SChristoph Hellwig } 2456f5ed0eb6SJeremy Kerr 2457837ef884SJeremy Kerr ctx->switch_log->head = ctx->switch_log->tail = 0; 2458f5ed0eb6SJeremy Kerr init_waitqueue_head(&ctx->switch_log->wait); 2459f5ed0eb6SJeremy Kerr rc = 0; 2460f5ed0eb6SJeremy Kerr 2461f5ed0eb6SJeremy Kerr out: 2462f5ed0eb6SJeremy Kerr spu_release(ctx); 2463f5ed0eb6SJeremy Kerr return rc; 2464f5ed0eb6SJeremy Kerr } 2465f5ed0eb6SJeremy Kerr 2466f5ed0eb6SJeremy Kerr static int spufs_switch_log_release(struct inode *inode, struct file *file) 2467f5ed0eb6SJeremy Kerr { 2468f5ed0eb6SJeremy Kerr struct spu_context *ctx = SPUFS_I(inode)->i_ctx; 2469f5ed0eb6SJeremy Kerr int rc; 2470f5ed0eb6SJeremy Kerr 2471f5ed0eb6SJeremy Kerr rc = spu_acquire(ctx); 2472f5ed0eb6SJeremy Kerr if (rc) 2473f5ed0eb6SJeremy Kerr return rc; 2474f5ed0eb6SJeremy Kerr 2475f5ed0eb6SJeremy Kerr kfree(ctx->switch_log); 2476f5ed0eb6SJeremy Kerr ctx->switch_log = NULL; 2477f5ed0eb6SJeremy Kerr spu_release(ctx); 24785158e9b5SChristoph Hellwig 24795158e9b5SChristoph Hellwig return 0; 24805158e9b5SChristoph Hellwig } 24815158e9b5SChristoph Hellwig 24825158e9b5SChristoph Hellwig static int switch_log_sprint(struct spu_context *ctx, char *tbuf, int n) 24835158e9b5SChristoph Hellwig { 24845158e9b5SChristoph Hellwig struct switch_log_entry *p; 24855158e9b5SChristoph Hellwig 24865158e9b5SChristoph Hellwig p = ctx->switch_log->log + ctx->switch_log->tail % SWITCH_LOG_BUFSIZE; 24875158e9b5SChristoph Hellwig 24885158e9b5SChristoph Hellwig return snprintf(tbuf, n, "%u.%09u %d %u %u %llu\n", 24895158e9b5SChristoph Hellwig (unsigned int) p->tstamp.tv_sec, 24905158e9b5SChristoph Hellwig (unsigned int) p->tstamp.tv_nsec, 24915158e9b5SChristoph Hellwig p->spu_id, 24925158e9b5SChristoph Hellwig (unsigned int) p->type, 24935158e9b5SChristoph Hellwig (unsigned int) p->val, 24945158e9b5SChristoph Hellwig (unsigned long long) p->timebase); 24955158e9b5SChristoph Hellwig } 24965158e9b5SChristoph Hellwig 24975158e9b5SChristoph Hellwig static ssize_t spufs_switch_log_read(struct file *file, char __user *buf, 24985158e9b5SChristoph Hellwig size_t len, loff_t *ppos) 24995158e9b5SChristoph Hellwig { 25005158e9b5SChristoph Hellwig struct inode *inode = file->f_path.dentry->d_inode; 25015158e9b5SChristoph Hellwig struct spu_context *ctx = SPUFS_I(inode)->i_ctx; 25025158e9b5SChristoph Hellwig int error = 0, cnt = 0; 25035158e9b5SChristoph Hellwig 25045158e9b5SChristoph Hellwig if (!buf || len < 0) 25055158e9b5SChristoph Hellwig return -EINVAL; 25065158e9b5SChristoph Hellwig 2507f5ed0eb6SJeremy Kerr error = spu_acquire(ctx); 2508f5ed0eb6SJeremy Kerr if (error) 2509f5ed0eb6SJeremy Kerr return error; 2510f5ed0eb6SJeremy Kerr 25115158e9b5SChristoph Hellwig while (cnt < len) { 25125158e9b5SChristoph Hellwig char tbuf[128]; 25135158e9b5SChristoph Hellwig int width; 25145158e9b5SChristoph Hellwig 2515f5ed0eb6SJeremy Kerr if (spufs_switch_log_used(ctx) == 0) { 251614f693eeSJeremy Kerr if (cnt > 0) { 251714f693eeSJeremy Kerr /* If there's data ready to go, we can 251814f693eeSJeremy Kerr * just return straight away */ 251914f693eeSJeremy Kerr break; 252014f693eeSJeremy Kerr 252114f693eeSJeremy Kerr } else if (file->f_flags & O_NONBLOCK) { 2522f5ed0eb6SJeremy Kerr error = -EAGAIN; 25235158e9b5SChristoph Hellwig break; 252414f693eeSJeremy Kerr 2525f5ed0eb6SJeremy Kerr } else { 252614f693eeSJeremy Kerr /* spufs_wait will drop the mutex and 252714f693eeSJeremy Kerr * re-acquire, but since we're in read(), the 252814f693eeSJeremy Kerr * file cannot be _released (and so 252914f693eeSJeremy Kerr * ctx->switch_log is stable). 2530f5ed0eb6SJeremy Kerr */ 2531f5ed0eb6SJeremy Kerr error = spufs_wait(ctx->switch_log->wait, 2532f5ed0eb6SJeremy Kerr spufs_switch_log_used(ctx) > 0); 25335158e9b5SChristoph Hellwig 2534f5ed0eb6SJeremy Kerr /* On error, spufs_wait returns without the 2535f5ed0eb6SJeremy Kerr * state mutex held */ 2536f5ed0eb6SJeremy Kerr if (error) 2537f5ed0eb6SJeremy Kerr return error; 25385158e9b5SChristoph Hellwig 253914f693eeSJeremy Kerr /* We may have had entries read from underneath 254014f693eeSJeremy Kerr * us while we dropped the mutex in spufs_wait, 254114f693eeSJeremy Kerr * so re-check */ 254214f693eeSJeremy Kerr if (spufs_switch_log_used(ctx) == 0) 2543f5ed0eb6SJeremy Kerr continue; 254414f693eeSJeremy Kerr } 254514f693eeSJeremy Kerr } 2546f5ed0eb6SJeremy Kerr 25475158e9b5SChristoph Hellwig width = switch_log_sprint(ctx, tbuf, sizeof(tbuf)); 2548f5ed0eb6SJeremy Kerr if (width < len) 25495158e9b5SChristoph Hellwig ctx->switch_log->tail = 25505158e9b5SChristoph Hellwig (ctx->switch_log->tail + 1) % 25515158e9b5SChristoph Hellwig SWITCH_LOG_BUFSIZE; 2552f5ed0eb6SJeremy Kerr else 2553f5ed0eb6SJeremy Kerr /* If the record is greater than space available return 2554f5ed0eb6SJeremy Kerr * partial buffer (so far) */ 25555158e9b5SChristoph Hellwig break; 25565158e9b5SChristoph Hellwig 25575158e9b5SChristoph Hellwig error = copy_to_user(buf + cnt, tbuf, width); 25585158e9b5SChristoph Hellwig if (error) 25595158e9b5SChristoph Hellwig break; 25605158e9b5SChristoph Hellwig cnt += width; 25615158e9b5SChristoph Hellwig } 25625158e9b5SChristoph Hellwig 2563f5ed0eb6SJeremy Kerr spu_release(ctx); 2564f5ed0eb6SJeremy Kerr 25655158e9b5SChristoph Hellwig return cnt == 0 ? error : cnt; 25665158e9b5SChristoph Hellwig } 25675158e9b5SChristoph Hellwig 25685158e9b5SChristoph Hellwig static unsigned int spufs_switch_log_poll(struct file *file, poll_table *wait) 25695158e9b5SChristoph Hellwig { 25705158e9b5SChristoph Hellwig struct inode *inode = file->f_path.dentry->d_inode; 25715158e9b5SChristoph Hellwig struct spu_context *ctx = SPUFS_I(inode)->i_ctx; 25725158e9b5SChristoph Hellwig unsigned int mask = 0; 2573f5ed0eb6SJeremy Kerr int rc; 25745158e9b5SChristoph Hellwig 25755158e9b5SChristoph Hellwig poll_wait(file, &ctx->switch_log->wait, wait); 25765158e9b5SChristoph Hellwig 2577f5ed0eb6SJeremy Kerr rc = spu_acquire(ctx); 2578f5ed0eb6SJeremy Kerr if (rc) 2579f5ed0eb6SJeremy Kerr return rc; 2580f5ed0eb6SJeremy Kerr 25815158e9b5SChristoph Hellwig if (spufs_switch_log_used(ctx) > 0) 25825158e9b5SChristoph Hellwig mask |= POLLIN; 25835158e9b5SChristoph Hellwig 2584f5ed0eb6SJeremy Kerr spu_release(ctx); 2585f5ed0eb6SJeremy Kerr 25865158e9b5SChristoph Hellwig return mask; 25875158e9b5SChristoph Hellwig } 25885158e9b5SChristoph Hellwig 25895158e9b5SChristoph Hellwig static const struct file_operations spufs_switch_log_fops = { 25905158e9b5SChristoph Hellwig .owner = THIS_MODULE, 25915158e9b5SChristoph Hellwig .open = spufs_switch_log_open, 25925158e9b5SChristoph Hellwig .read = spufs_switch_log_read, 25935158e9b5SChristoph Hellwig .poll = spufs_switch_log_poll, 2594f5ed0eb6SJeremy Kerr .release = spufs_switch_log_release, 25955158e9b5SChristoph Hellwig }; 25965158e9b5SChristoph Hellwig 2597f5ed0eb6SJeremy Kerr /** 2598f5ed0eb6SJeremy Kerr * Log a context switch event to a switch log reader. 2599f5ed0eb6SJeremy Kerr * 2600f5ed0eb6SJeremy Kerr * Must be called with ctx->state_mutex held. 2601f5ed0eb6SJeremy Kerr */ 26025158e9b5SChristoph Hellwig void spu_switch_log_notify(struct spu *spu, struct spu_context *ctx, 26035158e9b5SChristoph Hellwig u32 type, u32 val) 26045158e9b5SChristoph Hellwig { 26055158e9b5SChristoph Hellwig if (!ctx->switch_log) 26065158e9b5SChristoph Hellwig return; 26075158e9b5SChristoph Hellwig 26085158e9b5SChristoph Hellwig if (spufs_switch_log_avail(ctx) > 1) { 26095158e9b5SChristoph Hellwig struct switch_log_entry *p; 26105158e9b5SChristoph Hellwig 26115158e9b5SChristoph Hellwig p = ctx->switch_log->log + ctx->switch_log->head; 26125158e9b5SChristoph Hellwig ktime_get_ts(&p->tstamp); 26135158e9b5SChristoph Hellwig p->timebase = get_tb(); 26145158e9b5SChristoph Hellwig p->spu_id = spu ? spu->number : -1; 26155158e9b5SChristoph Hellwig p->type = type; 26165158e9b5SChristoph Hellwig p->val = val; 26175158e9b5SChristoph Hellwig 26185158e9b5SChristoph Hellwig ctx->switch_log->head = 26195158e9b5SChristoph Hellwig (ctx->switch_log->head + 1) % SWITCH_LOG_BUFSIZE; 26205158e9b5SChristoph Hellwig } 26215158e9b5SChristoph Hellwig 26225158e9b5SChristoph Hellwig wake_up(&ctx->switch_log->wait); 26235158e9b5SChristoph Hellwig } 2624e9f8a0b6SChristoph Hellwig 262546deed69SLuke Browning static int spufs_show_ctx(struct seq_file *s, void *private) 262646deed69SLuke Browning { 262746deed69SLuke Browning struct spu_context *ctx = s->private; 262846deed69SLuke Browning u64 mfc_control_RW; 262946deed69SLuke Browning 263046deed69SLuke Browning mutex_lock(&ctx->state_mutex); 263146deed69SLuke Browning if (ctx->spu) { 263246deed69SLuke Browning struct spu *spu = ctx->spu; 263346deed69SLuke Browning struct spu_priv2 __iomem *priv2 = spu->priv2; 263446deed69SLuke Browning 263546deed69SLuke Browning spin_lock_irq(&spu->register_lock); 263646deed69SLuke Browning mfc_control_RW = in_be64(&priv2->mfc_control_RW); 263746deed69SLuke Browning spin_unlock_irq(&spu->register_lock); 263846deed69SLuke Browning } else { 263946deed69SLuke Browning struct spu_state *csa = &ctx->csa; 264046deed69SLuke Browning 264146deed69SLuke Browning mfc_control_RW = csa->priv2.mfc_control_RW; 264246deed69SLuke Browning } 264346deed69SLuke Browning 264446deed69SLuke Browning seq_printf(s, "%c flgs(%lx) sflgs(%lx) pri(%d) ts(%d) spu(%02d)" 264546deed69SLuke Browning " %c %lx %lx %lx %lx %x %x\n", 264646deed69SLuke Browning ctx->state == SPU_STATE_SAVED ? 'S' : 'R', 264746deed69SLuke Browning ctx->flags, 264846deed69SLuke Browning ctx->sched_flags, 264946deed69SLuke Browning ctx->prio, 265046deed69SLuke Browning ctx->time_slice, 265146deed69SLuke Browning ctx->spu ? ctx->spu->number : -1, 265246deed69SLuke Browning !list_empty(&ctx->rq) ? 'q' : ' ', 265346deed69SLuke Browning ctx->csa.class_0_pending, 265446deed69SLuke Browning ctx->csa.class_0_dar, 265546deed69SLuke Browning ctx->csa.class_1_dsisr, 265646deed69SLuke Browning mfc_control_RW, 265746deed69SLuke Browning ctx->ops->runcntl_read(ctx), 265846deed69SLuke Browning ctx->ops->status_read(ctx)); 265946deed69SLuke Browning 266046deed69SLuke Browning mutex_unlock(&ctx->state_mutex); 266146deed69SLuke Browning 266246deed69SLuke Browning return 0; 266346deed69SLuke Browning } 266446deed69SLuke Browning 266546deed69SLuke Browning static int spufs_ctx_open(struct inode *inode, struct file *file) 266646deed69SLuke Browning { 266746deed69SLuke Browning return single_open(file, spufs_show_ctx, SPUFS_I(inode)->i_ctx); 266846deed69SLuke Browning } 266946deed69SLuke Browning 267046deed69SLuke Browning static const struct file_operations spufs_ctx_fops = { 267146deed69SLuke Browning .open = spufs_ctx_open, 267246deed69SLuke Browning .read = seq_read, 267346deed69SLuke Browning .llseek = seq_lseek, 267446deed69SLuke Browning .release = single_release, 267546deed69SLuke Browning }; 267646deed69SLuke Browning 267723d893f5SJeremy Kerr struct spufs_tree_descr spufs_dir_contents[] = { 2678cbe709c1SBenjamin Herrenschmidt { "capabilities", &spufs_caps_fops, 0444, }, 26796f7dde81SJeremy Kerr { "mem", &spufs_mem_fops, 0666, LS_SIZE, }, 26806f7dde81SJeremy Kerr { "regs", &spufs_regs_fops, 0666, sizeof(struct spu_reg128[128]), }, 268167207b96SArnd Bergmann { "mbox", &spufs_mbox_fops, 0444, }, 268267207b96SArnd Bergmann { "ibox", &spufs_ibox_fops, 0444, }, 268367207b96SArnd Bergmann { "wbox", &spufs_wbox_fops, 0222, }, 26846f7dde81SJeremy Kerr { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), }, 26856f7dde81SJeremy Kerr { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), }, 26866f7dde81SJeremy Kerr { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), }, 2687603c4612SJeremy Kerr { "signal1", &spufs_signal1_fops, 0666, }, 2688603c4612SJeremy Kerr { "signal2", &spufs_signal2_fops, 0666, }, 268967207b96SArnd Bergmann { "signal1_type", &spufs_signal1_type, 0666, }, 269067207b96SArnd Bergmann { "signal2_type", &spufs_signal2_type, 0666, }, 26916df10a82SMark Nutter { "cntl", &spufs_cntl_fops, 0666, }, 26926f7dde81SJeremy Kerr { "fpcr", &spufs_fpcr_fops, 0666, sizeof(struct spu_reg128), }, 2693b9e3bd77SDwayne Grant McConnell { "lslr", &spufs_lslr_ops, 0444, }, 2694b9e3bd77SDwayne Grant McConnell { "mfc", &spufs_mfc_fops, 0666, }, 2695b9e3bd77SDwayne Grant McConnell { "mss", &spufs_mss_fops, 0666, }, 2696b9e3bd77SDwayne Grant McConnell { "npc", &spufs_npc_ops, 0666, }, 2697b9e3bd77SDwayne Grant McConnell { "srr0", &spufs_srr0_ops, 0666, }, 26988b3d6663SArnd Bergmann { "decr", &spufs_decr_ops, 0666, }, 26998b3d6663SArnd Bergmann { "decr_status", &spufs_decr_status_ops, 0666, }, 27008b3d6663SArnd Bergmann { "event_mask", &spufs_event_mask_ops, 0666, }, 2701b9e3bd77SDwayne Grant McConnell { "event_status", &spufs_event_status_ops, 0444, }, 27026f7dde81SJeremy Kerr { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, }, 270386767277SArnd Bergmann { "phys-id", &spufs_id_ops, 0666, }, 270486767277SArnd Bergmann { "object-id", &spufs_object_id_ops, 0666, }, 27056f7dde81SJeremy Kerr { "mbox_info", &spufs_mbox_info_fops, 0444, sizeof(u32), }, 27066f7dde81SJeremy Kerr { "ibox_info", &spufs_ibox_info_fops, 0444, sizeof(u32), }, 27076f7dde81SJeremy Kerr { "wbox_info", &spufs_wbox_info_fops, 0444, sizeof(u32), }, 27086f7dde81SJeremy Kerr { "dma_info", &spufs_dma_info_fops, 0444, 27096f7dde81SJeremy Kerr sizeof(struct spu_dma_info), }, 27106f7dde81SJeremy Kerr { "proxydma_info", &spufs_proxydma_info_fops, 0444, 27116f7dde81SJeremy Kerr sizeof(struct spu_proxydma_info)}, 2712476273adSChristoph Hellwig { "tid", &spufs_tid_fops, 0444, }, 2713e9f8a0b6SChristoph Hellwig { "stat", &spufs_stat_fops, 0444, }, 27145158e9b5SChristoph Hellwig { "switch_log", &spufs_switch_log_fops, 0444 }, 271567207b96SArnd Bergmann {}, 271667207b96SArnd Bergmann }; 27175737edd1SMark Nutter 271823d893f5SJeremy Kerr struct spufs_tree_descr spufs_dir_nosched_contents[] = { 2719cbe709c1SBenjamin Herrenschmidt { "capabilities", &spufs_caps_fops, 0444, }, 27206f7dde81SJeremy Kerr { "mem", &spufs_mem_fops, 0666, LS_SIZE, }, 27215737edd1SMark Nutter { "mbox", &spufs_mbox_fops, 0444, }, 27225737edd1SMark Nutter { "ibox", &spufs_ibox_fops, 0444, }, 27235737edd1SMark Nutter { "wbox", &spufs_wbox_fops, 0222, }, 27246f7dde81SJeremy Kerr { "mbox_stat", &spufs_mbox_stat_fops, 0444, sizeof(u32), }, 27256f7dde81SJeremy Kerr { "ibox_stat", &spufs_ibox_stat_fops, 0444, sizeof(u32), }, 27266f7dde81SJeremy Kerr { "wbox_stat", &spufs_wbox_stat_fops, 0444, sizeof(u32), }, 2727d054b36fSJeremy Kerr { "signal1", &spufs_signal1_nosched_fops, 0222, }, 2728d054b36fSJeremy Kerr { "signal2", &spufs_signal2_nosched_fops, 0222, }, 27295737edd1SMark Nutter { "signal1_type", &spufs_signal1_type, 0666, }, 27305737edd1SMark Nutter { "signal2_type", &spufs_signal2_type, 0666, }, 27315737edd1SMark Nutter { "mss", &spufs_mss_fops, 0666, }, 27325737edd1SMark Nutter { "mfc", &spufs_mfc_fops, 0666, }, 27335737edd1SMark Nutter { "cntl", &spufs_cntl_fops, 0666, }, 27345737edd1SMark Nutter { "npc", &spufs_npc_ops, 0666, }, 27356f7dde81SJeremy Kerr { "psmap", &spufs_psmap_fops, 0666, SPUFS_PS_MAP_SIZE, }, 27365737edd1SMark Nutter { "phys-id", &spufs_id_ops, 0666, }, 27375737edd1SMark Nutter { "object-id", &spufs_object_id_ops, 0666, }, 2738476273adSChristoph Hellwig { "tid", &spufs_tid_fops, 0444, }, 2739e9f8a0b6SChristoph Hellwig { "stat", &spufs_stat_fops, 0444, }, 27402c3e4787SJeremy Kerr {}, 27412c3e4787SJeremy Kerr }; 27422c3e4787SJeremy Kerr 27432c3e4787SJeremy Kerr struct spufs_tree_descr spufs_dir_debug_contents[] = { 274446deed69SLuke Browning { ".ctx", &spufs_ctx_fops, 0444, }, 27455737edd1SMark Nutter {}, 27465737edd1SMark Nutter }; 2747bf1ab978SDwayne Grant McConnell 2748bf1ab978SDwayne Grant McConnell struct spufs_coredump_reader spufs_coredump_read[] = { 27494fca9c42SMichael Ellerman { "regs", __spufs_regs_read, NULL, sizeof(struct spu_reg128[128])}, 27504fca9c42SMichael Ellerman { "fpcr", __spufs_fpcr_read, NULL, sizeof(struct spu_reg128) }, 2751104f0cc2SMichael Ellerman { "lslr", NULL, spufs_lslr_get, 19 }, 2752104f0cc2SMichael Ellerman { "decr", NULL, spufs_decr_get, 19 }, 2753104f0cc2SMichael Ellerman { "decr_status", NULL, spufs_decr_status_get, 19 }, 27544fca9c42SMichael Ellerman { "mem", __spufs_mem_read, NULL, LS_SIZE, }, 27554fca9c42SMichael Ellerman { "signal1", __spufs_signal1_read, NULL, sizeof(u32) }, 2756104f0cc2SMichael Ellerman { "signal1_type", NULL, spufs_signal1_type_get, 19 }, 27574fca9c42SMichael Ellerman { "signal2", __spufs_signal2_read, NULL, sizeof(u32) }, 2758104f0cc2SMichael Ellerman { "signal2_type", NULL, spufs_signal2_type_get, 19 }, 2759104f0cc2SMichael Ellerman { "event_mask", NULL, spufs_event_mask_get, 19 }, 2760104f0cc2SMichael Ellerman { "event_status", NULL, spufs_event_status_get, 19 }, 27614fca9c42SMichael Ellerman { "mbox_info", __spufs_mbox_info_read, NULL, sizeof(u32) }, 27624fca9c42SMichael Ellerman { "ibox_info", __spufs_ibox_info_read, NULL, sizeof(u32) }, 27634fca9c42SMichael Ellerman { "wbox_info", __spufs_wbox_info_read, NULL, 4 * sizeof(u32)}, 27644fca9c42SMichael Ellerman { "dma_info", __spufs_dma_info_read, NULL, sizeof(struct spu_dma_info)}, 27654fca9c42SMichael Ellerman { "proxydma_info", __spufs_proxydma_info_read, 27664fca9c42SMichael Ellerman NULL, sizeof(struct spu_proxydma_info)}, 2767104f0cc2SMichael Ellerman { "object-id", NULL, spufs_object_id_get, 19 }, 2768104f0cc2SMichael Ellerman { "npc", NULL, spufs_npc_get, 19 }, 2769936d5bf1SMichael Ellerman { NULL }, 2770bf1ab978SDwayne Grant McConnell }; 2771