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> |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 38 | #include <linux/kthread.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 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 47 | struct spu_prio_array { |
Christoph Hellwig | 72cb360 | 2007-02-13 21:54:28 +0100 | [diff] [blame] | 48 | DECLARE_BITMAP(bitmap, MAX_PRIO); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 49 | struct list_head runq[MAX_PRIO]; |
| 50 | spinlock_t runq_lock; |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 51 | struct list_head active_list[MAX_NUMNODES]; |
| 52 | struct mutex active_mutex[MAX_NUMNODES]; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 53 | }; |
| 54 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 55 | static struct spu_prio_array *spu_prio; |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 56 | static struct task_struct *spusched_task; |
| 57 | static struct timer_list spusched_timer; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 58 | |
Christoph Hellwig | fe443ef | 2007-06-29 10:57:52 +1000 | [diff] [blame^] | 59 | /* |
| 60 | * Priority of a normal, non-rt, non-niced'd process (aka nice level 0). |
| 61 | */ |
| 62 | #define NORMAL_PRIO 120 |
| 63 | |
| 64 | /* |
| 65 | * Frequency of the spu scheduler tick. By default we do one SPU scheduler |
| 66 | * tick for every 10 CPU scheduler ticks. |
| 67 | */ |
| 68 | #define SPUSCHED_TICK (10) |
| 69 | |
| 70 | /* |
| 71 | * These are the 'tuning knobs' of the scheduler: |
| 72 | * |
| 73 | * Minimum timeslice is 5 msecs (or 10 jiffies, whichever is larger), |
| 74 | * default timeslice is 100 msecs, maximum timeslice is 800 msecs. |
| 75 | */ |
| 76 | #define MIN_SPU_TIMESLICE max(5 * HZ / 100, 10) |
| 77 | #define DEF_SPU_TIMESLICE (100 * HZ / 100) |
| 78 | |
| 79 | #define MAX_USER_PRIO (MAX_PRIO - MAX_RT_PRIO) |
| 80 | #define SCALE_PRIO(x, prio) \ |
| 81 | max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_SPU_TIMESLICE) |
| 82 | |
| 83 | /* |
| 84 | * scale user-nice values [ -20 ... 0 ... 19 ] to time slice values: |
| 85 | * [800ms ... 100ms ... 5ms] |
| 86 | * |
| 87 | * The higher a thread's priority, the bigger timeslices |
| 88 | * it gets during one round of execution. But even the lowest |
| 89 | * priority thread gets MIN_TIMESLICE worth of execution time. |
| 90 | */ |
| 91 | void spu_set_timeslice(struct spu_context *ctx) |
| 92 | { |
| 93 | if (ctx->prio < NORMAL_PRIO) |
| 94 | ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE * 4, ctx->prio); |
| 95 | else |
| 96 | ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE, ctx->prio); |
| 97 | } |
| 98 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 99 | static inline int node_allowed(int node) |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 100 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 101 | cpumask_t mask; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 102 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 103 | if (!nr_cpus_node(node)) |
| 104 | return 0; |
| 105 | mask = node_to_cpumask(node); |
| 106 | if (!cpus_intersects(mask, current->cpus_allowed)) |
| 107 | return 0; |
| 108 | return 1; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 111 | /** |
| 112 | * spu_add_to_active_list - add spu to active list |
| 113 | * @spu: spu to add to the active list |
| 114 | */ |
| 115 | static void spu_add_to_active_list(struct spu *spu) |
| 116 | { |
| 117 | mutex_lock(&spu_prio->active_mutex[spu->node]); |
| 118 | list_add_tail(&spu->list, &spu_prio->active_list[spu->node]); |
| 119 | mutex_unlock(&spu_prio->active_mutex[spu->node]); |
| 120 | } |
| 121 | |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 122 | static void __spu_remove_from_active_list(struct spu *spu) |
| 123 | { |
| 124 | list_del_init(&spu->list); |
| 125 | } |
| 126 | |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 127 | /** |
| 128 | * spu_remove_from_active_list - remove spu from active list |
| 129 | * @spu: spu to remove from the active list |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 130 | */ |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 131 | static void spu_remove_from_active_list(struct spu *spu) |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 132 | { |
| 133 | int node = spu->node; |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 134 | |
| 135 | mutex_lock(&spu_prio->active_mutex[node]); |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 136 | __spu_remove_from_active_list(spu); |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 137 | mutex_unlock(&spu_prio->active_mutex[node]); |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Arnd Bergmann | 8676727 | 2006-10-04 17:26:21 +0200 | [diff] [blame] | 140 | static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier); |
| 141 | |
| 142 | static void spu_switch_notify(struct spu *spu, struct spu_context *ctx) |
| 143 | { |
| 144 | blocking_notifier_call_chain(&spu_switch_notifier, |
| 145 | ctx ? ctx->object_id : 0, spu); |
| 146 | } |
| 147 | |
| 148 | int spu_switch_event_register(struct notifier_block * n) |
| 149 | { |
| 150 | return blocking_notifier_chain_register(&spu_switch_notifier, n); |
| 151 | } |
| 152 | |
| 153 | int spu_switch_event_unregister(struct notifier_block * n) |
| 154 | { |
| 155 | return blocking_notifier_chain_unregister(&spu_switch_notifier, n); |
| 156 | } |
| 157 | |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 158 | /** |
| 159 | * spu_bind_context - bind spu context to physical spu |
| 160 | * @spu: physical spu to bind to |
| 161 | * @ctx: context to bind |
| 162 | */ |
| 163 | static void spu_bind_context(struct spu *spu, struct spu_context *ctx) |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 164 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 165 | pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid, |
| 166 | spu->number, spu->node); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 167 | spu->ctx = ctx; |
| 168 | spu->flags = 0; |
| 169 | ctx->spu = spu; |
| 170 | ctx->ops = &spu_hw_ops; |
| 171 | spu->pid = current->pid; |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 172 | spu_associate_mm(spu, ctx->owner); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 173 | spu->ibox_callback = spufs_ibox_callback; |
| 174 | spu->wbox_callback = spufs_wbox_callback; |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 175 | spu->stop_callback = spufs_stop_callback; |
Arnd Bergmann | a33a7d7 | 2006-03-23 00:00:11 +0100 | [diff] [blame] | 176 | spu->mfc_callback = spufs_mfc_callback; |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 177 | spu->dma_callback = spufs_dma_callback; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 178 | mb(); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 179 | spu_unmap_mappings(ctx); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 180 | spu_restore(&ctx->csa, spu); |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 181 | spu->timestamp = jiffies; |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 182 | spu_cpu_affinity_set(spu, raw_smp_processor_id()); |
Arnd Bergmann | 8676727 | 2006-10-04 17:26:21 +0200 | [diff] [blame] | 183 | spu_switch_notify(spu, ctx); |
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 | |
Arnd Bergmann | 8676727 | 2006-10-04 17:26:21 +0200 | [diff] [blame] | 197 | spu_switch_notify(spu, NULL); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 198 | spu_unmap_mappings(ctx); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 199 | spu_save(&ctx->csa, spu); |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 200 | spu->timestamp = jiffies; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 201 | ctx->state = SPU_STATE_SAVED; |
| 202 | spu->ibox_callback = NULL; |
| 203 | spu->wbox_callback = NULL; |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 204 | spu->stop_callback = NULL; |
Arnd Bergmann | a33a7d7 | 2006-03-23 00:00:11 +0100 | [diff] [blame] | 205 | spu->mfc_callback = NULL; |
Arnd Bergmann | 9add11d | 2006-10-04 17:26:14 +0200 | [diff] [blame] | 206 | spu->dma_callback = NULL; |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 207 | spu_associate_mm(spu, NULL); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 208 | spu->pid = 0; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 209 | ctx->ops = &spu_backing_ops; |
| 210 | ctx->spu = NULL; |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 211 | spu->flags = 0; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 212 | spu->ctx = NULL; |
| 213 | } |
| 214 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 215 | /** |
| 216 | * spu_add_to_rq - add a context to the runqueue |
| 217 | * @ctx: context to add |
| 218 | */ |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame] | 219 | static void __spu_add_to_rq(struct spu_context *ctx) |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 220 | { |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame] | 221 | int prio = ctx->prio; |
| 222 | |
| 223 | list_add_tail(&ctx->rq, &spu_prio->runq[prio]); |
| 224 | set_bit(prio, spu_prio->bitmap); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 225 | } |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 226 | |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame] | 227 | static void __spu_del_from_rq(struct spu_context *ctx) |
Christoph Hellwig | a475c2f | 2007-04-23 21:08:11 +0200 | [diff] [blame] | 228 | { |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame] | 229 | int prio = ctx->prio; |
| 230 | |
Christoph Hellwig | a475c2f | 2007-04-23 21:08:11 +0200 | [diff] [blame] | 231 | if (!list_empty(&ctx->rq)) |
| 232 | list_del_init(&ctx->rq); |
| 233 | if (list_empty(&spu_prio->runq[prio])) |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame] | 234 | clear_bit(prio, spu_prio->bitmap); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 237 | static void spu_prio_wait(struct spu_context *ctx) |
| 238 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 239 | DEFINE_WAIT(wait); |
| 240 | |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame] | 241 | spin_lock(&spu_prio->runq_lock); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 242 | prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 243 | if (!signal_pending(current)) { |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame] | 244 | __spu_add_to_rq(ctx); |
| 245 | spin_unlock(&spu_prio->runq_lock); |
Christoph Hellwig | 650f8b0 | 2007-02-13 21:36:50 +0100 | [diff] [blame] | 246 | mutex_unlock(&ctx->state_mutex); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 247 | schedule(); |
Christoph Hellwig | 650f8b0 | 2007-02-13 21:36:50 +0100 | [diff] [blame] | 248 | mutex_lock(&ctx->state_mutex); |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame] | 249 | spin_lock(&spu_prio->runq_lock); |
| 250 | __spu_del_from_rq(ctx); |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 251 | } |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame] | 252 | spin_unlock(&spu_prio->runq_lock); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 253 | __set_current_state(TASK_RUNNING); |
| 254 | remove_wait_queue(&ctx->stop_wq, &wait); |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 255 | } |
| 256 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 257 | static struct spu *spu_get_idle(struct spu_context *ctx) |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 258 | { |
| 259 | struct spu *spu = NULL; |
| 260 | int node = cpu_to_node(raw_smp_processor_id()); |
| 261 | int n; |
| 262 | |
| 263 | for (n = 0; n < MAX_NUMNODES; n++, node++) { |
| 264 | node = (node < MAX_NUMNODES) ? node : 0; |
| 265 | if (!node_allowed(node)) |
| 266 | continue; |
| 267 | spu = spu_alloc_node(node); |
| 268 | if (spu) |
| 269 | break; |
| 270 | } |
| 271 | return spu; |
| 272 | } |
| 273 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 274 | /** |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 275 | * find_victim - find a lower priority context to preempt |
| 276 | * @ctx: canidate context for running |
| 277 | * |
| 278 | * Returns the freed physical spu to run the new context on. |
| 279 | */ |
| 280 | static struct spu *find_victim(struct spu_context *ctx) |
| 281 | { |
| 282 | struct spu_context *victim = NULL; |
| 283 | struct spu *spu; |
| 284 | int node, n; |
| 285 | |
| 286 | /* |
| 287 | * Look for a possible preemption candidate on the local node first. |
| 288 | * If there is no candidate look at the other nodes. This isn't |
| 289 | * exactly fair, but so far the whole spu schedule tries to keep |
| 290 | * a strong node affinity. We might want to fine-tune this in |
| 291 | * the future. |
| 292 | */ |
| 293 | restart: |
| 294 | node = cpu_to_node(raw_smp_processor_id()); |
| 295 | for (n = 0; n < MAX_NUMNODES; n++, node++) { |
| 296 | node = (node < MAX_NUMNODES) ? node : 0; |
| 297 | if (!node_allowed(node)) |
| 298 | continue; |
| 299 | |
| 300 | mutex_lock(&spu_prio->active_mutex[node]); |
| 301 | list_for_each_entry(spu, &spu_prio->active_list[node], list) { |
| 302 | struct spu_context *tmp = spu->ctx; |
| 303 | |
Christoph Hellwig | fe443ef | 2007-06-29 10:57:52 +1000 | [diff] [blame^] | 304 | if (tmp->prio > ctx->prio && |
| 305 | (!victim || tmp->prio > victim->prio)) |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 306 | victim = spu->ctx; |
| 307 | } |
| 308 | mutex_unlock(&spu_prio->active_mutex[node]); |
| 309 | |
| 310 | if (victim) { |
| 311 | /* |
| 312 | * This nests ctx->state_mutex, but we always lock |
| 313 | * higher priority contexts before lower priority |
| 314 | * ones, so this is safe until we introduce |
| 315 | * priority inheritance schemes. |
| 316 | */ |
| 317 | if (!mutex_trylock(&victim->state_mutex)) { |
| 318 | victim = NULL; |
| 319 | goto restart; |
| 320 | } |
| 321 | |
| 322 | spu = victim->spu; |
| 323 | if (!spu) { |
| 324 | /* |
| 325 | * This race can happen because we've dropped |
| 326 | * the active list mutex. No a problem, just |
| 327 | * restart the search. |
| 328 | */ |
| 329 | mutex_unlock(&victim->state_mutex); |
| 330 | victim = NULL; |
| 331 | goto restart; |
| 332 | } |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 333 | spu_remove_from_active_list(spu); |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 334 | spu_unbind_context(spu, victim); |
| 335 | mutex_unlock(&victim->state_mutex); |
Christoph Hellwig | e097b51 | 2007-04-23 21:08:09 +0200 | [diff] [blame] | 336 | /* |
| 337 | * We need to break out of the wait loop in spu_run |
| 338 | * manually to ensure this context gets put on the |
| 339 | * runqueue again ASAP. |
| 340 | */ |
| 341 | wake_up(&victim->stop_wq); |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 342 | return spu; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | /** |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 350 | * spu_activate - find a free spu for a context and execute it |
| 351 | * @ctx: spu context to schedule |
| 352 | * @flags: flags (currently ignored) |
| 353 | * |
Christoph Hellwig | 0887309 | 2007-04-23 21:08:06 +0200 | [diff] [blame] | 354 | * 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] | 355 | * add the context to the runqueue so it gets woken up once an spu |
| 356 | * is available. |
| 357 | */ |
Christoph Hellwig | 26bec67 | 2007-02-13 21:54:24 +0100 | [diff] [blame] | 358 | int spu_activate(struct spu_context *ctx, unsigned long flags) |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 359 | { |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 360 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 361 | if (ctx->spu) |
| 362 | return 0; |
| 363 | |
| 364 | do { |
| 365 | struct spu *spu; |
| 366 | |
| 367 | spu = spu_get_idle(ctx); |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 368 | /* |
| 369 | * If this is a realtime thread we try to get it running by |
| 370 | * preempting a lower priority thread. |
| 371 | */ |
Christoph Hellwig | fe443ef | 2007-06-29 10:57:52 +1000 | [diff] [blame^] | 372 | if (!spu && rt_prio(ctx->prio)) |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 373 | spu = find_victim(ctx); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 374 | if (spu) { |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 375 | spu_bind_context(spu, ctx); |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 376 | spu_add_to_active_list(spu); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 377 | return 0; |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 378 | } |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 379 | |
Christoph Hellwig | 50b520d | 2007-03-10 00:05:36 +0100 | [diff] [blame] | 380 | spu_prio_wait(ctx); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 381 | } while (!signal_pending(current)); |
| 382 | |
| 383 | return -ERESTARTSYS; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 384 | } |
| 385 | |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 386 | /** |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 387 | * grab_runnable_context - try to find a runnable context |
| 388 | * |
| 389 | * Remove the highest priority context on the runqueue and return it |
| 390 | * to the caller. Returns %NULL if no runnable context was found. |
| 391 | */ |
| 392 | static struct spu_context *grab_runnable_context(int prio) |
| 393 | { |
| 394 | struct spu_context *ctx = NULL; |
| 395 | int best; |
| 396 | |
| 397 | spin_lock(&spu_prio->runq_lock); |
| 398 | best = sched_find_first_bit(spu_prio->bitmap); |
| 399 | if (best < prio) { |
| 400 | struct list_head *rq = &spu_prio->runq[best]; |
| 401 | |
| 402 | BUG_ON(list_empty(rq)); |
| 403 | |
| 404 | ctx = list_entry(rq->next, struct spu_context, rq); |
| 405 | __spu_del_from_rq(ctx); |
| 406 | } |
| 407 | spin_unlock(&spu_prio->runq_lock); |
| 408 | |
| 409 | return ctx; |
| 410 | } |
| 411 | |
| 412 | static int __spu_deactivate(struct spu_context *ctx, int force, int max_prio) |
| 413 | { |
| 414 | struct spu *spu = ctx->spu; |
| 415 | struct spu_context *new = NULL; |
| 416 | |
| 417 | if (spu) { |
| 418 | new = grab_runnable_context(max_prio); |
| 419 | if (new || force) { |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 420 | spu_remove_from_active_list(spu); |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 421 | spu_unbind_context(spu, ctx); |
| 422 | spu_free(spu); |
| 423 | if (new) |
| 424 | wake_up(&new->stop_wq); |
| 425 | } |
| 426 | |
| 427 | } |
| 428 | |
| 429 | return new != NULL; |
| 430 | } |
| 431 | |
| 432 | /** |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 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 | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 441 | __spu_deactivate(ctx, 1, MAX_PRIO); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 442 | } |
| 443 | |
Christoph Hellwig | ae7b4c5 | 2007-02-13 21:54:26 +0100 | [diff] [blame] | 444 | /** |
| 445 | * spu_yield - yield a physical spu if others are waiting |
| 446 | * @ctx: spu context to yield |
| 447 | * |
| 448 | * Check if there is a higher priority context waiting and if yes |
| 449 | * unbind @ctx from the physical spu and schedule the highest |
| 450 | * priority context to run on the freed physical spu instead. |
| 451 | */ |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 452 | void spu_yield(struct spu_context *ctx) |
| 453 | { |
Christoph Hellwig | e5c0b9e | 2007-06-05 11:25:59 +1000 | [diff] [blame] | 454 | if (!(ctx->flags & SPU_CREATE_NOSCHED)) { |
| 455 | mutex_lock(&ctx->state_mutex); |
| 456 | __spu_deactivate(ctx, 0, MAX_PRIO); |
| 457 | mutex_unlock(&ctx->state_mutex); |
| 458 | } |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 459 | } |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 460 | |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 461 | static void spusched_tick(struct spu_context *ctx) |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 462 | { |
Christoph Hellwig | fe443ef | 2007-06-29 10:57:52 +1000 | [diff] [blame^] | 463 | if (ctx->policy == SCHED_FIFO || --ctx->time_slice) |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 464 | return; |
| 465 | |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 466 | /* |
| 467 | * Unfortunately active_mutex ranks outside of state_mutex, so |
| 468 | * we have to trylock here. If we fail give the context another |
| 469 | * tick and try again. |
| 470 | */ |
| 471 | if (mutex_trylock(&ctx->state_mutex)) { |
| 472 | struct spu_context *new = grab_runnable_context(ctx->prio + 1); |
| 473 | if (new) { |
| 474 | struct spu *spu = ctx->spu; |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 475 | |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 476 | __spu_remove_from_active_list(spu); |
| 477 | spu_unbind_context(spu, ctx); |
| 478 | spu_free(spu); |
| 479 | wake_up(&new->stop_wq); |
| 480 | /* |
| 481 | * We need to break out of the wait loop in |
| 482 | * spu_run manually to ensure this context |
| 483 | * gets put on the runqueue again ASAP. |
| 484 | */ |
| 485 | wake_up(&ctx->stop_wq); |
| 486 | } |
Christoph Hellwig | fe443ef | 2007-06-29 10:57:52 +1000 | [diff] [blame^] | 487 | spu_set_timeslice(ctx); |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 488 | mutex_unlock(&ctx->state_mutex); |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 489 | } else { |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 490 | ctx->time_slice++; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 491 | } |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 492 | } |
| 493 | |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 494 | static void spusched_wake(unsigned long data) |
| 495 | { |
| 496 | mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK); |
| 497 | wake_up_process(spusched_task); |
| 498 | } |
| 499 | |
| 500 | static int spusched_thread(void *unused) |
| 501 | { |
| 502 | struct spu *spu, *next; |
| 503 | int node; |
| 504 | |
| 505 | setup_timer(&spusched_timer, spusched_wake, 0); |
| 506 | __mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK); |
| 507 | |
| 508 | while (!kthread_should_stop()) { |
| 509 | set_current_state(TASK_INTERRUPTIBLE); |
| 510 | schedule(); |
| 511 | for (node = 0; node < MAX_NUMNODES; node++) { |
| 512 | mutex_lock(&spu_prio->active_mutex[node]); |
| 513 | list_for_each_entry_safe(spu, next, |
| 514 | &spu_prio->active_list[node], |
| 515 | list) |
| 516 | spusched_tick(spu->ctx); |
| 517 | mutex_unlock(&spu_prio->active_mutex[node]); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | del_timer_sync(&spusched_timer); |
| 522 | return 0; |
| 523 | } |
| 524 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 525 | int __init spu_sched_init(void) |
| 526 | { |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 527 | int i; |
| 528 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 529 | spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL); |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 530 | if (!spu_prio) |
| 531 | return -ENOMEM; |
| 532 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 533 | for (i = 0; i < MAX_PRIO; i++) { |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 534 | INIT_LIST_HEAD(&spu_prio->runq[i]); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 535 | __clear_bit(i, spu_prio->bitmap); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 536 | } |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 537 | __set_bit(MAX_PRIO, spu_prio->bitmap); |
| 538 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 539 | mutex_init(&spu_prio->active_mutex[i]); |
| 540 | INIT_LIST_HEAD(&spu_prio->active_list[i]); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 541 | } |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 542 | spin_lock_init(&spu_prio->runq_lock); |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 543 | |
| 544 | spusched_task = kthread_run(spusched_thread, NULL, "spusched"); |
| 545 | if (IS_ERR(spusched_task)) { |
| 546 | kfree(spu_prio); |
| 547 | return PTR_ERR(spusched_task); |
| 548 | } |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 549 | return 0; |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 550 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | void __exit spu_sched_exit(void) |
| 554 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 555 | struct spu *spu, *tmp; |
| 556 | int node; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 557 | |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 558 | kthread_stop(spusched_task); |
| 559 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 560 | for (node = 0; node < MAX_NUMNODES; node++) { |
| 561 | mutex_lock(&spu_prio->active_mutex[node]); |
| 562 | list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node], |
| 563 | list) { |
| 564 | list_del_init(&spu->list); |
| 565 | spu_free(spu); |
| 566 | } |
| 567 | mutex_unlock(&spu_prio->active_mutex[node]); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 568 | } |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 569 | kfree(spu_prio); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 570 | } |