arnd@arndb.de | 0afacde | 2006-10-24 18:31:18 +0200 | [diff] [blame] | 1 | #define DEBUG |
| 2 | |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 3 | #include <linux/wait.h> |
| 4 | #include <linux/ptrace.h> |
| 5 | |
| 6 | #include <asm/spu.h> |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame^] | 7 | #include <asm/spu_priv1.h> |
| 8 | #include <asm/io.h> |
Dave Jones | cfff5b2 | 2006-03-31 23:53:09 -0500 | [diff] [blame] | 9 | #include <asm/unistd.h> |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 10 | |
| 11 | #include "spufs.h" |
| 12 | |
| 13 | /* interrupt-level stop callback function. */ |
| 14 | void spufs_stop_callback(struct spu *spu) |
| 15 | { |
| 16 | struct spu_context *ctx = spu->ctx; |
| 17 | |
| 18 | wake_up_all(&ctx->stop_wq); |
| 19 | } |
| 20 | |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 21 | void spufs_dma_callback(struct spu *spu, int type) |
| 22 | { |
| 23 | struct spu_context *ctx = spu->ctx; |
| 24 | |
| 25 | if (ctx->flags & SPU_CREATE_EVENTS_ENABLED) { |
| 26 | ctx->event_return |= type; |
| 27 | wake_up_all(&ctx->stop_wq); |
| 28 | } else { |
| 29 | switch (type) { |
| 30 | case SPE_EVENT_DMA_ALIGNMENT: |
Arnd Bergmann | 453d9f7 | 2006-11-20 18:45:03 +0100 | [diff] [blame] | 31 | case SPE_EVENT_SPE_DATA_STORAGE: |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 32 | case SPE_EVENT_INVALID_DMA: |
| 33 | force_sig(SIGBUS, /* info, */ current); |
| 34 | break; |
| 35 | case SPE_EVENT_SPE_ERROR: |
| 36 | force_sig(SIGILL, /* info */ current); |
| 37 | break; |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 42 | static inline int spu_stopped(struct spu_context *ctx, u32 * stat) |
| 43 | { |
| 44 | struct spu *spu; |
| 45 | u64 pte_fault; |
| 46 | |
| 47 | *stat = ctx->ops->status_read(ctx); |
| 48 | if (ctx->state != SPU_STATE_RUNNABLE) |
| 49 | return 1; |
| 50 | spu = ctx->spu; |
| 51 | pte_fault = spu->dsisr & |
| 52 | (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED); |
| 53 | return (!(*stat & 0x1) || pte_fault || spu->class_0_pending) ? 1 : 0; |
| 54 | } |
| 55 | |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame^] | 56 | static int spu_setup_isolated(struct spu_context *ctx) |
| 57 | { |
| 58 | int ret; |
| 59 | u64 __iomem *mfc_cntl; |
| 60 | u64 sr1; |
| 61 | u32 status; |
| 62 | unsigned long timeout; |
| 63 | const u32 status_loading = SPU_STATUS_RUNNING |
| 64 | | SPU_STATUS_ISOLATED_STATE | SPU_STATUS_ISOLATED_LOAD_STATUS; |
| 65 | |
| 66 | if (!isolated_loader) |
| 67 | return -ENODEV; |
| 68 | |
| 69 | ret = spu_acquire_exclusive(ctx); |
| 70 | if (ret) |
| 71 | goto out; |
| 72 | |
| 73 | mfc_cntl = &ctx->spu->priv2->mfc_control_RW; |
| 74 | |
| 75 | /* purge the MFC DMA queue to ensure no spurious accesses before we |
| 76 | * enter kernel mode */ |
| 77 | timeout = jiffies + HZ; |
| 78 | out_be64(mfc_cntl, MFC_CNTL_PURGE_DMA_REQUEST); |
| 79 | while ((in_be64(mfc_cntl) & MFC_CNTL_PURGE_DMA_STATUS_MASK) |
| 80 | != MFC_CNTL_PURGE_DMA_COMPLETE) { |
| 81 | if (time_after(jiffies, timeout)) { |
| 82 | printk(KERN_ERR "%s: timeout flushing MFC DMA queue\n", |
| 83 | __FUNCTION__); |
| 84 | ret = -EIO; |
| 85 | goto out_unlock; |
| 86 | } |
| 87 | cond_resched(); |
| 88 | } |
| 89 | |
| 90 | /* put the SPE in kernel mode to allow access to the loader */ |
| 91 | sr1 = spu_mfc_sr1_get(ctx->spu); |
| 92 | sr1 &= ~MFC_STATE1_PROBLEM_STATE_MASK; |
| 93 | spu_mfc_sr1_set(ctx->spu, sr1); |
| 94 | |
| 95 | /* start the loader */ |
| 96 | ctx->ops->signal1_write(ctx, (unsigned long)isolated_loader >> 32); |
| 97 | ctx->ops->signal2_write(ctx, |
| 98 | (unsigned long)isolated_loader & 0xffffffff); |
| 99 | |
| 100 | ctx->ops->runcntl_write(ctx, |
| 101 | SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE); |
| 102 | |
| 103 | ret = 0; |
| 104 | timeout = jiffies + HZ; |
| 105 | while (((status = ctx->ops->status_read(ctx)) & status_loading) == |
| 106 | status_loading) { |
| 107 | if (time_after(jiffies, timeout)) { |
| 108 | printk(KERN_ERR "%s: timeout waiting for loader\n", |
| 109 | __FUNCTION__); |
| 110 | ret = -EIO; |
| 111 | goto out_drop_priv; |
| 112 | } |
| 113 | cond_resched(); |
| 114 | } |
| 115 | |
| 116 | if (!(status & SPU_STATUS_RUNNING)) { |
| 117 | /* If isolated LOAD has failed: run SPU, we will get a stop-and |
| 118 | * signal later. */ |
| 119 | pr_debug("%s: isolated LOAD failed\n", __FUNCTION__); |
| 120 | ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE); |
| 121 | ret = -EACCES; |
| 122 | |
| 123 | } else if (!(status & SPU_STATUS_ISOLATED_STATE)) { |
| 124 | /* This isn't allowed by the CBEA, but check anyway */ |
| 125 | pr_debug("%s: SPU fell out of isolated mode?\n", __FUNCTION__); |
| 126 | ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_STOP); |
| 127 | ret = -EINVAL; |
| 128 | } |
| 129 | |
| 130 | out_drop_priv: |
| 131 | /* Finished accessing the loader. Drop kernel mode */ |
| 132 | sr1 |= MFC_STATE1_PROBLEM_STATE_MASK; |
| 133 | spu_mfc_sr1_set(ctx->spu, sr1); |
| 134 | |
| 135 | out_unlock: |
| 136 | spu_release_exclusive(ctx); |
| 137 | out: |
| 138 | return ret; |
| 139 | } |
| 140 | |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 141 | static inline int spu_run_init(struct spu_context *ctx, u32 * npc) |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 142 | { |
| 143 | int ret; |
Mark Nutter | 5737edd | 2006-10-24 18:31:16 +0200 | [diff] [blame] | 144 | unsigned long runcntl = SPU_RUNCNTL_RUNNABLE; |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 145 | |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame^] | 146 | ret = spu_acquire_runnable(ctx); |
| 147 | if (ret) |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 148 | return ret; |
Mark Nutter | 5737edd | 2006-10-24 18:31:16 +0200 | [diff] [blame] | 149 | |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame^] | 150 | if (ctx->flags & SPU_CREATE_ISOLATE) { |
| 151 | if (!(ctx->ops->status_read(ctx) & SPU_STATUS_ISOLATED_STATE)) { |
| 152 | /* Need to release ctx, because spu_setup_isolated will |
| 153 | * acquire it exclusively. |
| 154 | */ |
| 155 | spu_release(ctx); |
| 156 | ret = spu_setup_isolated(ctx); |
| 157 | if (!ret) |
| 158 | ret = spu_acquire_runnable(ctx); |
| 159 | } |
| 160 | |
| 161 | /* if userspace has set the runcntrl register (eg, to issue an |
| 162 | * isolated exit), we need to re-set it here */ |
| 163 | runcntl = ctx->ops->runcntl_read(ctx) & |
| 164 | (SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE); |
| 165 | if (runcntl == 0) |
| 166 | runcntl = SPU_RUNCNTL_RUNNABLE; |
| 167 | } else |
Mark Nutter | 5737edd | 2006-10-24 18:31:16 +0200 | [diff] [blame] | 168 | ctx->ops->npc_write(ctx, *npc); |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame^] | 169 | |
| 170 | ctx->ops->runcntl_write(ctx, runcntl); |
| 171 | return ret; |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static inline int spu_run_fini(struct spu_context *ctx, u32 * npc, |
| 175 | u32 * status) |
| 176 | { |
| 177 | int ret = 0; |
| 178 | |
| 179 | *status = ctx->ops->status_read(ctx); |
| 180 | *npc = ctx->ops->npc_read(ctx); |
| 181 | spu_release(ctx); |
| 182 | |
| 183 | if (signal_pending(current)) |
| 184 | ret = -ERESTARTSYS; |
Masato Noguchi | 2ebb247 | 2006-11-20 18:45:04 +0100 | [diff] [blame] | 185 | |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | static inline int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc, |
| 190 | u32 *status) |
| 191 | { |
| 192 | int ret; |
| 193 | |
| 194 | if ((ret = spu_run_fini(ctx, npc, status)) != 0) |
| 195 | return ret; |
| 196 | if (*status & (SPU_STATUS_STOPPED_BY_STOP | |
| 197 | SPU_STATUS_STOPPED_BY_HALT)) { |
| 198 | return *status; |
| 199 | } |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 200 | if ((ret = spu_run_init(ctx, npc)) != 0) |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 201 | return ret; |
| 202 | return 0; |
| 203 | } |
| 204 | |
Arnd Bergmann | 2dd1493 | 2006-03-23 00:00:09 +0100 | [diff] [blame] | 205 | /* |
| 206 | * SPU syscall restarting is tricky because we violate the basic |
| 207 | * assumption that the signal handler is running on the interrupted |
| 208 | * thread. Here instead, the handler runs on PowerPC user space code, |
| 209 | * while the syscall was called from the SPU. |
| 210 | * This means we can only do a very rough approximation of POSIX |
| 211 | * signal semantics. |
| 212 | */ |
| 213 | int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret, |
| 214 | unsigned int *npc) |
| 215 | { |
| 216 | int ret; |
| 217 | |
| 218 | switch (*spu_ret) { |
| 219 | case -ERESTARTSYS: |
| 220 | case -ERESTARTNOINTR: |
| 221 | /* |
| 222 | * Enter the regular syscall restarting for |
| 223 | * sys_spu_run, then restart the SPU syscall |
| 224 | * callback. |
| 225 | */ |
| 226 | *npc -= 8; |
| 227 | ret = -ERESTARTSYS; |
| 228 | break; |
| 229 | case -ERESTARTNOHAND: |
| 230 | case -ERESTART_RESTARTBLOCK: |
| 231 | /* |
| 232 | * Restart block is too hard for now, just return -EINTR |
| 233 | * to the SPU. |
| 234 | * ERESTARTNOHAND comes from sys_pause, we also return |
| 235 | * -EINTR from there. |
| 236 | * Assume that we need to be restarted ourselves though. |
| 237 | */ |
| 238 | *spu_ret = -EINTR; |
| 239 | ret = -ERESTARTSYS; |
| 240 | break; |
| 241 | default: |
| 242 | printk(KERN_WARNING "%s: unexpected return code %ld\n", |
| 243 | __FUNCTION__, *spu_ret); |
| 244 | ret = 0; |
| 245 | } |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | int spu_process_callback(struct spu_context *ctx) |
| 250 | { |
| 251 | struct spu_syscall_block s; |
| 252 | u32 ls_pointer, npc; |
| 253 | char *ls; |
| 254 | long spu_ret; |
| 255 | int ret; |
| 256 | |
| 257 | /* get syscall block from local store */ |
| 258 | npc = ctx->ops->npc_read(ctx); |
| 259 | ls = ctx->ops->get_ls(ctx); |
| 260 | ls_pointer = *(u32*)(ls + npc); |
| 261 | if (ls_pointer > (LS_SIZE - sizeof(s))) |
| 262 | return -EFAULT; |
| 263 | memcpy(&s, ls + ls_pointer, sizeof (s)); |
| 264 | |
| 265 | /* do actual syscall without pinning the spu */ |
| 266 | ret = 0; |
| 267 | spu_ret = -ENOSYS; |
| 268 | npc += 4; |
| 269 | |
| 270 | if (s.nr_ret < __NR_syscalls) { |
| 271 | spu_release(ctx); |
| 272 | /* do actual system call from here */ |
| 273 | spu_ret = spu_sys_callback(&s); |
| 274 | if (spu_ret <= -ERESTARTSYS) { |
| 275 | ret = spu_handle_restartsys(ctx, &spu_ret, &npc); |
| 276 | } |
| 277 | spu_acquire(ctx); |
| 278 | if (ret == -ERESTARTSYS) |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | /* write result, jump over indirect pointer */ |
| 283 | memcpy(ls + ls_pointer, &spu_ret, sizeof (spu_ret)); |
| 284 | ctx->ops->npc_write(ctx, npc); |
| 285 | ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE); |
| 286 | return ret; |
| 287 | } |
| 288 | |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 289 | static inline int spu_process_events(struct spu_context *ctx) |
| 290 | { |
| 291 | struct spu *spu = ctx->spu; |
| 292 | u64 pte_fault = MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED; |
| 293 | int ret = 0; |
| 294 | |
| 295 | if (spu->dsisr & pte_fault) |
| 296 | ret = spu_irq_class_1_bottom(spu); |
| 297 | if (spu->class_0_pending) |
| 298 | ret = spu_irq_class_0_bottom(spu); |
| 299 | if (!ret && signal_pending(current)) |
| 300 | ret = -ERESTARTSYS; |
| 301 | return ret; |
| 302 | } |
| 303 | |
| 304 | long spufs_run_spu(struct file *file, struct spu_context *ctx, |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 305 | u32 *npc, u32 *event) |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 306 | { |
| 307 | int ret; |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 308 | u32 status; |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 309 | |
| 310 | if (down_interruptible(&ctx->run_sema)) |
| 311 | return -ERESTARTSYS; |
| 312 | |
Arnd Bergmann | ee2d734 | 2006-11-20 18:45:08 +0100 | [diff] [blame] | 313 | ctx->ops->master_start(ctx); |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 314 | ctx->event_return = 0; |
| 315 | ret = spu_run_init(ctx, npc); |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 316 | if (ret) |
| 317 | goto out; |
| 318 | |
| 319 | do { |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 320 | ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, &status)); |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 321 | if (unlikely(ret)) |
| 322 | break; |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 323 | if ((status & SPU_STATUS_STOPPED_BY_STOP) && |
| 324 | (status >> SPU_STOP_STATUS_SHIFT == 0x2104)) { |
Arnd Bergmann | 2dd1493 | 2006-03-23 00:00:09 +0100 | [diff] [blame] | 325 | ret = spu_process_callback(ctx); |
| 326 | if (ret) |
| 327 | break; |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 328 | status &= ~SPU_STATUS_STOPPED_BY_STOP; |
Arnd Bergmann | 2dd1493 | 2006-03-23 00:00:09 +0100 | [diff] [blame] | 329 | } |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 330 | if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) { |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 331 | ret = spu_reacquire_runnable(ctx, npc, &status); |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 332 | if (ret) |
Masato Noguchi | 2ebb247 | 2006-11-20 18:45:04 +0100 | [diff] [blame] | 333 | goto out2; |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 334 | continue; |
| 335 | } |
| 336 | ret = spu_process_events(ctx); |
| 337 | |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 338 | } while (!ret && !(status & (SPU_STATUS_STOPPED_BY_STOP | |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 339 | SPU_STATUS_STOPPED_BY_HALT))); |
| 340 | |
Arnd Bergmann | ee2d734 | 2006-11-20 18:45:08 +0100 | [diff] [blame] | 341 | ctx->ops->master_stop(ctx); |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 342 | ret = spu_run_fini(ctx, npc, &status); |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 343 | spu_yield(ctx); |
| 344 | |
Masato Noguchi | 2ebb247 | 2006-11-20 18:45:04 +0100 | [diff] [blame] | 345 | out2: |
| 346 | if ((ret == 0) || |
| 347 | ((ret == -ERESTARTSYS) && |
| 348 | ((status & SPU_STATUS_STOPPED_BY_HALT) || |
| 349 | ((status & SPU_STATUS_STOPPED_BY_STOP) && |
| 350 | (status >> SPU_STOP_STATUS_SHIFT != 0x2104))))) |
| 351 | ret = status; |
| 352 | |
| 353 | if (unlikely(current->ptrace & PT_PTRACED)) { |
| 354 | if ((status & SPU_STATUS_STOPPED_BY_STOP) |
| 355 | && (status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) { |
| 356 | force_sig(SIGTRAP, current); |
| 357 | ret = -ERESTARTSYS; |
| 358 | } |
| 359 | } |
| 360 | |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 361 | out: |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 362 | *event = ctx->event_return; |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 363 | up(&ctx->run_sema); |
| 364 | return ret; |
| 365 | } |
| 366 | |