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