10afacde3Sarnd@arndb.de #define DEBUG 20afacde3Sarnd@arndb.de 3ce8ab854SArnd Bergmann #include <linux/wait.h> 4ce8ab854SArnd Bergmann #include <linux/ptrace.h> 5ce8ab854SArnd Bergmann 6ce8ab854SArnd Bergmann #include <asm/spu.h> 7c6730ed4SJeremy Kerr #include <asm/spu_priv1.h> 8c6730ed4SJeremy Kerr #include <asm/io.h> 9cfff5b23SDave Jones #include <asm/unistd.h> 10ce8ab854SArnd Bergmann 11ce8ab854SArnd Bergmann #include "spufs.h" 12ce8ab854SArnd Bergmann 13ce8ab854SArnd Bergmann /* interrupt-level stop callback function. */ 14ce8ab854SArnd Bergmann void spufs_stop_callback(struct spu *spu) 15ce8ab854SArnd Bergmann { 16ce8ab854SArnd Bergmann struct spu_context *ctx = spu->ctx; 17ce8ab854SArnd Bergmann 18ce8ab854SArnd Bergmann wake_up_all(&ctx->stop_wq); 19ce8ab854SArnd Bergmann } 20ce8ab854SArnd Bergmann 21ce8ab854SArnd Bergmann static inline int spu_stopped(struct spu_context *ctx, u32 * stat) 22ce8ab854SArnd Bergmann { 23ce8ab854SArnd Bergmann struct spu *spu; 24ce8ab854SArnd Bergmann u64 pte_fault; 25ce8ab854SArnd Bergmann 26ce8ab854SArnd Bergmann *stat = ctx->ops->status_read(ctx); 27ce8ab854SArnd Bergmann if (ctx->state != SPU_STATE_RUNNABLE) 28ce8ab854SArnd Bergmann return 1; 29ce8ab854SArnd Bergmann spu = ctx->spu; 30ce8ab854SArnd Bergmann pte_fault = spu->dsisr & 31ce8ab854SArnd Bergmann (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED); 32ce8ab854SArnd Bergmann return (!(*stat & 0x1) || pte_fault || spu->class_0_pending) ? 1 : 0; 33ce8ab854SArnd Bergmann } 34ce8ab854SArnd Bergmann 35c6730ed4SJeremy Kerr static int spu_setup_isolated(struct spu_context *ctx) 36c6730ed4SJeremy Kerr { 37c6730ed4SJeremy Kerr int ret; 38c6730ed4SJeremy Kerr u64 __iomem *mfc_cntl; 39c6730ed4SJeremy Kerr u64 sr1; 40c6730ed4SJeremy Kerr u32 status; 41c6730ed4SJeremy Kerr unsigned long timeout; 42c6730ed4SJeremy Kerr const u32 status_loading = SPU_STATUS_RUNNING 43c6730ed4SJeremy Kerr | SPU_STATUS_ISOLATED_STATE | SPU_STATUS_ISOLATED_LOAD_STATUS; 44c6730ed4SJeremy Kerr 457ec18ab9SChristoph Hellwig ret = -ENODEV; 46c6730ed4SJeremy Kerr if (!isolated_loader) 47c6730ed4SJeremy Kerr goto out; 48c6730ed4SJeremy Kerr 497ec18ab9SChristoph Hellwig /* 507ec18ab9SChristoph Hellwig * We need to exclude userspace access to the context. 517ec18ab9SChristoph Hellwig * 527ec18ab9SChristoph Hellwig * To protect against memory access we invalidate all ptes 537ec18ab9SChristoph Hellwig * and make sure the pagefault handlers block on the mutex. 547ec18ab9SChristoph Hellwig */ 557ec18ab9SChristoph Hellwig spu_unmap_mappings(ctx); 567ec18ab9SChristoph Hellwig 57c6730ed4SJeremy Kerr mfc_cntl = &ctx->spu->priv2->mfc_control_RW; 58c6730ed4SJeremy Kerr 59c6730ed4SJeremy Kerr /* purge the MFC DMA queue to ensure no spurious accesses before we 60c6730ed4SJeremy Kerr * enter kernel mode */ 61c6730ed4SJeremy Kerr timeout = jiffies + HZ; 62c6730ed4SJeremy Kerr out_be64(mfc_cntl, MFC_CNTL_PURGE_DMA_REQUEST); 63c6730ed4SJeremy Kerr while ((in_be64(mfc_cntl) & MFC_CNTL_PURGE_DMA_STATUS_MASK) 64c6730ed4SJeremy Kerr != MFC_CNTL_PURGE_DMA_COMPLETE) { 65c6730ed4SJeremy Kerr if (time_after(jiffies, timeout)) { 66c6730ed4SJeremy Kerr printk(KERN_ERR "%s: timeout flushing MFC DMA queue\n", 67c6730ed4SJeremy Kerr __FUNCTION__); 68c6730ed4SJeremy Kerr ret = -EIO; 697ec18ab9SChristoph Hellwig goto out; 70c6730ed4SJeremy Kerr } 71c6730ed4SJeremy Kerr cond_resched(); 72c6730ed4SJeremy Kerr } 73c6730ed4SJeremy Kerr 74c6730ed4SJeremy Kerr /* put the SPE in kernel mode to allow access to the loader */ 75c6730ed4SJeremy Kerr sr1 = spu_mfc_sr1_get(ctx->spu); 76c6730ed4SJeremy Kerr sr1 &= ~MFC_STATE1_PROBLEM_STATE_MASK; 77c6730ed4SJeremy Kerr spu_mfc_sr1_set(ctx->spu, sr1); 78c6730ed4SJeremy Kerr 79c6730ed4SJeremy Kerr /* start the loader */ 80c6730ed4SJeremy Kerr ctx->ops->signal1_write(ctx, (unsigned long)isolated_loader >> 32); 81c6730ed4SJeremy Kerr ctx->ops->signal2_write(ctx, 82c6730ed4SJeremy Kerr (unsigned long)isolated_loader & 0xffffffff); 83c6730ed4SJeremy Kerr 84c6730ed4SJeremy Kerr ctx->ops->runcntl_write(ctx, 85c6730ed4SJeremy Kerr SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE); 86c6730ed4SJeremy Kerr 87c6730ed4SJeremy Kerr ret = 0; 88c6730ed4SJeremy Kerr timeout = jiffies + HZ; 89c6730ed4SJeremy Kerr while (((status = ctx->ops->status_read(ctx)) & status_loading) == 90c6730ed4SJeremy Kerr status_loading) { 91c6730ed4SJeremy Kerr if (time_after(jiffies, timeout)) { 92c6730ed4SJeremy Kerr printk(KERN_ERR "%s: timeout waiting for loader\n", 93c6730ed4SJeremy Kerr __FUNCTION__); 94c6730ed4SJeremy Kerr ret = -EIO; 95c6730ed4SJeremy Kerr goto out_drop_priv; 96c6730ed4SJeremy Kerr } 97c6730ed4SJeremy Kerr cond_resched(); 98c6730ed4SJeremy Kerr } 99c6730ed4SJeremy Kerr 100c6730ed4SJeremy Kerr if (!(status & SPU_STATUS_RUNNING)) { 101c6730ed4SJeremy Kerr /* If isolated LOAD has failed: run SPU, we will get a stop-and 102c6730ed4SJeremy Kerr * signal later. */ 103c6730ed4SJeremy Kerr pr_debug("%s: isolated LOAD failed\n", __FUNCTION__); 104c6730ed4SJeremy Kerr ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE); 105c6730ed4SJeremy Kerr ret = -EACCES; 1067ec18ab9SChristoph Hellwig goto out_drop_priv; 1077ec18ab9SChristoph Hellwig } 108c6730ed4SJeremy Kerr 1097ec18ab9SChristoph Hellwig if (!(status & SPU_STATUS_ISOLATED_STATE)) { 110c6730ed4SJeremy Kerr /* This isn't allowed by the CBEA, but check anyway */ 111c6730ed4SJeremy Kerr pr_debug("%s: SPU fell out of isolated mode?\n", __FUNCTION__); 112c6730ed4SJeremy Kerr ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_STOP); 113c6730ed4SJeremy Kerr ret = -EINVAL; 1147ec18ab9SChristoph Hellwig goto out_drop_priv; 115c6730ed4SJeremy Kerr } 116c6730ed4SJeremy Kerr 117c6730ed4SJeremy Kerr out_drop_priv: 118c6730ed4SJeremy Kerr /* Finished accessing the loader. Drop kernel mode */ 119c6730ed4SJeremy Kerr sr1 |= MFC_STATE1_PROBLEM_STATE_MASK; 120c6730ed4SJeremy Kerr spu_mfc_sr1_set(ctx->spu, sr1); 121c6730ed4SJeremy Kerr 122c6730ed4SJeremy Kerr out: 123c6730ed4SJeremy Kerr return ret; 124c6730ed4SJeremy Kerr } 125c6730ed4SJeremy Kerr 1269add11daSArnd Bergmann static inline int spu_run_init(struct spu_context *ctx, u32 * npc) 127ce8ab854SArnd Bergmann { 128ce8ab854SArnd Bergmann int ret; 1295737edd1SMark Nutter unsigned long runcntl = SPU_RUNCNTL_RUNNABLE; 130ce8ab854SArnd Bergmann 13150b520d4SChristoph Hellwig ret = spu_acquire_runnable(ctx, 0); 132c6730ed4SJeremy Kerr if (ret) 133ce8ab854SArnd Bergmann return ret; 1345737edd1SMark Nutter 135c6730ed4SJeremy Kerr if (ctx->flags & SPU_CREATE_ISOLATE) { 136c6730ed4SJeremy Kerr if (!(ctx->ops->status_read(ctx) & SPU_STATUS_ISOLATED_STATE)) { 137c6730ed4SJeremy Kerr ret = spu_setup_isolated(ctx); 1387ec18ab9SChristoph Hellwig if (ret) 1397ec18ab9SChristoph Hellwig spu_release(ctx); 1400afacde3Sarnd@arndb.de } 141c6730ed4SJeremy Kerr 142c6730ed4SJeremy Kerr /* if userspace has set the runcntrl register (eg, to issue an 143c6730ed4SJeremy Kerr * isolated exit), we need to re-set it here */ 144c6730ed4SJeremy Kerr runcntl = ctx->ops->runcntl_read(ctx) & 145c6730ed4SJeremy Kerr (SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE); 146c6730ed4SJeremy Kerr if (runcntl == 0) 147c6730ed4SJeremy Kerr runcntl = SPU_RUNCNTL_RUNNABLE; 1482eb1b120SChristoph Hellwig } else { 1492eb1b120SChristoph Hellwig spu_start_tick(ctx); 150c6730ed4SJeremy Kerr ctx->ops->npc_write(ctx, *npc); 1512eb1b120SChristoph Hellwig } 152c6730ed4SJeremy Kerr 153c6730ed4SJeremy Kerr ctx->ops->runcntl_write(ctx, runcntl); 154c6730ed4SJeremy Kerr return ret; 155ce8ab854SArnd Bergmann } 156ce8ab854SArnd Bergmann 157ce8ab854SArnd Bergmann static inline int spu_run_fini(struct spu_context *ctx, u32 * npc, 158ce8ab854SArnd Bergmann u32 * status) 159ce8ab854SArnd Bergmann { 160ce8ab854SArnd Bergmann int ret = 0; 161ce8ab854SArnd Bergmann 1622eb1b120SChristoph Hellwig spu_stop_tick(ctx); 163ce8ab854SArnd Bergmann *status = ctx->ops->status_read(ctx); 164ce8ab854SArnd Bergmann *npc = ctx->ops->npc_read(ctx); 165ce8ab854SArnd Bergmann spu_release(ctx); 166ce8ab854SArnd Bergmann 167ce8ab854SArnd Bergmann if (signal_pending(current)) 168ce8ab854SArnd Bergmann ret = -ERESTARTSYS; 1692ebb2477SMasato Noguchi 170ce8ab854SArnd Bergmann return ret; 171ce8ab854SArnd Bergmann } 172ce8ab854SArnd Bergmann 173ce8ab854SArnd Bergmann static inline int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc, 174ce8ab854SArnd Bergmann u32 *status) 175ce8ab854SArnd Bergmann { 176ce8ab854SArnd Bergmann int ret; 177ce8ab854SArnd Bergmann 178ce8ab854SArnd Bergmann if ((ret = spu_run_fini(ctx, npc, status)) != 0) 179ce8ab854SArnd Bergmann return ret; 180ce8ab854SArnd Bergmann if (*status & (SPU_STATUS_STOPPED_BY_STOP | 181ce8ab854SArnd Bergmann SPU_STATUS_STOPPED_BY_HALT)) { 182ce8ab854SArnd Bergmann return *status; 183ce8ab854SArnd Bergmann } 1849add11daSArnd Bergmann if ((ret = spu_run_init(ctx, npc)) != 0) 185ce8ab854SArnd Bergmann return ret; 186ce8ab854SArnd Bergmann return 0; 187ce8ab854SArnd Bergmann } 188ce8ab854SArnd Bergmann 1892dd14934SArnd Bergmann /* 1902dd14934SArnd Bergmann * SPU syscall restarting is tricky because we violate the basic 1912dd14934SArnd Bergmann * assumption that the signal handler is running on the interrupted 1922dd14934SArnd Bergmann * thread. Here instead, the handler runs on PowerPC user space code, 1932dd14934SArnd Bergmann * while the syscall was called from the SPU. 1942dd14934SArnd Bergmann * This means we can only do a very rough approximation of POSIX 1952dd14934SArnd Bergmann * signal semantics. 1962dd14934SArnd Bergmann */ 1972dd14934SArnd Bergmann int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret, 1982dd14934SArnd Bergmann unsigned int *npc) 1992dd14934SArnd Bergmann { 2002dd14934SArnd Bergmann int ret; 2012dd14934SArnd Bergmann 2022dd14934SArnd Bergmann switch (*spu_ret) { 2032dd14934SArnd Bergmann case -ERESTARTSYS: 2042dd14934SArnd Bergmann case -ERESTARTNOINTR: 2052dd14934SArnd Bergmann /* 2062dd14934SArnd Bergmann * Enter the regular syscall restarting for 2072dd14934SArnd Bergmann * sys_spu_run, then restart the SPU syscall 2082dd14934SArnd Bergmann * callback. 2092dd14934SArnd Bergmann */ 2102dd14934SArnd Bergmann *npc -= 8; 2112dd14934SArnd Bergmann ret = -ERESTARTSYS; 2122dd14934SArnd Bergmann break; 2132dd14934SArnd Bergmann case -ERESTARTNOHAND: 2142dd14934SArnd Bergmann case -ERESTART_RESTARTBLOCK: 2152dd14934SArnd Bergmann /* 2162dd14934SArnd Bergmann * Restart block is too hard for now, just return -EINTR 2172dd14934SArnd Bergmann * to the SPU. 2182dd14934SArnd Bergmann * ERESTARTNOHAND comes from sys_pause, we also return 2192dd14934SArnd Bergmann * -EINTR from there. 2202dd14934SArnd Bergmann * Assume that we need to be restarted ourselves though. 2212dd14934SArnd Bergmann */ 2222dd14934SArnd Bergmann *spu_ret = -EINTR; 2232dd14934SArnd Bergmann ret = -ERESTARTSYS; 2242dd14934SArnd Bergmann break; 2252dd14934SArnd Bergmann default: 2262dd14934SArnd Bergmann printk(KERN_WARNING "%s: unexpected return code %ld\n", 2272dd14934SArnd Bergmann __FUNCTION__, *spu_ret); 2282dd14934SArnd Bergmann ret = 0; 2292dd14934SArnd Bergmann } 2302dd14934SArnd Bergmann return ret; 2312dd14934SArnd Bergmann } 2322dd14934SArnd Bergmann 2332dd14934SArnd Bergmann int spu_process_callback(struct spu_context *ctx) 2342dd14934SArnd Bergmann { 2352dd14934SArnd Bergmann struct spu_syscall_block s; 2362dd14934SArnd Bergmann u32 ls_pointer, npc; 2379e2fe2ceSAkinobu Mita void __iomem *ls; 2382dd14934SArnd Bergmann long spu_ret; 2392dd14934SArnd Bergmann int ret; 2402dd14934SArnd Bergmann 2412dd14934SArnd Bergmann /* get syscall block from local store */ 2429e2fe2ceSAkinobu Mita npc = ctx->ops->npc_read(ctx) & ~3; 2439e2fe2ceSAkinobu Mita ls = (void __iomem *)ctx->ops->get_ls(ctx); 2449e2fe2ceSAkinobu Mita ls_pointer = in_be32(ls + npc); 2452dd14934SArnd Bergmann if (ls_pointer > (LS_SIZE - sizeof(s))) 2462dd14934SArnd Bergmann return -EFAULT; 2479e2fe2ceSAkinobu Mita memcpy_fromio(&s, ls + ls_pointer, sizeof(s)); 2482dd14934SArnd Bergmann 2492dd14934SArnd Bergmann /* do actual syscall without pinning the spu */ 2502dd14934SArnd Bergmann ret = 0; 2512dd14934SArnd Bergmann spu_ret = -ENOSYS; 2522dd14934SArnd Bergmann npc += 4; 2532dd14934SArnd Bergmann 2542dd14934SArnd Bergmann if (s.nr_ret < __NR_syscalls) { 2552dd14934SArnd Bergmann spu_release(ctx); 2562dd14934SArnd Bergmann /* do actual system call from here */ 2572dd14934SArnd Bergmann spu_ret = spu_sys_callback(&s); 2582dd14934SArnd Bergmann if (spu_ret <= -ERESTARTSYS) { 2592dd14934SArnd Bergmann ret = spu_handle_restartsys(ctx, &spu_ret, &npc); 2602dd14934SArnd Bergmann } 2612dd14934SArnd Bergmann spu_acquire(ctx); 2622dd14934SArnd Bergmann if (ret == -ERESTARTSYS) 2632dd14934SArnd Bergmann return ret; 2642dd14934SArnd Bergmann } 2652dd14934SArnd Bergmann 2662dd14934SArnd Bergmann /* write result, jump over indirect pointer */ 2679e2fe2ceSAkinobu Mita memcpy_toio(ls + ls_pointer, &spu_ret, sizeof(spu_ret)); 2682dd14934SArnd Bergmann ctx->ops->npc_write(ctx, npc); 2692dd14934SArnd Bergmann ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE); 2702dd14934SArnd Bergmann return ret; 2712dd14934SArnd Bergmann } 2722dd14934SArnd Bergmann 273ce8ab854SArnd Bergmann static inline int spu_process_events(struct spu_context *ctx) 274ce8ab854SArnd Bergmann { 275ce8ab854SArnd Bergmann struct spu *spu = ctx->spu; 276ce8ab854SArnd Bergmann int ret = 0; 277ce8ab854SArnd Bergmann 278ce8ab854SArnd Bergmann if (spu->class_0_pending) 279ce8ab854SArnd Bergmann ret = spu_irq_class_0_bottom(spu); 280ce8ab854SArnd Bergmann if (!ret && signal_pending(current)) 281ce8ab854SArnd Bergmann ret = -ERESTARTSYS; 282ce8ab854SArnd Bergmann return ret; 283ce8ab854SArnd Bergmann } 284ce8ab854SArnd Bergmann 285ce8ab854SArnd Bergmann long spufs_run_spu(struct file *file, struct spu_context *ctx, 2869add11daSArnd Bergmann u32 *npc, u32 *event) 287ce8ab854SArnd Bergmann { 288ce8ab854SArnd Bergmann int ret; 2899add11daSArnd Bergmann u32 status; 290ce8ab854SArnd Bergmann 291e45d48a3SChristoph Hellwig if (mutex_lock_interruptible(&ctx->run_mutex)) 292ce8ab854SArnd Bergmann return -ERESTARTSYS; 293ce8ab854SArnd Bergmann 294ee2d7340SArnd Bergmann ctx->ops->master_start(ctx); 2959add11daSArnd Bergmann ctx->event_return = 0; 2969add11daSArnd Bergmann ret = spu_run_init(ctx, npc); 297ce8ab854SArnd Bergmann if (ret) 298ce8ab854SArnd Bergmann goto out; 299ce8ab854SArnd Bergmann 300ce8ab854SArnd Bergmann do { 3019add11daSArnd Bergmann ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, &status)); 302ce8ab854SArnd Bergmann if (unlikely(ret)) 303ce8ab854SArnd Bergmann break; 3049add11daSArnd Bergmann if ((status & SPU_STATUS_STOPPED_BY_STOP) && 3059add11daSArnd Bergmann (status >> SPU_STOP_STATUS_SHIFT == 0x2104)) { 3062dd14934SArnd Bergmann ret = spu_process_callback(ctx); 3072dd14934SArnd Bergmann if (ret) 3082dd14934SArnd Bergmann break; 3099add11daSArnd Bergmann status &= ~SPU_STATUS_STOPPED_BY_STOP; 3102dd14934SArnd Bergmann } 31157dace23SArnd Bergmann ret = spufs_handle_class1(ctx); 31257dace23SArnd Bergmann if (ret) 31357dace23SArnd Bergmann break; 31457dace23SArnd Bergmann 315ce8ab854SArnd Bergmann if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) { 3169add11daSArnd Bergmann ret = spu_reacquire_runnable(ctx, npc, &status); 3172eb1b120SChristoph Hellwig if (ret) { 3182eb1b120SChristoph Hellwig spu_stop_tick(ctx); 3192ebb2477SMasato Noguchi goto out2; 3202eb1b120SChristoph Hellwig } 321ce8ab854SArnd Bergmann continue; 322ce8ab854SArnd Bergmann } 323ce8ab854SArnd Bergmann ret = spu_process_events(ctx); 324ce8ab854SArnd Bergmann 3259add11daSArnd Bergmann } while (!ret && !(status & (SPU_STATUS_STOPPED_BY_STOP | 326ce8ab854SArnd Bergmann SPU_STATUS_STOPPED_BY_HALT))); 327ce8ab854SArnd Bergmann 328ee2d7340SArnd Bergmann ctx->ops->master_stop(ctx); 3299add11daSArnd Bergmann ret = spu_run_fini(ctx, npc, &status); 330ce8ab854SArnd Bergmann spu_yield(ctx); 331ce8ab854SArnd Bergmann 3322ebb2477SMasato Noguchi out2: 3332ebb2477SMasato Noguchi if ((ret == 0) || 3342ebb2477SMasato Noguchi ((ret == -ERESTARTSYS) && 3352ebb2477SMasato Noguchi ((status & SPU_STATUS_STOPPED_BY_HALT) || 3362ebb2477SMasato Noguchi ((status & SPU_STATUS_STOPPED_BY_STOP) && 3372ebb2477SMasato Noguchi (status >> SPU_STOP_STATUS_SHIFT != 0x2104))))) 3382ebb2477SMasato Noguchi ret = status; 3392ebb2477SMasato Noguchi 3402ebb2477SMasato Noguchi if ((status & SPU_STATUS_STOPPED_BY_STOP) 3412ebb2477SMasato Noguchi && (status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) { 3422ebb2477SMasato Noguchi force_sig(SIGTRAP, current); 3432ebb2477SMasato Noguchi ret = -ERESTARTSYS; 3442ebb2477SMasato Noguchi } 3452ebb2477SMasato Noguchi 346ce8ab854SArnd Bergmann out: 3479add11daSArnd Bergmann *event = ctx->event_return; 348e45d48a3SChristoph Hellwig mutex_unlock(&ctx->run_mutex); 349ce8ab854SArnd Bergmann return ret; 350ce8ab854SArnd Bergmann } 351