Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 1 | /* sched.c - SPU scheduler. |
| 2 | * |
| 3 | * Copyright (C) IBM 2005 |
| 4 | * Author: Mark Nutter <mnutter@us.ibm.com> |
| 5 | * |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 6 | * 2006-03-31 NUMA domains added. |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | */ |
| 22 | |
Arnd Bergmann | 3b3d22c | 2005-12-05 22:52:24 -0500 | [diff] [blame] | 23 | #undef DEBUG |
| 24 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 25 | #include <linux/module.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/sched.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/mm.h> |
| 30 | #include <linux/completion.h> |
| 31 | #include <linux/vmalloc.h> |
| 32 | #include <linux/smp.h> |
| 33 | #include <linux/smp_lock.h> |
| 34 | #include <linux/stddef.h> |
| 35 | #include <linux/unistd.h> |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 36 | #include <linux/numa.h> |
| 37 | #include <linux/mutex.h> |
Arnd Bergmann | 8676727 | 2006-10-04 17:26:21 +0200 | [diff] [blame] | 38 | #include <linux/notifier.h> |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 39 | |
| 40 | #include <asm/io.h> |
| 41 | #include <asm/mmu_context.h> |
| 42 | #include <asm/spu.h> |
| 43 | #include <asm/spu_csa.h> |
Geoff Levand | a91942a | 2006-06-19 20:33:30 +0200 | [diff] [blame] | 44 | #include <asm/spu_priv1.h> |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 45 | #include "spufs.h" |
| 46 | |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 47 | #define SPU_TIMESLICE (HZ) |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 48 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 49 | struct spu_prio_array { |
Christoph Hellwig | 72cb360 | 2007-02-13 21:54:28 +0100 | [diff] [blame] | 50 | DECLARE_BITMAP(bitmap, MAX_PRIO); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 51 | struct list_head runq[MAX_PRIO]; |
| 52 | spinlock_t runq_lock; |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 53 | struct list_head active_list[MAX_NUMNODES]; |
| 54 | struct mutex active_mutex[MAX_NUMNODES]; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 55 | }; |
| 56 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 57 | static struct spu_prio_array *spu_prio; |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 58 | static struct workqueue_struct *spu_sched_wq; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 59 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 60 | static inline int node_allowed(int node) |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 61 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 62 | cpumask_t mask; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 63 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 64 | if (!nr_cpus_node(node)) |
| 65 | return 0; |
| 66 | mask = node_to_cpumask(node); |
| 67 | if (!cpus_intersects(mask, current->cpus_allowed)) |
| 68 | return 0; |
| 69 | return 1; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 70 | } |
| 71 | |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 72 | void spu_start_tick(struct spu_context *ctx) |
| 73 | { |
Christoph Hellwig | 0887309 | 2007-04-23 21:08:06 +0200 | [diff] [blame] | 74 | if (ctx->policy == SCHED_RR) { |
| 75 | /* |
| 76 | * Make sure the exiting bit is cleared. |
| 77 | */ |
| 78 | clear_bit(SPU_SCHED_EXITING, &ctx->sched_flags); |
Arnd Bergmann | 390c534 | 2007-04-23 21:08:10 +0200 | [diff] [blame] | 79 | mb(); |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 80 | queue_delayed_work(spu_sched_wq, &ctx->sched_work, SPU_TIMESLICE); |
Christoph Hellwig | 0887309 | 2007-04-23 21:08:06 +0200 | [diff] [blame] | 81 | } |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void spu_stop_tick(struct spu_context *ctx) |
| 85 | { |
Christoph Hellwig | 0887309 | 2007-04-23 21:08:06 +0200 | [diff] [blame] | 86 | if (ctx->policy == SCHED_RR) { |
| 87 | /* |
| 88 | * While the work can be rearming normally setting this flag |
| 89 | * makes sure it does not rearm itself anymore. |
| 90 | */ |
| 91 | set_bit(SPU_SCHED_EXITING, &ctx->sched_flags); |
Arnd Bergmann | 390c534 | 2007-04-23 21:08:10 +0200 | [diff] [blame] | 92 | mb(); |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 93 | cancel_delayed_work(&ctx->sched_work); |
Christoph Hellwig | 0887309 | 2007-04-23 21:08:06 +0200 | [diff] [blame] | 94 | } |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void spu_sched_tick(struct work_struct *work) |
| 98 | { |
| 99 | struct spu_context *ctx = |
| 100 | container_of(work, struct spu_context, sched_work.work); |
| 101 | struct spu *spu; |
Christoph Hellwig | b3e76cc | 2007-04-23 21:08:08 +0200 | [diff] [blame] | 102 | int preempted = 0; |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 103 | |
Christoph Hellwig | 0887309 | 2007-04-23 21:08:06 +0200 | [diff] [blame] | 104 | /* |
| 105 | * If this context is being stopped avoid rescheduling from the |
| 106 | * scheduler tick because we would block on the state_mutex. |
| 107 | * The caller will yield the spu later on anyway. |
| 108 | */ |
| 109 | if (test_bit(SPU_SCHED_EXITING, &ctx->sched_flags)) |
| 110 | return; |
| 111 | |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 112 | mutex_lock(&ctx->state_mutex); |
| 113 | spu = ctx->spu; |
| 114 | if (spu) { |
| 115 | int best = sched_find_first_bit(spu_prio->bitmap); |
| 116 | if (best <= ctx->prio) { |
| 117 | spu_deactivate(ctx); |
Christoph Hellwig | b3e76cc | 2007-04-23 21:08:08 +0200 | [diff] [blame] | 118 | preempted = 1; |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | mutex_unlock(&ctx->state_mutex); |
| 122 | |
Christoph Hellwig | b3e76cc | 2007-04-23 21:08:08 +0200 | [diff] [blame] | 123 | if (preempted) { |
| 124 | /* |
| 125 | * We need to break out of the wait loop in spu_run manually |
| 126 | * to ensure this context gets put on the runqueue again |
| 127 | * ASAP. |
| 128 | */ |
| 129 | wake_up(&ctx->stop_wq); |
| 130 | } else |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 131 | spu_start_tick(ctx); |
| 132 | } |
| 133 | |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 134 | /** |
| 135 | * spu_add_to_active_list - add spu to active list |
| 136 | * @spu: spu to add to the active list |
| 137 | */ |
| 138 | static void spu_add_to_active_list(struct spu *spu) |
| 139 | { |
| 140 | mutex_lock(&spu_prio->active_mutex[spu->node]); |
| 141 | list_add_tail(&spu->list, &spu_prio->active_list[spu->node]); |
| 142 | mutex_unlock(&spu_prio->active_mutex[spu->node]); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * spu_remove_from_active_list - remove spu from active list |
| 147 | * @spu: spu to remove from the active list |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 148 | */ |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 149 | static void spu_remove_from_active_list(struct spu *spu) |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 150 | { |
| 151 | int node = spu->node; |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 152 | |
| 153 | mutex_lock(&spu_prio->active_mutex[node]); |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 154 | list_del_init(&spu->list); |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 155 | mutex_unlock(&spu_prio->active_mutex[node]); |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Arnd Bergmann | 8676727 | 2006-10-04 17:26:21 +0200 | [diff] [blame] | 158 | static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier); |
| 159 | |
| 160 | static void spu_switch_notify(struct spu *spu, struct spu_context *ctx) |
| 161 | { |
| 162 | blocking_notifier_call_chain(&spu_switch_notifier, |
| 163 | ctx ? ctx->object_id : 0, spu); |
| 164 | } |
| 165 | |
| 166 | int spu_switch_event_register(struct notifier_block * n) |
| 167 | { |
| 168 | return blocking_notifier_chain_register(&spu_switch_notifier, n); |
| 169 | } |
| 170 | |
| 171 | int spu_switch_event_unregister(struct notifier_block * n) |
| 172 | { |
| 173 | return blocking_notifier_chain_unregister(&spu_switch_notifier, n); |
| 174 | } |
| 175 | |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 176 | /** |
| 177 | * spu_bind_context - bind spu context to physical spu |
| 178 | * @spu: physical spu to bind to |
| 179 | * @ctx: context to bind |
| 180 | */ |
| 181 | static void spu_bind_context(struct spu *spu, struct spu_context *ctx) |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 182 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 183 | pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid, |
| 184 | spu->number, spu->node); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 185 | spu->ctx = ctx; |
| 186 | spu->flags = 0; |
| 187 | ctx->spu = spu; |
| 188 | ctx->ops = &spu_hw_ops; |
| 189 | spu->pid = current->pid; |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 190 | spu_associate_mm(spu, ctx->owner); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 191 | spu->ibox_callback = spufs_ibox_callback; |
| 192 | spu->wbox_callback = spufs_wbox_callback; |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 193 | spu->stop_callback = spufs_stop_callback; |
Arnd Bergmann | a33a7d7 | 2006-03-23 00:00:11 +0100 | [diff] [blame] | 194 | spu->mfc_callback = spufs_mfc_callback; |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 195 | spu->dma_callback = spufs_dma_callback; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 196 | mb(); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 197 | spu_unmap_mappings(ctx); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 198 | spu_restore(&ctx->csa, spu); |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 199 | spu->timestamp = jiffies; |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 200 | spu_cpu_affinity_set(spu, raw_smp_processor_id()); |
Arnd Bergmann | 8676727 | 2006-10-04 17:26:21 +0200 | [diff] [blame] | 201 | spu_switch_notify(spu, ctx); |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 202 | spu_add_to_active_list(spu); |
Christoph Hellwig | 81998ba | 2007-02-13 21:36:48 +0100 | [diff] [blame] | 203 | ctx->state = SPU_STATE_RUNNABLE; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 204 | } |
| 205 | |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 206 | /** |
| 207 | * spu_unbind_context - unbind spu context from physical spu |
| 208 | * @spu: physical spu to unbind from |
| 209 | * @ctx: context to unbind |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 210 | */ |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 211 | static void spu_unbind_context(struct spu *spu, struct spu_context *ctx) |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 212 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 213 | pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__, |
| 214 | spu->pid, spu->number, spu->node); |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 215 | |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 216 | spu_remove_from_active_list(spu); |
Arnd Bergmann | 8676727 | 2006-10-04 17:26:21 +0200 | [diff] [blame] | 217 | spu_switch_notify(spu, NULL); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 218 | spu_unmap_mappings(ctx); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 219 | spu_save(&ctx->csa, spu); |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 220 | spu->timestamp = jiffies; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 221 | ctx->state = SPU_STATE_SAVED; |
| 222 | spu->ibox_callback = NULL; |
| 223 | spu->wbox_callback = NULL; |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 224 | spu->stop_callback = NULL; |
Arnd Bergmann | a33a7d7 | 2006-03-23 00:00:11 +0100 | [diff] [blame] | 225 | spu->mfc_callback = NULL; |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 226 | spu->dma_callback = NULL; |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 227 | spu_associate_mm(spu, NULL); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 228 | spu->pid = 0; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 229 | ctx->ops = &spu_backing_ops; |
| 230 | ctx->spu = NULL; |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 231 | spu->flags = 0; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 232 | spu->ctx = NULL; |
| 233 | } |
| 234 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 235 | /** |
| 236 | * spu_add_to_rq - add a context to the runqueue |
| 237 | * @ctx: context to add |
| 238 | */ |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame^] | 239 | static void __spu_add_to_rq(struct spu_context *ctx) |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 240 | { |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame^] | 241 | int prio = ctx->prio; |
| 242 | |
| 243 | list_add_tail(&ctx->rq, &spu_prio->runq[prio]); |
| 244 | set_bit(prio, spu_prio->bitmap); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 245 | } |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 246 | |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame^] | 247 | static void __spu_del_from_rq(struct spu_context *ctx) |
Christoph Hellwig | a475c2f | 2007-04-23 21:08:11 +0200 | [diff] [blame] | 248 | { |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame^] | 249 | int prio = ctx->prio; |
| 250 | |
Christoph Hellwig | a475c2f | 2007-04-23 21:08:11 +0200 | [diff] [blame] | 251 | if (!list_empty(&ctx->rq)) |
| 252 | list_del_init(&ctx->rq); |
| 253 | if (list_empty(&spu_prio->runq[prio])) |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame^] | 254 | clear_bit(prio, spu_prio->bitmap); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 257 | static void spu_prio_wait(struct spu_context *ctx) |
| 258 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 259 | DEFINE_WAIT(wait); |
| 260 | |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame^] | 261 | spin_lock(&spu_prio->runq_lock); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 262 | prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 263 | if (!signal_pending(current)) { |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame^] | 264 | __spu_add_to_rq(ctx); |
| 265 | spin_unlock(&spu_prio->runq_lock); |
Christoph Hellwig | 650f8b0 | 2007-02-13 21:36:50 +0100 | [diff] [blame] | 266 | mutex_unlock(&ctx->state_mutex); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 267 | schedule(); |
Christoph Hellwig | 650f8b0 | 2007-02-13 21:36:50 +0100 | [diff] [blame] | 268 | mutex_lock(&ctx->state_mutex); |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame^] | 269 | spin_lock(&spu_prio->runq_lock); |
| 270 | __spu_del_from_rq(ctx); |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 271 | } |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame^] | 272 | spin_unlock(&spu_prio->runq_lock); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 273 | __set_current_state(TASK_RUNNING); |
| 274 | remove_wait_queue(&ctx->stop_wq, &wait); |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 275 | } |
| 276 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 277 | /** |
| 278 | * spu_reschedule - try to find a runnable context for a spu |
| 279 | * @spu: spu available |
| 280 | * |
| 281 | * This function is called whenever a spu becomes idle. It looks for the |
| 282 | * most suitable runnable spu context and schedules it for execution. |
| 283 | */ |
| 284 | static void spu_reschedule(struct spu *spu) |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 285 | { |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 286 | int best; |
| 287 | |
| 288 | spu_free(spu); |
| 289 | |
| 290 | spin_lock(&spu_prio->runq_lock); |
| 291 | best = sched_find_first_bit(spu_prio->bitmap); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 292 | if (best < MAX_PRIO) { |
Christoph Hellwig | a475c2f | 2007-04-23 21:08:11 +0200 | [diff] [blame] | 293 | struct list_head *rq = &spu_prio->runq[best]; |
| 294 | struct spu_context *ctx; |
| 295 | |
| 296 | BUG_ON(list_empty(rq)); |
| 297 | |
| 298 | ctx = list_entry(rq->next, struct spu_context, rq); |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame^] | 299 | __spu_del_from_rq(ctx); |
Christoph Hellwig | a475c2f | 2007-04-23 21:08:11 +0200 | [diff] [blame] | 300 | wake_up(&ctx->stop_wq); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 301 | } |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 302 | spin_unlock(&spu_prio->runq_lock); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 303 | } |
| 304 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 305 | static struct spu *spu_get_idle(struct spu_context *ctx) |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 306 | { |
| 307 | struct spu *spu = NULL; |
| 308 | int node = cpu_to_node(raw_smp_processor_id()); |
| 309 | int n; |
| 310 | |
| 311 | for (n = 0; n < MAX_NUMNODES; n++, node++) { |
| 312 | node = (node < MAX_NUMNODES) ? node : 0; |
| 313 | if (!node_allowed(node)) |
| 314 | continue; |
| 315 | spu = spu_alloc_node(node); |
| 316 | if (spu) |
| 317 | break; |
| 318 | } |
| 319 | return spu; |
| 320 | } |
| 321 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 322 | /** |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 323 | * find_victim - find a lower priority context to preempt |
| 324 | * @ctx: canidate context for running |
| 325 | * |
| 326 | * Returns the freed physical spu to run the new context on. |
| 327 | */ |
| 328 | static struct spu *find_victim(struct spu_context *ctx) |
| 329 | { |
| 330 | struct spu_context *victim = NULL; |
| 331 | struct spu *spu; |
| 332 | int node, n; |
| 333 | |
| 334 | /* |
| 335 | * Look for a possible preemption candidate on the local node first. |
| 336 | * If there is no candidate look at the other nodes. This isn't |
| 337 | * exactly fair, but so far the whole spu schedule tries to keep |
| 338 | * a strong node affinity. We might want to fine-tune this in |
| 339 | * the future. |
| 340 | */ |
| 341 | restart: |
| 342 | node = cpu_to_node(raw_smp_processor_id()); |
| 343 | for (n = 0; n < MAX_NUMNODES; n++, node++) { |
| 344 | node = (node < MAX_NUMNODES) ? node : 0; |
| 345 | if (!node_allowed(node)) |
| 346 | continue; |
| 347 | |
| 348 | mutex_lock(&spu_prio->active_mutex[node]); |
| 349 | list_for_each_entry(spu, &spu_prio->active_list[node], list) { |
| 350 | struct spu_context *tmp = spu->ctx; |
| 351 | |
| 352 | if (tmp->rt_priority < ctx->rt_priority && |
| 353 | (!victim || tmp->rt_priority < victim->rt_priority)) |
| 354 | victim = spu->ctx; |
| 355 | } |
| 356 | mutex_unlock(&spu_prio->active_mutex[node]); |
| 357 | |
| 358 | if (victim) { |
| 359 | /* |
| 360 | * This nests ctx->state_mutex, but we always lock |
| 361 | * higher priority contexts before lower priority |
| 362 | * ones, so this is safe until we introduce |
| 363 | * priority inheritance schemes. |
| 364 | */ |
| 365 | if (!mutex_trylock(&victim->state_mutex)) { |
| 366 | victim = NULL; |
| 367 | goto restart; |
| 368 | } |
| 369 | |
| 370 | spu = victim->spu; |
| 371 | if (!spu) { |
| 372 | /* |
| 373 | * This race can happen because we've dropped |
| 374 | * the active list mutex. No a problem, just |
| 375 | * restart the search. |
| 376 | */ |
| 377 | mutex_unlock(&victim->state_mutex); |
| 378 | victim = NULL; |
| 379 | goto restart; |
| 380 | } |
| 381 | spu_unbind_context(spu, victim); |
| 382 | mutex_unlock(&victim->state_mutex); |
Christoph Hellwig | e097b51 | 2007-04-23 21:08:09 +0200 | [diff] [blame] | 383 | /* |
| 384 | * We need to break out of the wait loop in spu_run |
| 385 | * manually to ensure this context gets put on the |
| 386 | * runqueue again ASAP. |
| 387 | */ |
| 388 | wake_up(&victim->stop_wq); |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 389 | return spu; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | return NULL; |
| 394 | } |
| 395 | |
| 396 | /** |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 397 | * spu_activate - find a free spu for a context and execute it |
| 398 | * @ctx: spu context to schedule |
| 399 | * @flags: flags (currently ignored) |
| 400 | * |
Christoph Hellwig | 0887309 | 2007-04-23 21:08:06 +0200 | [diff] [blame] | 401 | * Tries to find a free spu to run @ctx. If no free spu is available |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 402 | * add the context to the runqueue so it gets woken up once an spu |
| 403 | * is available. |
| 404 | */ |
Christoph Hellwig | 26bec67 | 2007-02-13 21:54:24 +0100 | [diff] [blame] | 405 | int spu_activate(struct spu_context *ctx, unsigned long flags) |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 406 | { |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 407 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 408 | if (ctx->spu) |
| 409 | return 0; |
| 410 | |
| 411 | do { |
| 412 | struct spu *spu; |
| 413 | |
| 414 | spu = spu_get_idle(ctx); |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 415 | /* |
| 416 | * If this is a realtime thread we try to get it running by |
| 417 | * preempting a lower priority thread. |
| 418 | */ |
| 419 | if (!spu && ctx->rt_priority) |
| 420 | spu = find_victim(ctx); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 421 | if (spu) { |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 422 | spu_bind_context(spu, ctx); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 423 | return 0; |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 424 | } |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 425 | |
Christoph Hellwig | 50b520d | 2007-03-10 00:05:36 +0100 | [diff] [blame] | 426 | spu_prio_wait(ctx); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 427 | } while (!signal_pending(current)); |
| 428 | |
| 429 | return -ERESTARTSYS; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 430 | } |
| 431 | |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 432 | /** |
| 433 | * spu_deactivate - unbind a context from it's physical spu |
| 434 | * @ctx: spu context to unbind |
| 435 | * |
| 436 | * Unbind @ctx from the physical spu it is running on and schedule |
| 437 | * the highest priority context to run on the freed physical spu. |
| 438 | */ |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 439 | void spu_deactivate(struct spu_context *ctx) |
| 440 | { |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 441 | struct spu *spu = ctx->spu; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 442 | |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 443 | if (spu) { |
| 444 | spu_unbind_context(spu, ctx); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 445 | spu_reschedule(spu); |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 446 | } |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 447 | } |
| 448 | |
Christoph Hellwig | ae7b4c5 | 2007-02-13 21:54:26 +0100 | [diff] [blame] | 449 | /** |
| 450 | * spu_yield - yield a physical spu if others are waiting |
| 451 | * @ctx: spu context to yield |
| 452 | * |
| 453 | * Check if there is a higher priority context waiting and if yes |
| 454 | * unbind @ctx from the physical spu and schedule the highest |
| 455 | * priority context to run on the freed physical spu instead. |
| 456 | */ |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 457 | void spu_yield(struct spu_context *ctx) |
| 458 | { |
| 459 | struct spu *spu; |
| 460 | |
Christoph Hellwig | 650f8b0 | 2007-02-13 21:36:50 +0100 | [diff] [blame] | 461 | if (mutex_trylock(&ctx->state_mutex)) { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 462 | if ((spu = ctx->spu) != NULL) { |
| 463 | int best = sched_find_first_bit(spu_prio->bitmap); |
| 464 | if (best < MAX_PRIO) { |
| 465 | pr_debug("%s: yielding SPU %d NODE %d\n", |
| 466 | __FUNCTION__, spu->number, spu->node); |
| 467 | spu_deactivate(ctx); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 468 | } |
| 469 | } |
Christoph Hellwig | 650f8b0 | 2007-02-13 21:36:50 +0100 | [diff] [blame] | 470 | mutex_unlock(&ctx->state_mutex); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 471 | } |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | int __init spu_sched_init(void) |
| 475 | { |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 476 | int i; |
| 477 | |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 478 | spu_sched_wq = create_singlethread_workqueue("spusched"); |
| 479 | if (!spu_sched_wq) |
| 480 | return 1; |
| 481 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 482 | spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL); |
| 483 | if (!spu_prio) { |
| 484 | printk(KERN_WARNING "%s: Unable to allocate priority queue.\n", |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 485 | __FUNCTION__); |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 486 | destroy_workqueue(spu_sched_wq); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 487 | return 1; |
| 488 | } |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 489 | for (i = 0; i < MAX_PRIO; i++) { |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 490 | INIT_LIST_HEAD(&spu_prio->runq[i]); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 491 | __clear_bit(i, spu_prio->bitmap); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 492 | } |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 493 | __set_bit(MAX_PRIO, spu_prio->bitmap); |
| 494 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 495 | mutex_init(&spu_prio->active_mutex[i]); |
| 496 | INIT_LIST_HEAD(&spu_prio->active_list[i]); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 497 | } |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 498 | spin_lock_init(&spu_prio->runq_lock); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | void __exit spu_sched_exit(void) |
| 503 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 504 | struct spu *spu, *tmp; |
| 505 | int node; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 506 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 507 | for (node = 0; node < MAX_NUMNODES; node++) { |
| 508 | mutex_lock(&spu_prio->active_mutex[node]); |
| 509 | list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node], |
| 510 | list) { |
| 511 | list_del_init(&spu->list); |
| 512 | spu_free(spu); |
| 513 | } |
| 514 | mutex_unlock(&spu_prio->active_mutex[node]); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 515 | } |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 516 | kfree(spu_prio); |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 517 | destroy_workqueue(spu_sched_wq); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 518 | } |