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 | { |
| 74 | if (ctx->policy == SCHED_RR) |
| 75 | queue_delayed_work(spu_sched_wq, &ctx->sched_work, SPU_TIMESLICE); |
| 76 | } |
| 77 | |
| 78 | void spu_stop_tick(struct spu_context *ctx) |
| 79 | { |
| 80 | if (ctx->policy == SCHED_RR) |
| 81 | cancel_delayed_work(&ctx->sched_work); |
| 82 | } |
| 83 | |
| 84 | void spu_sched_tick(struct work_struct *work) |
| 85 | { |
| 86 | struct spu_context *ctx = |
| 87 | container_of(work, struct spu_context, sched_work.work); |
| 88 | struct spu *spu; |
| 89 | int rearm = 1; |
| 90 | |
| 91 | mutex_lock(&ctx->state_mutex); |
| 92 | spu = ctx->spu; |
| 93 | if (spu) { |
| 94 | int best = sched_find_first_bit(spu_prio->bitmap); |
| 95 | if (best <= ctx->prio) { |
| 96 | spu_deactivate(ctx); |
| 97 | rearm = 0; |
| 98 | } |
| 99 | } |
| 100 | mutex_unlock(&ctx->state_mutex); |
| 101 | |
| 102 | if (rearm) |
| 103 | spu_start_tick(ctx); |
| 104 | } |
| 105 | |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 106 | /** |
| 107 | * spu_add_to_active_list - add spu to active list |
| 108 | * @spu: spu to add to the active list |
| 109 | */ |
| 110 | static void spu_add_to_active_list(struct spu *spu) |
| 111 | { |
| 112 | mutex_lock(&spu_prio->active_mutex[spu->node]); |
| 113 | list_add_tail(&spu->list, &spu_prio->active_list[spu->node]); |
| 114 | mutex_unlock(&spu_prio->active_mutex[spu->node]); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * spu_remove_from_active_list - remove spu from active list |
| 119 | * @spu: spu to remove from the active list |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 120 | */ |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 121 | static void spu_remove_from_active_list(struct spu *spu) |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 122 | { |
| 123 | int node = spu->node; |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 124 | |
| 125 | mutex_lock(&spu_prio->active_mutex[node]); |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 126 | list_del_init(&spu->list); |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 127 | mutex_unlock(&spu_prio->active_mutex[node]); |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 130 | static inline void mm_needs_global_tlbie(struct mm_struct *mm) |
| 131 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 132 | int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1; |
| 133 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 134 | /* Global TLBIE broadcast required with SPEs. */ |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 135 | __cpus_setall(&mm->cpu_vm_mask, nr); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 136 | } |
| 137 | |
Arnd Bergmann | 8676727 | 2006-10-04 17:26:21 +0200 | [diff] [blame] | 138 | static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier); |
| 139 | |
| 140 | static void spu_switch_notify(struct spu *spu, struct spu_context *ctx) |
| 141 | { |
| 142 | blocking_notifier_call_chain(&spu_switch_notifier, |
| 143 | ctx ? ctx->object_id : 0, spu); |
| 144 | } |
| 145 | |
| 146 | int spu_switch_event_register(struct notifier_block * n) |
| 147 | { |
| 148 | return blocking_notifier_chain_register(&spu_switch_notifier, n); |
| 149 | } |
| 150 | |
| 151 | int spu_switch_event_unregister(struct notifier_block * n) |
| 152 | { |
| 153 | return blocking_notifier_chain_unregister(&spu_switch_notifier, n); |
| 154 | } |
| 155 | |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 156 | /** |
| 157 | * spu_bind_context - bind spu context to physical spu |
| 158 | * @spu: physical spu to bind to |
| 159 | * @ctx: context to bind |
| 160 | */ |
| 161 | static void spu_bind_context(struct spu *spu, struct spu_context *ctx) |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 162 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 163 | pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid, |
| 164 | spu->number, spu->node); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 165 | spu->ctx = ctx; |
| 166 | spu->flags = 0; |
| 167 | ctx->spu = spu; |
| 168 | ctx->ops = &spu_hw_ops; |
| 169 | spu->pid = current->pid; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 170 | spu->mm = ctx->owner; |
| 171 | mm_needs_global_tlbie(spu->mm); |
| 172 | spu->ibox_callback = spufs_ibox_callback; |
| 173 | spu->wbox_callback = spufs_wbox_callback; |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 174 | spu->stop_callback = spufs_stop_callback; |
Arnd Bergmann | a33a7d7 | 2006-03-23 00:00:11 +0100 | [diff] [blame] | 175 | spu->mfc_callback = spufs_mfc_callback; |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 176 | spu->dma_callback = spufs_dma_callback; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 177 | mb(); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 178 | spu_unmap_mappings(ctx); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 179 | spu_restore(&ctx->csa, spu); |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 180 | spu->timestamp = jiffies; |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 181 | spu_cpu_affinity_set(spu, raw_smp_processor_id()); |
Arnd Bergmann | 8676727 | 2006-10-04 17:26:21 +0200 | [diff] [blame] | 182 | spu_switch_notify(spu, ctx); |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 183 | spu_add_to_active_list(spu); |
Christoph Hellwig | 81998ba | 2007-02-13 21:36:48 +0100 | [diff] [blame] | 184 | ctx->state = SPU_STATE_RUNNABLE; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 185 | } |
| 186 | |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 187 | /** |
| 188 | * spu_unbind_context - unbind spu context from physical spu |
| 189 | * @spu: physical spu to unbind from |
| 190 | * @ctx: context to unbind |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 191 | */ |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 192 | static void spu_unbind_context(struct spu *spu, struct spu_context *ctx) |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 193 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 194 | pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__, |
| 195 | spu->pid, spu->number, spu->node); |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 196 | |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 197 | spu_remove_from_active_list(spu); |
Arnd Bergmann | 8676727 | 2006-10-04 17:26:21 +0200 | [diff] [blame] | 198 | spu_switch_notify(spu, NULL); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 199 | spu_unmap_mappings(ctx); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 200 | spu_save(&ctx->csa, spu); |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 201 | spu->timestamp = jiffies; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 202 | ctx->state = SPU_STATE_SAVED; |
| 203 | spu->ibox_callback = NULL; |
| 204 | spu->wbox_callback = NULL; |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 205 | spu->stop_callback = NULL; |
Arnd Bergmann | a33a7d7 | 2006-03-23 00:00:11 +0100 | [diff] [blame] | 206 | spu->mfc_callback = NULL; |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 207 | spu->dma_callback = NULL; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 208 | spu->mm = NULL; |
| 209 | spu->pid = 0; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 210 | ctx->ops = &spu_backing_ops; |
| 211 | ctx->spu = NULL; |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 212 | spu->flags = 0; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 213 | spu->ctx = NULL; |
| 214 | } |
| 215 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 216 | /** |
| 217 | * spu_add_to_rq - add a context to the runqueue |
| 218 | * @ctx: context to add |
| 219 | */ |
| 220 | static void spu_add_to_rq(struct spu_context *ctx) |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 221 | { |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 222 | spin_lock(&spu_prio->runq_lock); |
| 223 | list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]); |
| 224 | set_bit(ctx->prio, spu_prio->bitmap); |
| 225 | spin_unlock(&spu_prio->runq_lock); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 226 | } |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 227 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 228 | /** |
| 229 | * spu_del_from_rq - remove a context from the runqueue |
| 230 | * @ctx: context to remove |
| 231 | */ |
| 232 | static void spu_del_from_rq(struct spu_context *ctx) |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 233 | { |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 234 | spin_lock(&spu_prio->runq_lock); |
| 235 | list_del_init(&ctx->rq); |
| 236 | if (list_empty(&spu_prio->runq[ctx->prio])) |
| 237 | clear_bit(ctx->prio, spu_prio->bitmap); |
| 238 | spin_unlock(&spu_prio->runq_lock); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 241 | /** |
| 242 | * spu_grab_context - remove one context from the runqueue |
| 243 | * @prio: priority of the context to be removed |
| 244 | * |
| 245 | * This function removes one context from the runqueue for priority @prio. |
| 246 | * If there is more than one context with the given priority the first |
| 247 | * task on the runqueue will be taken. |
| 248 | * |
| 249 | * Returns the spu_context it just removed. |
| 250 | * |
| 251 | * Must be called with spu_prio->runq_lock held. |
| 252 | */ |
| 253 | static struct spu_context *spu_grab_context(int prio) |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 254 | { |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 255 | struct list_head *rq = &spu_prio->runq[prio]; |
| 256 | |
| 257 | if (list_empty(rq)) |
| 258 | return NULL; |
| 259 | return list_entry(rq->next, struct spu_context, rq); |
| 260 | } |
| 261 | |
| 262 | static void spu_prio_wait(struct spu_context *ctx) |
| 263 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 264 | DEFINE_WAIT(wait); |
| 265 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 266 | prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 267 | if (!signal_pending(current)) { |
Christoph Hellwig | 650f8b0 | 2007-02-13 21:36:50 +0100 | [diff] [blame] | 268 | mutex_unlock(&ctx->state_mutex); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 269 | schedule(); |
Christoph Hellwig | 650f8b0 | 2007-02-13 21:36:50 +0100 | [diff] [blame] | 270 | mutex_lock(&ctx->state_mutex); |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 271 | } |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 272 | __set_current_state(TASK_RUNNING); |
| 273 | remove_wait_queue(&ctx->stop_wq, &wait); |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 274 | } |
| 275 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 276 | /** |
| 277 | * spu_reschedule - try to find a runnable context for a spu |
| 278 | * @spu: spu available |
| 279 | * |
| 280 | * This function is called whenever a spu becomes idle. It looks for the |
| 281 | * most suitable runnable spu context and schedules it for execution. |
| 282 | */ |
| 283 | static void spu_reschedule(struct spu *spu) |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 284 | { |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 285 | int best; |
| 286 | |
| 287 | spu_free(spu); |
| 288 | |
| 289 | spin_lock(&spu_prio->runq_lock); |
| 290 | best = sched_find_first_bit(spu_prio->bitmap); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 291 | if (best < MAX_PRIO) { |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 292 | struct spu_context *ctx = spu_grab_context(best); |
Christoph Hellwig | 50b520d | 2007-03-10 00:05:36 +0100 | [diff] [blame^] | 293 | if (ctx) |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 294 | wake_up(&ctx->stop_wq); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 295 | } |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 296 | spin_unlock(&spu_prio->runq_lock); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 297 | } |
| 298 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 299 | static struct spu *spu_get_idle(struct spu_context *ctx) |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 300 | { |
| 301 | struct spu *spu = NULL; |
| 302 | int node = cpu_to_node(raw_smp_processor_id()); |
| 303 | int n; |
| 304 | |
| 305 | for (n = 0; n < MAX_NUMNODES; n++, node++) { |
| 306 | node = (node < MAX_NUMNODES) ? node : 0; |
| 307 | if (!node_allowed(node)) |
| 308 | continue; |
| 309 | spu = spu_alloc_node(node); |
| 310 | if (spu) |
| 311 | break; |
| 312 | } |
| 313 | return spu; |
| 314 | } |
| 315 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 316 | /** |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 317 | * find_victim - find a lower priority context to preempt |
| 318 | * @ctx: canidate context for running |
| 319 | * |
| 320 | * Returns the freed physical spu to run the new context on. |
| 321 | */ |
| 322 | static struct spu *find_victim(struct spu_context *ctx) |
| 323 | { |
| 324 | struct spu_context *victim = NULL; |
| 325 | struct spu *spu; |
| 326 | int node, n; |
| 327 | |
| 328 | /* |
| 329 | * Look for a possible preemption candidate on the local node first. |
| 330 | * If there is no candidate look at the other nodes. This isn't |
| 331 | * exactly fair, but so far the whole spu schedule tries to keep |
| 332 | * a strong node affinity. We might want to fine-tune this in |
| 333 | * the future. |
| 334 | */ |
| 335 | restart: |
| 336 | node = cpu_to_node(raw_smp_processor_id()); |
| 337 | for (n = 0; n < MAX_NUMNODES; n++, node++) { |
| 338 | node = (node < MAX_NUMNODES) ? node : 0; |
| 339 | if (!node_allowed(node)) |
| 340 | continue; |
| 341 | |
| 342 | mutex_lock(&spu_prio->active_mutex[node]); |
| 343 | list_for_each_entry(spu, &spu_prio->active_list[node], list) { |
| 344 | struct spu_context *tmp = spu->ctx; |
| 345 | |
| 346 | if (tmp->rt_priority < ctx->rt_priority && |
| 347 | (!victim || tmp->rt_priority < victim->rt_priority)) |
| 348 | victim = spu->ctx; |
| 349 | } |
| 350 | mutex_unlock(&spu_prio->active_mutex[node]); |
| 351 | |
| 352 | if (victim) { |
| 353 | /* |
| 354 | * This nests ctx->state_mutex, but we always lock |
| 355 | * higher priority contexts before lower priority |
| 356 | * ones, so this is safe until we introduce |
| 357 | * priority inheritance schemes. |
| 358 | */ |
| 359 | if (!mutex_trylock(&victim->state_mutex)) { |
| 360 | victim = NULL; |
| 361 | goto restart; |
| 362 | } |
| 363 | |
| 364 | spu = victim->spu; |
| 365 | if (!spu) { |
| 366 | /* |
| 367 | * This race can happen because we've dropped |
| 368 | * the active list mutex. No a problem, just |
| 369 | * restart the search. |
| 370 | */ |
| 371 | mutex_unlock(&victim->state_mutex); |
| 372 | victim = NULL; |
| 373 | goto restart; |
| 374 | } |
| 375 | spu_unbind_context(spu, victim); |
| 376 | mutex_unlock(&victim->state_mutex); |
| 377 | return spu; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | /** |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 385 | * spu_activate - find a free spu for a context and execute it |
| 386 | * @ctx: spu context to schedule |
| 387 | * @flags: flags (currently ignored) |
| 388 | * |
| 389 | * Tries to find a free spu to run @ctx. If no free spu is availble |
| 390 | * add the context to the runqueue so it gets woken up once an spu |
| 391 | * is available. |
| 392 | */ |
Christoph Hellwig | 26bec67 | 2007-02-13 21:54:24 +0100 | [diff] [blame] | 393 | int spu_activate(struct spu_context *ctx, unsigned long flags) |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 394 | { |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 395 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 396 | if (ctx->spu) |
| 397 | return 0; |
| 398 | |
| 399 | do { |
| 400 | struct spu *spu; |
| 401 | |
| 402 | spu = spu_get_idle(ctx); |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 403 | /* |
| 404 | * If this is a realtime thread we try to get it running by |
| 405 | * preempting a lower priority thread. |
| 406 | */ |
| 407 | if (!spu && ctx->rt_priority) |
| 408 | spu = find_victim(ctx); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 409 | if (spu) { |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 410 | spu_bind_context(spu, ctx); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 411 | return 0; |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 412 | } |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 413 | |
| 414 | spu_add_to_rq(ctx); |
Christoph Hellwig | 50b520d | 2007-03-10 00:05:36 +0100 | [diff] [blame^] | 415 | spu_prio_wait(ctx); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 416 | spu_del_from_rq(ctx); |
| 417 | } while (!signal_pending(current)); |
| 418 | |
| 419 | return -ERESTARTSYS; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 420 | } |
| 421 | |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 422 | /** |
| 423 | * spu_deactivate - unbind a context from it's physical spu |
| 424 | * @ctx: spu context to unbind |
| 425 | * |
| 426 | * Unbind @ctx from the physical spu it is running on and schedule |
| 427 | * the highest priority context to run on the freed physical spu. |
| 428 | */ |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 429 | void spu_deactivate(struct spu_context *ctx) |
| 430 | { |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 431 | struct spu *spu = ctx->spu; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 432 | |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 433 | if (spu) { |
| 434 | spu_unbind_context(spu, ctx); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 435 | spu_reschedule(spu); |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 436 | } |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 437 | } |
| 438 | |
Christoph Hellwig | ae7b4c5 | 2007-02-13 21:54:26 +0100 | [diff] [blame] | 439 | /** |
| 440 | * spu_yield - yield a physical spu if others are waiting |
| 441 | * @ctx: spu context to yield |
| 442 | * |
| 443 | * Check if there is a higher priority context waiting and if yes |
| 444 | * unbind @ctx from the physical spu and schedule the highest |
| 445 | * priority context to run on the freed physical spu instead. |
| 446 | */ |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 447 | void spu_yield(struct spu_context *ctx) |
| 448 | { |
| 449 | struct spu *spu; |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 450 | int need_yield = 0; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 451 | |
Christoph Hellwig | 650f8b0 | 2007-02-13 21:36:50 +0100 | [diff] [blame] | 452 | if (mutex_trylock(&ctx->state_mutex)) { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 453 | if ((spu = ctx->spu) != NULL) { |
| 454 | int best = sched_find_first_bit(spu_prio->bitmap); |
| 455 | if (best < MAX_PRIO) { |
| 456 | pr_debug("%s: yielding SPU %d NODE %d\n", |
| 457 | __FUNCTION__, spu->number, spu->node); |
| 458 | spu_deactivate(ctx); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 459 | need_yield = 1; |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 460 | } |
| 461 | } |
Christoph Hellwig | 650f8b0 | 2007-02-13 21:36:50 +0100 | [diff] [blame] | 462 | mutex_unlock(&ctx->state_mutex); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 463 | } |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 464 | if (unlikely(need_yield)) |
| 465 | yield(); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | int __init spu_sched_init(void) |
| 469 | { |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 470 | int i; |
| 471 | |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 472 | spu_sched_wq = create_singlethread_workqueue("spusched"); |
| 473 | if (!spu_sched_wq) |
| 474 | return 1; |
| 475 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 476 | spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL); |
| 477 | if (!spu_prio) { |
| 478 | printk(KERN_WARNING "%s: Unable to allocate priority queue.\n", |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 479 | __FUNCTION__); |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 480 | destroy_workqueue(spu_sched_wq); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 481 | return 1; |
| 482 | } |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 483 | for (i = 0; i < MAX_PRIO; i++) { |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 484 | INIT_LIST_HEAD(&spu_prio->runq[i]); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 485 | __clear_bit(i, spu_prio->bitmap); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 486 | } |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 487 | __set_bit(MAX_PRIO, spu_prio->bitmap); |
| 488 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 489 | mutex_init(&spu_prio->active_mutex[i]); |
| 490 | INIT_LIST_HEAD(&spu_prio->active_list[i]); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 491 | } |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 492 | spin_lock_init(&spu_prio->runq_lock); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | void __exit spu_sched_exit(void) |
| 497 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 498 | struct spu *spu, *tmp; |
| 499 | int node; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 500 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 501 | for (node = 0; node < MAX_NUMNODES; node++) { |
| 502 | mutex_lock(&spu_prio->active_mutex[node]); |
| 503 | list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node], |
| 504 | list) { |
| 505 | list_del_init(&spu->list); |
| 506 | spu_free(spu); |
| 507 | } |
| 508 | mutex_unlock(&spu_prio->active_mutex[node]); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 509 | } |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 510 | kfree(spu_prio); |
Christoph Hellwig | 2eb1b12 | 2007-02-13 21:54:29 +0100 | [diff] [blame] | 511 | destroy_workqueue(spu_sched_wq); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 512 | } |