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 | |
Bob Nelson | 36aaccc | 2007-07-20 21:39:52 +0200 | [diff] [blame] | 21 | static inline int spu_stopped(struct spu_context *ctx, u32 *stat) |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 22 | { |
| 23 | struct spu *spu; |
| 24 | u64 pte_fault; |
| 25 | |
| 26 | *stat = ctx->ops->status_read(ctx); |
Bob Nelson | 36aaccc | 2007-07-20 21:39:52 +0200 | [diff] [blame] | 27 | |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 28 | spu = ctx->spu; |
Bob Nelson | 36aaccc | 2007-07-20 21:39:52 +0200 | [diff] [blame] | 29 | if (ctx->state != SPU_STATE_RUNNABLE || |
| 30 | test_bit(SPU_SCHED_NOTIFY_ACTIVE, &ctx->sched_flags)) |
| 31 | return 1; |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 32 | pte_fault = spu->dsisr & |
| 33 | (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED); |
Sebastian Siewior | be70317 | 2007-06-29 10:57:50 +1000 | [diff] [blame] | 34 | return (!(*stat & SPU_STATUS_RUNNING) || pte_fault || spu->class_0_pending) ? |
| 35 | 1 : 0; |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 36 | } |
| 37 | |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame] | 38 | static int spu_setup_isolated(struct spu_context *ctx) |
| 39 | { |
| 40 | int ret; |
| 41 | u64 __iomem *mfc_cntl; |
| 42 | u64 sr1; |
| 43 | u32 status; |
| 44 | unsigned long timeout; |
| 45 | const u32 status_loading = SPU_STATUS_RUNNING |
| 46 | | SPU_STATUS_ISOLATED_STATE | SPU_STATUS_ISOLATED_LOAD_STATUS; |
| 47 | |
Christoph Hellwig | 7ec18ab | 2007-04-23 21:08:12 +0200 | [diff] [blame] | 48 | ret = -ENODEV; |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame] | 49 | if (!isolated_loader) |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame] | 50 | goto out; |
| 51 | |
Christoph Hellwig | 7ec18ab | 2007-04-23 21:08:12 +0200 | [diff] [blame] | 52 | /* |
| 53 | * We need to exclude userspace access to the context. |
| 54 | * |
| 55 | * To protect against memory access we invalidate all ptes |
| 56 | * and make sure the pagefault handlers block on the mutex. |
| 57 | */ |
| 58 | spu_unmap_mappings(ctx); |
| 59 | |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame] | 60 | mfc_cntl = &ctx->spu->priv2->mfc_control_RW; |
| 61 | |
| 62 | /* purge the MFC DMA queue to ensure no spurious accesses before we |
| 63 | * enter kernel mode */ |
| 64 | timeout = jiffies + HZ; |
| 65 | out_be64(mfc_cntl, MFC_CNTL_PURGE_DMA_REQUEST); |
| 66 | while ((in_be64(mfc_cntl) & MFC_CNTL_PURGE_DMA_STATUS_MASK) |
| 67 | != MFC_CNTL_PURGE_DMA_COMPLETE) { |
| 68 | if (time_after(jiffies, timeout)) { |
| 69 | printk(KERN_ERR "%s: timeout flushing MFC DMA queue\n", |
| 70 | __FUNCTION__); |
| 71 | ret = -EIO; |
Christoph Hellwig | 7ec18ab | 2007-04-23 21:08:12 +0200 | [diff] [blame] | 72 | goto out; |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame] | 73 | } |
| 74 | cond_resched(); |
| 75 | } |
| 76 | |
| 77 | /* put the SPE in kernel mode to allow access to the loader */ |
| 78 | sr1 = spu_mfc_sr1_get(ctx->spu); |
| 79 | sr1 &= ~MFC_STATE1_PROBLEM_STATE_MASK; |
| 80 | spu_mfc_sr1_set(ctx->spu, sr1); |
| 81 | |
| 82 | /* start the loader */ |
| 83 | ctx->ops->signal1_write(ctx, (unsigned long)isolated_loader >> 32); |
| 84 | ctx->ops->signal2_write(ctx, |
| 85 | (unsigned long)isolated_loader & 0xffffffff); |
| 86 | |
| 87 | ctx->ops->runcntl_write(ctx, |
| 88 | SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE); |
| 89 | |
| 90 | ret = 0; |
| 91 | timeout = jiffies + HZ; |
| 92 | while (((status = ctx->ops->status_read(ctx)) & status_loading) == |
| 93 | status_loading) { |
| 94 | if (time_after(jiffies, timeout)) { |
| 95 | printk(KERN_ERR "%s: timeout waiting for loader\n", |
| 96 | __FUNCTION__); |
| 97 | ret = -EIO; |
| 98 | goto out_drop_priv; |
| 99 | } |
| 100 | cond_resched(); |
| 101 | } |
| 102 | |
| 103 | if (!(status & SPU_STATUS_RUNNING)) { |
| 104 | /* If isolated LOAD has failed: run SPU, we will get a stop-and |
| 105 | * signal later. */ |
| 106 | pr_debug("%s: isolated LOAD failed\n", __FUNCTION__); |
| 107 | ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE); |
| 108 | ret = -EACCES; |
Christoph Hellwig | 7ec18ab | 2007-04-23 21:08:12 +0200 | [diff] [blame] | 109 | goto out_drop_priv; |
| 110 | } |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame] | 111 | |
Christoph Hellwig | 7ec18ab | 2007-04-23 21:08:12 +0200 | [diff] [blame] | 112 | if (!(status & SPU_STATUS_ISOLATED_STATE)) { |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame] | 113 | /* This isn't allowed by the CBEA, but check anyway */ |
| 114 | pr_debug("%s: SPU fell out of isolated mode?\n", __FUNCTION__); |
| 115 | ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_STOP); |
| 116 | ret = -EINVAL; |
Christoph Hellwig | 7ec18ab | 2007-04-23 21:08:12 +0200 | [diff] [blame] | 117 | goto out_drop_priv; |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | out_drop_priv: |
| 121 | /* Finished accessing the loader. Drop kernel mode */ |
| 122 | sr1 |= MFC_STATE1_PROBLEM_STATE_MASK; |
| 123 | spu_mfc_sr1_set(ctx->spu, sr1); |
| 124 | |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame] | 125 | out: |
| 126 | return ret; |
| 127 | } |
| 128 | |
Bob Nelson | 36aaccc | 2007-07-20 21:39:52 +0200 | [diff] [blame] | 129 | static int spu_run_init(struct spu_context *ctx, u32 *npc) |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 130 | { |
Luke Browning | cc210b3 | 2007-12-20 16:39:59 +0900 | [diff] [blame^] | 131 | unsigned long runcntl; |
| 132 | |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 133 | spuctx_switch_state(ctx, SPU_UTIL_SYSTEM); |
| 134 | |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame] | 135 | if (ctx->flags & SPU_CREATE_ISOLATE) { |
Christoph Hellwig | aa45e25 | 2007-04-23 21:08:27 +0200 | [diff] [blame] | 136 | |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame] | 137 | if (!(ctx->ops->status_read(ctx) & SPU_STATUS_ISOLATED_STATE)) { |
Christoph Hellwig | aa45e25 | 2007-04-23 21:08:27 +0200 | [diff] [blame] | 138 | int ret = spu_setup_isolated(ctx); |
Christoph Hellwig | 7ec18ab | 2007-04-23 21:08:12 +0200 | [diff] [blame] | 139 | if (ret) |
Christoph Hellwig | aa45e25 | 2007-04-23 21:08:27 +0200 | [diff] [blame] | 140 | return ret; |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* if userspace has set the runcntrl register (eg, to issue an |
| 144 | * isolated exit), we need to re-set it here */ |
| 145 | runcntl = ctx->ops->runcntl_read(ctx) & |
| 146 | (SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE); |
| 147 | if (runcntl == 0) |
| 148 | runcntl = SPU_RUNCNTL_RUNNABLE; |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 149 | } else { |
Luke Browning | cc210b3 | 2007-12-20 16:39:59 +0900 | [diff] [blame^] | 150 | unsigned long privcntl; |
| 151 | |
Benjamin Herrenschmidt | 0516923 | 2007-06-04 15:15:37 +1000 | [diff] [blame] | 152 | if (test_thread_flag(TIF_SINGLESTEP)) |
Luke Browning | cc210b3 | 2007-12-20 16:39:59 +0900 | [diff] [blame^] | 153 | privcntl = SPU_PRIVCNTL_MODE_SINGLE_STEP; |
| 154 | else |
| 155 | privcntl = SPU_PRIVCNTL_MODE_NORMAL; |
| 156 | runcntl = SPU_RUNCNTL_RUNNABLE; |
| 157 | |
| 158 | ctx->ops->npc_write(ctx, *npc); |
| 159 | ctx->ops->privcntl_write(ctx, privcntl); |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 160 | } |
Jeremy Kerr | c6730ed | 2006-11-20 18:45:10 +0100 | [diff] [blame] | 161 | |
Luke Browning | cc210b3 | 2007-12-20 16:39:59 +0900 | [diff] [blame^] | 162 | ctx->ops->runcntl_write(ctx, runcntl); |
| 163 | |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 164 | spuctx_switch_state(ctx, SPU_UTIL_USER); |
| 165 | |
Christoph Hellwig | aa45e25 | 2007-04-23 21:08:27 +0200 | [diff] [blame] | 166 | return 0; |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Bob Nelson | 36aaccc | 2007-07-20 21:39:52 +0200 | [diff] [blame] | 169 | static int spu_run_fini(struct spu_context *ctx, u32 *npc, |
| 170 | u32 *status) |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 171 | { |
| 172 | int ret = 0; |
| 173 | |
| 174 | *status = ctx->ops->status_read(ctx); |
| 175 | *npc = ctx->ops->npc_read(ctx); |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 176 | |
| 177 | spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED); |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 178 | spu_release(ctx); |
| 179 | |
| 180 | if (signal_pending(current)) |
| 181 | ret = -ERESTARTSYS; |
Masato Noguchi | 2ebb247 | 2006-11-20 18:45:04 +0100 | [diff] [blame] | 182 | |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 183 | return ret; |
| 184 | } |
| 185 | |
Christoph Hellwig | aa45e25 | 2007-04-23 21:08:27 +0200 | [diff] [blame] | 186 | static int spu_reacquire_runnable(struct spu_context *ctx, u32 *npc, |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 187 | u32 *status) |
| 188 | { |
| 189 | int ret; |
| 190 | |
Christoph Hellwig | aa45e25 | 2007-04-23 21:08:27 +0200 | [diff] [blame] | 191 | ret = spu_run_fini(ctx, npc, status); |
| 192 | if (ret) |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 193 | return ret; |
Christoph Hellwig | aa45e25 | 2007-04-23 21:08:27 +0200 | [diff] [blame] | 194 | |
| 195 | if (*status & (SPU_STATUS_STOPPED_BY_STOP | SPU_STATUS_STOPPED_BY_HALT)) |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 196 | return *status; |
Christoph Hellwig | aa45e25 | 2007-04-23 21:08:27 +0200 | [diff] [blame] | 197 | |
| 198 | ret = spu_acquire_runnable(ctx, 0); |
| 199 | if (ret) |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 200 | return ret; |
Christoph Hellwig | aa45e25 | 2007-04-23 21:08:27 +0200 | [diff] [blame] | 201 | |
Andre Detsch | ada83da | 2007-08-21 10:06:22 +0800 | [diff] [blame] | 202 | spuctx_switch_state(ctx, SPU_UTIL_USER); |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 203 | return 0; |
| 204 | } |
| 205 | |
Arnd Bergmann | 2dd1493 | 2006-03-23 00:00:09 +0100 | [diff] [blame] | 206 | /* |
| 207 | * SPU syscall restarting is tricky because we violate the basic |
| 208 | * assumption that the signal handler is running on the interrupted |
| 209 | * thread. Here instead, the handler runs on PowerPC user space code, |
| 210 | * while the syscall was called from the SPU. |
| 211 | * This means we can only do a very rough approximation of POSIX |
| 212 | * signal semantics. |
| 213 | */ |
Sebastian Siewior | 1238819 | 2007-09-19 14:38:12 +1000 | [diff] [blame] | 214 | static int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret, |
Arnd Bergmann | 2dd1493 | 2006-03-23 00:00:09 +0100 | [diff] [blame] | 215 | unsigned int *npc) |
| 216 | { |
| 217 | int ret; |
| 218 | |
| 219 | switch (*spu_ret) { |
| 220 | case -ERESTARTSYS: |
| 221 | case -ERESTARTNOINTR: |
| 222 | /* |
| 223 | * Enter the regular syscall restarting for |
| 224 | * sys_spu_run, then restart the SPU syscall |
| 225 | * callback. |
| 226 | */ |
| 227 | *npc -= 8; |
| 228 | ret = -ERESTARTSYS; |
| 229 | break; |
| 230 | case -ERESTARTNOHAND: |
| 231 | case -ERESTART_RESTARTBLOCK: |
| 232 | /* |
| 233 | * Restart block is too hard for now, just return -EINTR |
| 234 | * to the SPU. |
| 235 | * ERESTARTNOHAND comes from sys_pause, we also return |
| 236 | * -EINTR from there. |
| 237 | * Assume that we need to be restarted ourselves though. |
| 238 | */ |
| 239 | *spu_ret = -EINTR; |
| 240 | ret = -ERESTARTSYS; |
| 241 | break; |
| 242 | default: |
| 243 | printk(KERN_WARNING "%s: unexpected return code %ld\n", |
| 244 | __FUNCTION__, *spu_ret); |
| 245 | ret = 0; |
| 246 | } |
| 247 | return ret; |
| 248 | } |
| 249 | |
Sebastian Siewior | 1238819 | 2007-09-19 14:38:12 +1000 | [diff] [blame] | 250 | static int spu_process_callback(struct spu_context *ctx) |
Arnd Bergmann | 2dd1493 | 2006-03-23 00:00:09 +0100 | [diff] [blame] | 251 | { |
| 252 | struct spu_syscall_block s; |
| 253 | u32 ls_pointer, npc; |
Akinobu Mita | 9e2fe2c | 2007-04-23 21:08:22 +0200 | [diff] [blame] | 254 | void __iomem *ls; |
Arnd Bergmann | 2dd1493 | 2006-03-23 00:00:09 +0100 | [diff] [blame] | 255 | long spu_ret; |
| 256 | int ret; |
| 257 | |
| 258 | /* get syscall block from local store */ |
Akinobu Mita | 9e2fe2c | 2007-04-23 21:08:22 +0200 | [diff] [blame] | 259 | npc = ctx->ops->npc_read(ctx) & ~3; |
| 260 | ls = (void __iomem *)ctx->ops->get_ls(ctx); |
| 261 | ls_pointer = in_be32(ls + npc); |
Arnd Bergmann | 2dd1493 | 2006-03-23 00:00:09 +0100 | [diff] [blame] | 262 | if (ls_pointer > (LS_SIZE - sizeof(s))) |
| 263 | return -EFAULT; |
Akinobu Mita | 9e2fe2c | 2007-04-23 21:08:22 +0200 | [diff] [blame] | 264 | memcpy_fromio(&s, ls + ls_pointer, sizeof(s)); |
Arnd Bergmann | 2dd1493 | 2006-03-23 00:00:09 +0100 | [diff] [blame] | 265 | |
| 266 | /* do actual syscall without pinning the spu */ |
| 267 | ret = 0; |
| 268 | spu_ret = -ENOSYS; |
| 269 | npc += 4; |
| 270 | |
| 271 | if (s.nr_ret < __NR_syscalls) { |
| 272 | spu_release(ctx); |
| 273 | /* do actual system call from here */ |
| 274 | spu_ret = spu_sys_callback(&s); |
| 275 | if (spu_ret <= -ERESTARTSYS) { |
| 276 | ret = spu_handle_restartsys(ctx, &spu_ret, &npc); |
| 277 | } |
| 278 | spu_acquire(ctx); |
| 279 | if (ret == -ERESTARTSYS) |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | /* write result, jump over indirect pointer */ |
Akinobu Mita | 9e2fe2c | 2007-04-23 21:08:22 +0200 | [diff] [blame] | 284 | memcpy_toio(ls + ls_pointer, &spu_ret, sizeof(spu_ret)); |
Arnd Bergmann | 2dd1493 | 2006-03-23 00:00:09 +0100 | [diff] [blame] | 285 | ctx->ops->npc_write(ctx, npc); |
| 286 | ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE); |
| 287 | return ret; |
| 288 | } |
| 289 | |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 290 | static inline int spu_process_events(struct spu_context *ctx) |
| 291 | { |
| 292 | struct spu *spu = ctx->spu; |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 293 | int ret = 0; |
| 294 | |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 295 | if (spu->class_0_pending) |
| 296 | ret = spu_irq_class_0_bottom(spu); |
| 297 | if (!ret && signal_pending(current)) |
| 298 | ret = -ERESTARTSYS; |
| 299 | return ret; |
| 300 | } |
| 301 | |
Jeremy Kerr | 50af32a | 2007-07-20 21:39:42 +0200 | [diff] [blame] | 302 | long spufs_run_spu(struct spu_context *ctx, u32 *npc, u32 *event) |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 303 | { |
| 304 | int ret; |
Bob Nelson | 36aaccc | 2007-07-20 21:39:52 +0200 | [diff] [blame] | 305 | struct spu *spu; |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 306 | u32 status; |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 307 | |
Christoph Hellwig | e45d48a3 | 2007-04-23 21:08:17 +0200 | [diff] [blame] | 308 | if (mutex_lock_interruptible(&ctx->run_mutex)) |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 309 | return -ERESTARTSYS; |
| 310 | |
Masato Noguchi | c25620d | 2007-12-05 13:49:31 +1100 | [diff] [blame] | 311 | spu_enable_spu(ctx); |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 312 | ctx->event_return = 0; |
Christoph Hellwig | aa45e25 | 2007-04-23 21:08:27 +0200 | [diff] [blame] | 313 | |
Christoph Hellwig | 2cf2b3b | 2007-06-29 10:57:55 +1000 | [diff] [blame] | 314 | spu_acquire(ctx); |
| 315 | if (ctx->state == SPU_STATE_SAVED) { |
| 316 | __spu_update_sched_info(ctx); |
Christoph Hellwig | 9d78592 | 2007-07-25 21:31:09 +1000 | [diff] [blame] | 317 | spu_set_timeslice(ctx); |
Christoph Hellwig | 2cf2b3b | 2007-06-29 10:57:55 +1000 | [diff] [blame] | 318 | |
| 319 | ret = spu_activate(ctx, 0); |
| 320 | if (ret) { |
| 321 | spu_release(ctx); |
| 322 | goto out; |
| 323 | } |
| 324 | } else { |
| 325 | /* |
| 326 | * We have to update the scheduling priority under active_mutex |
| 327 | * to protect against find_victim(). |
Christoph Hellwig | 9d78592 | 2007-07-25 21:31:09 +1000 | [diff] [blame] | 328 | * |
| 329 | * No need to update the timeslice ASAP, it will get updated |
| 330 | * once the current one has expired. |
Christoph Hellwig | 2cf2b3b | 2007-06-29 10:57:55 +1000 | [diff] [blame] | 331 | */ |
| 332 | spu_update_sched_info(ctx); |
| 333 | } |
Christoph Hellwig | aa45e25 | 2007-04-23 21:08:27 +0200 | [diff] [blame] | 334 | |
| 335 | ret = spu_run_init(ctx, npc); |
| 336 | if (ret) { |
| 337 | spu_release(ctx); |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 338 | goto out; |
Christoph Hellwig | aa45e25 | 2007-04-23 21:08:27 +0200 | [diff] [blame] | 339 | } |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 340 | |
| 341 | do { |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 342 | ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, &status)); |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 343 | if (unlikely(ret)) |
| 344 | break; |
Bob Nelson | 36aaccc | 2007-07-20 21:39:52 +0200 | [diff] [blame] | 345 | spu = ctx->spu; |
| 346 | if (unlikely(test_and_clear_bit(SPU_SCHED_NOTIFY_ACTIVE, |
| 347 | &ctx->sched_flags))) { |
| 348 | if (!(status & SPU_STATUS_STOPPED_BY_STOP)) { |
| 349 | spu_switch_notify(spu, ctx); |
| 350 | continue; |
| 351 | } |
| 352 | } |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 353 | |
| 354 | spuctx_switch_state(ctx, SPU_UTIL_SYSTEM); |
| 355 | |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 356 | if ((status & SPU_STATUS_STOPPED_BY_STOP) && |
| 357 | (status >> SPU_STOP_STATUS_SHIFT == 0x2104)) { |
Arnd Bergmann | 2dd1493 | 2006-03-23 00:00:09 +0100 | [diff] [blame] | 358 | ret = spu_process_callback(ctx); |
| 359 | if (ret) |
| 360 | break; |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 361 | status &= ~SPU_STATUS_STOPPED_BY_STOP; |
Arnd Bergmann | 2dd1493 | 2006-03-23 00:00:09 +0100 | [diff] [blame] | 362 | } |
Arnd Bergmann | 57dace2 | 2007-04-23 21:08:15 +0200 | [diff] [blame] | 363 | ret = spufs_handle_class1(ctx); |
| 364 | if (ret) |
| 365 | break; |
| 366 | |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 367 | if (unlikely(ctx->state != SPU_STATE_RUNNABLE)) { |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 368 | ret = spu_reacquire_runnable(ctx, npc, &status); |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 369 | if (ret) |
Masato Noguchi | 2ebb247 | 2006-11-20 18:45:04 +0100 | [diff] [blame] | 370 | goto out2; |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 371 | continue; |
| 372 | } |
| 373 | ret = spu_process_events(ctx); |
| 374 | |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 375 | } while (!ret && !(status & (SPU_STATUS_STOPPED_BY_STOP | |
Benjamin Herrenschmidt | 0516923 | 2007-06-04 15:15:37 +1000 | [diff] [blame] | 376 | SPU_STATUS_STOPPED_BY_HALT | |
| 377 | SPU_STATUS_SINGLE_STEP))); |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 378 | |
Christoph Hellwig | e9f8a0b | 2007-06-29 10:58:03 +1000 | [diff] [blame] | 379 | if ((status & SPU_STATUS_STOPPED_BY_STOP) && |
Christoph Hellwig | fe2f896 | 2007-06-29 10:58:07 +1000 | [diff] [blame] | 380 | (((status >> SPU_STOP_STATUS_SHIFT) & 0x3f00) == 0x2100) && |
| 381 | (ctx->state == SPU_STATE_RUNNABLE)) |
Christoph Hellwig | e9f8a0b | 2007-06-29 10:58:03 +1000 | [diff] [blame] | 382 | ctx->stats.libassist++; |
| 383 | |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 384 | |
Masato Noguchi | c25620d | 2007-12-05 13:49:31 +1100 | [diff] [blame] | 385 | spu_disable_spu(ctx); |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 386 | ret = spu_run_fini(ctx, npc, &status); |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 387 | spu_yield(ctx); |
| 388 | |
Masato Noguchi | 2ebb247 | 2006-11-20 18:45:04 +0100 | [diff] [blame] | 389 | out2: |
| 390 | if ((ret == 0) || |
| 391 | ((ret == -ERESTARTSYS) && |
| 392 | ((status & SPU_STATUS_STOPPED_BY_HALT) || |
Benjamin Herrenschmidt | 0516923 | 2007-06-04 15:15:37 +1000 | [diff] [blame] | 393 | (status & SPU_STATUS_SINGLE_STEP) || |
Masato Noguchi | 2ebb247 | 2006-11-20 18:45:04 +0100 | [diff] [blame] | 394 | ((status & SPU_STATUS_STOPPED_BY_STOP) && |
| 395 | (status >> SPU_STOP_STATUS_SHIFT != 0x2104))))) |
| 396 | ret = status; |
| 397 | |
Benjamin Herrenschmidt | 0516923 | 2007-06-04 15:15:37 +1000 | [diff] [blame] | 398 | /* Note: we don't need to force_sig SIGTRAP on single-step |
| 399 | * since we have TIF_SINGLESTEP set, thus the kernel will do |
| 400 | * it upon return from the syscall anyawy |
| 401 | */ |
Arnd Bergmann | c2b2226 | 2006-11-27 19:18:53 +0100 | [diff] [blame] | 402 | if ((status & SPU_STATUS_STOPPED_BY_STOP) |
| 403 | && (status >> SPU_STOP_STATUS_SHIFT) == 0x3fff) { |
| 404 | force_sig(SIGTRAP, current); |
| 405 | ret = -ERESTARTSYS; |
Masato Noguchi | 2ebb247 | 2006-11-20 18:45:04 +0100 | [diff] [blame] | 406 | } |
| 407 | |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 408 | out: |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 409 | *event = ctx->event_return; |
Christoph Hellwig | e45d48a3 | 2007-04-23 21:08:17 +0200 | [diff] [blame] | 410 | mutex_unlock(&ctx->run_mutex); |
Arnd Bergmann | ce8ab85 | 2006-01-04 20:31:29 +0100 | [diff] [blame] | 411 | return ret; |
| 412 | } |