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> |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 39 | #include <linux/pid_namespace.h> |
| 40 | #include <linux/proc_fs.h> |
| 41 | #include <linux/seq_file.h> |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 42 | |
| 43 | #include <asm/io.h> |
| 44 | #include <asm/mmu_context.h> |
| 45 | #include <asm/spu.h> |
| 46 | #include <asm/spu_csa.h> |
Geoff Levand | a91942a | 2006-06-19 20:33:30 +0200 | [diff] [blame] | 47 | #include <asm/spu_priv1.h> |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 48 | #include "spufs.h" |
| 49 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 50 | struct spu_prio_array { |
Christoph Hellwig | 72cb360 | 2007-02-13 21:54:28 +0100 | [diff] [blame] | 51 | DECLARE_BITMAP(bitmap, MAX_PRIO); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 52 | struct list_head runq[MAX_PRIO]; |
| 53 | spinlock_t runq_lock; |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 54 | int nr_waiting; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 55 | }; |
| 56 | |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 57 | static unsigned long spu_avenrun[3]; |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 58 | static struct spu_prio_array *spu_prio; |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 59 | static struct task_struct *spusched_task; |
| 60 | static struct timer_list spusched_timer; |
Aegis Lin | 90608a2 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 61 | static struct timer_list spuloadavg_timer; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 62 | |
Christoph Hellwig | fe443ef | 2007-06-29 10:57:52 +1000 | [diff] [blame] | 63 | /* |
| 64 | * Priority of a normal, non-rt, non-niced'd process (aka nice level 0). |
| 65 | */ |
| 66 | #define NORMAL_PRIO 120 |
| 67 | |
| 68 | /* |
| 69 | * Frequency of the spu scheduler tick. By default we do one SPU scheduler |
| 70 | * tick for every 10 CPU scheduler ticks. |
| 71 | */ |
| 72 | #define SPUSCHED_TICK (10) |
| 73 | |
| 74 | /* |
| 75 | * These are the 'tuning knobs' of the scheduler: |
| 76 | * |
Jeremy Kerr | 60e2423 | 2007-06-29 10:57:53 +1000 | [diff] [blame] | 77 | * Minimum timeslice is 5 msecs (or 1 spu scheduler tick, whichever is |
| 78 | * larger), default timeslice is 100 msecs, maximum timeslice is 800 msecs. |
Christoph Hellwig | fe443ef | 2007-06-29 10:57:52 +1000 | [diff] [blame] | 79 | */ |
Jeremy Kerr | 60e2423 | 2007-06-29 10:57:53 +1000 | [diff] [blame] | 80 | #define MIN_SPU_TIMESLICE max(5 * HZ / (1000 * SPUSCHED_TICK), 1) |
| 81 | #define DEF_SPU_TIMESLICE (100 * HZ / (1000 * SPUSCHED_TICK)) |
Christoph Hellwig | fe443ef | 2007-06-29 10:57:52 +1000 | [diff] [blame] | 82 | |
| 83 | #define MAX_USER_PRIO (MAX_PRIO - MAX_RT_PRIO) |
| 84 | #define SCALE_PRIO(x, prio) \ |
| 85 | max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_SPU_TIMESLICE) |
| 86 | |
| 87 | /* |
| 88 | * scale user-nice values [ -20 ... 0 ... 19 ] to time slice values: |
| 89 | * [800ms ... 100ms ... 5ms] |
| 90 | * |
| 91 | * The higher a thread's priority, the bigger timeslices |
| 92 | * it gets during one round of execution. But even the lowest |
| 93 | * priority thread gets MIN_TIMESLICE worth of execution time. |
| 94 | */ |
| 95 | void spu_set_timeslice(struct spu_context *ctx) |
| 96 | { |
| 97 | if (ctx->prio < NORMAL_PRIO) |
| 98 | ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE * 4, ctx->prio); |
| 99 | else |
| 100 | ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE, ctx->prio); |
| 101 | } |
| 102 | |
Christoph Hellwig | 2cf2b3b | 2007-06-29 10:57:55 +1000 | [diff] [blame] | 103 | /* |
| 104 | * Update scheduling information from the owning thread. |
| 105 | */ |
| 106 | void __spu_update_sched_info(struct spu_context *ctx) |
| 107 | { |
| 108 | /* |
Luke Browning | 9156953 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 109 | * assert that the context is not on the runqueue, so it is safe |
| 110 | * to change its scheduling parameters. |
| 111 | */ |
| 112 | BUG_ON(!list_empty(&ctx->rq)); |
| 113 | |
| 114 | /* |
Julio M. Merino Vidal | 9b1d21f | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 115 | * 32-Bit assignments are atomic on powerpc, and we don't care about |
| 116 | * memory ordering here because retrieving the controlling thread is |
| 117 | * per definition racy. |
Christoph Hellwig | 476273a | 2007-06-29 10:58:01 +1000 | [diff] [blame] | 118 | */ |
| 119 | ctx->tid = current->pid; |
| 120 | |
| 121 | /* |
Christoph Hellwig | 2cf2b3b | 2007-06-29 10:57:55 +1000 | [diff] [blame] | 122 | * We do our own priority calculations, so we normally want |
Julio M. Merino Vidal | 9b1d21f | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 123 | * ->static_prio to start with. Unfortunately this field |
Christoph Hellwig | 2cf2b3b | 2007-06-29 10:57:55 +1000 | [diff] [blame] | 124 | * contains junk for threads with a realtime scheduling |
| 125 | * policy so we have to look at ->prio in this case. |
| 126 | */ |
| 127 | if (rt_prio(current->prio)) |
| 128 | ctx->prio = current->prio; |
| 129 | else |
| 130 | ctx->prio = current->static_prio; |
| 131 | ctx->policy = current->policy; |
Christoph Hellwig | ea1ae59 | 2007-06-29 10:57:56 +1000 | [diff] [blame] | 132 | |
| 133 | /* |
Luke Browning | 9156953 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 134 | * TO DO: the context may be loaded, so we may need to activate |
| 135 | * it again on a different node. But it shouldn't hurt anything |
| 136 | * to update its parameters, because we know that the scheduler |
| 137 | * is not actively looking at this field, since it is not on the |
| 138 | * runqueue. The context will be rescheduled on the proper node |
| 139 | * if it is timesliced or preempted. |
Christoph Hellwig | ea1ae59 | 2007-06-29 10:57:56 +1000 | [diff] [blame] | 140 | */ |
Christoph Hellwig | ea1ae59 | 2007-06-29 10:57:56 +1000 | [diff] [blame] | 141 | ctx->cpus_allowed = current->cpus_allowed; |
Christoph Hellwig | 2cf2b3b | 2007-06-29 10:57:55 +1000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void spu_update_sched_info(struct spu_context *ctx) |
| 145 | { |
Luke Browning | 9156953 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 146 | int node; |
Christoph Hellwig | 2cf2b3b | 2007-06-29 10:57:55 +1000 | [diff] [blame] | 147 | |
Luke Browning | 9156953 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 148 | if (ctx->state == SPU_STATE_RUNNABLE) { |
| 149 | node = ctx->spu->node; |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 150 | |
| 151 | /* |
| 152 | * Take list_mutex to sync with find_victim(). |
| 153 | */ |
Luke Browning | 9156953 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 154 | mutex_lock(&cbe_spu_info[node].list_mutex); |
| 155 | __spu_update_sched_info(ctx); |
| 156 | mutex_unlock(&cbe_spu_info[node].list_mutex); |
| 157 | } else { |
| 158 | __spu_update_sched_info(ctx); |
| 159 | } |
Christoph Hellwig | 2cf2b3b | 2007-06-29 10:57:55 +1000 | [diff] [blame] | 160 | } |
| 161 | |
Christoph Hellwig | ea1ae59 | 2007-06-29 10:57:56 +1000 | [diff] [blame] | 162 | static int __node_allowed(struct spu_context *ctx, int node) |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 163 | { |
Christoph Hellwig | ea1ae59 | 2007-06-29 10:57:56 +1000 | [diff] [blame] | 164 | if (nr_cpus_node(node)) { |
| 165 | cpumask_t mask = node_to_cpumask(node); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 166 | |
Christoph Hellwig | ea1ae59 | 2007-06-29 10:57:56 +1000 | [diff] [blame] | 167 | if (cpus_intersects(mask, ctx->cpus_allowed)) |
| 168 | return 1; |
| 169 | } |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static int node_allowed(struct spu_context *ctx, int node) |
| 175 | { |
| 176 | int rval; |
| 177 | |
| 178 | spin_lock(&spu_prio->runq_lock); |
| 179 | rval = __node_allowed(ctx, node); |
| 180 | spin_unlock(&spu_prio->runq_lock); |
| 181 | |
| 182 | return rval; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 183 | } |
| 184 | |
Bob Nelson | aed3a8c | 2007-12-15 01:27:30 +1100 | [diff] [blame] | 185 | void do_notify_spus_active(void) |
Bob Nelson | 36aaccc | 2007-07-20 21:39:52 +0200 | [diff] [blame] | 186 | { |
| 187 | int node; |
| 188 | |
| 189 | /* |
| 190 | * Wake up the active spu_contexts. |
| 191 | * |
| 192 | * When the awakened processes see their "notify_active" flag is set, |
Julio M. Merino Vidal | 9b1d21f | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 193 | * they will call spu_switch_notify(). |
Bob Nelson | 36aaccc | 2007-07-20 21:39:52 +0200 | [diff] [blame] | 194 | */ |
| 195 | for_each_online_node(node) { |
| 196 | struct spu *spu; |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 197 | |
| 198 | mutex_lock(&cbe_spu_info[node].list_mutex); |
| 199 | list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) { |
| 200 | if (spu->alloc_state != SPU_FREE) { |
| 201 | struct spu_context *ctx = spu->ctx; |
| 202 | set_bit(SPU_SCHED_NOTIFY_ACTIVE, |
| 203 | &ctx->sched_flags); |
| 204 | mb(); |
| 205 | wake_up_all(&ctx->stop_wq); |
| 206 | } |
Bob Nelson | 36aaccc | 2007-07-20 21:39:52 +0200 | [diff] [blame] | 207 | } |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 208 | mutex_unlock(&cbe_spu_info[node].list_mutex); |
Bob Nelson | 36aaccc | 2007-07-20 21:39:52 +0200 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 212 | /** |
| 213 | * spu_bind_context - bind spu context to physical spu |
| 214 | * @spu: physical spu to bind to |
| 215 | * @ctx: context to bind |
| 216 | */ |
| 217 | static void spu_bind_context(struct spu *spu, struct spu_context *ctx) |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 218 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 219 | pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid, |
| 220 | spu->number, spu->node); |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 221 | spuctx_switch_state(ctx, SPU_UTIL_SYSTEM); |
Christoph Hellwig | e9f8a0b | 2007-06-29 10:58:03 +1000 | [diff] [blame] | 222 | |
Arnd Bergmann | aa6d5b2 | 2007-07-20 21:39:44 +0200 | [diff] [blame] | 223 | if (ctx->flags & SPU_CREATE_NOSCHED) |
| 224 | atomic_inc(&cbe_spu_info[spu->node].reserved_spus); |
| 225 | |
Christoph Hellwig | e9f8a0b | 2007-06-29 10:58:03 +1000 | [diff] [blame] | 226 | ctx->stats.slb_flt_base = spu->stats.slb_flt; |
| 227 | ctx->stats.class2_intr_base = spu->stats.class2_intr; |
| 228 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 229 | spu->ctx = ctx; |
| 230 | spu->flags = 0; |
| 231 | ctx->spu = spu; |
| 232 | ctx->ops = &spu_hw_ops; |
| 233 | spu->pid = current->pid; |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 234 | spu->tgid = current->tgid; |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 235 | spu_associate_mm(spu, ctx->owner); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 236 | spu->ibox_callback = spufs_ibox_callback; |
| 237 | spu->wbox_callback = spufs_wbox_callback; |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 238 | spu->stop_callback = spufs_stop_callback; |
Arnd Bergmann | a33a7d7 | 2006-03-23 00:00:11 +0100 | [diff] [blame] | 239 | spu->mfc_callback = spufs_mfc_callback; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 240 | mb(); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 241 | spu_unmap_mappings(ctx); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 242 | spu_restore(&ctx->csa, spu); |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 243 | spu->timestamp = jiffies; |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 244 | spu_cpu_affinity_set(spu, raw_smp_processor_id()); |
Arnd Bergmann | 8676727 | 2006-10-04 17:26:21 +0200 | [diff] [blame] | 245 | spu_switch_notify(spu, ctx); |
Christoph Hellwig | 81998ba | 2007-02-13 21:36:48 +0100 | [diff] [blame] | 246 | ctx->state = SPU_STATE_RUNNABLE; |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 247 | |
| 248 | spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Arnd Bergmann | c5fc8d2 | 2007-07-20 21:39:48 +0200 | [diff] [blame] | 251 | /* |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 252 | * Must be used with the list_mutex held. |
Arnd Bergmann | c5fc8d2 | 2007-07-20 21:39:48 +0200 | [diff] [blame] | 253 | */ |
| 254 | static inline int sched_spu(struct spu *spu) |
| 255 | { |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 256 | BUG_ON(!mutex_is_locked(&cbe_spu_info[spu->node].list_mutex)); |
| 257 | |
Arnd Bergmann | c5fc8d2 | 2007-07-20 21:39:48 +0200 | [diff] [blame] | 258 | return (!spu->ctx || !(spu->ctx->flags & SPU_CREATE_NOSCHED)); |
| 259 | } |
| 260 | |
| 261 | static void aff_merge_remaining_ctxs(struct spu_gang *gang) |
| 262 | { |
| 263 | struct spu_context *ctx; |
| 264 | |
| 265 | list_for_each_entry(ctx, &gang->aff_list_head, aff_list) { |
| 266 | if (list_empty(&ctx->aff_list)) |
| 267 | list_add(&ctx->aff_list, &gang->aff_list_head); |
| 268 | } |
| 269 | gang->aff_flags |= AFF_MERGED; |
| 270 | } |
| 271 | |
| 272 | static void aff_set_offsets(struct spu_gang *gang) |
| 273 | { |
| 274 | struct spu_context *ctx; |
| 275 | int offset; |
| 276 | |
| 277 | offset = -1; |
| 278 | list_for_each_entry_reverse(ctx, &gang->aff_ref_ctx->aff_list, |
| 279 | aff_list) { |
| 280 | if (&ctx->aff_list == &gang->aff_list_head) |
| 281 | break; |
| 282 | ctx->aff_offset = offset--; |
| 283 | } |
| 284 | |
| 285 | offset = 0; |
| 286 | list_for_each_entry(ctx, gang->aff_ref_ctx->aff_list.prev, aff_list) { |
| 287 | if (&ctx->aff_list == &gang->aff_list_head) |
| 288 | break; |
| 289 | ctx->aff_offset = offset++; |
| 290 | } |
| 291 | |
| 292 | gang->aff_flags |= AFF_OFFSETS_SET; |
| 293 | } |
| 294 | |
| 295 | static struct spu *aff_ref_location(struct spu_context *ctx, int mem_aff, |
| 296 | int group_size, int lowest_offset) |
| 297 | { |
| 298 | struct spu *spu; |
| 299 | int node, n; |
| 300 | |
| 301 | /* |
| 302 | * TODO: A better algorithm could be used to find a good spu to be |
| 303 | * used as reference location for the ctxs chain. |
| 304 | */ |
| 305 | node = cpu_to_node(raw_smp_processor_id()); |
| 306 | for (n = 0; n < MAX_NUMNODES; n++, node++) { |
| 307 | node = (node < MAX_NUMNODES) ? node : 0; |
| 308 | if (!node_allowed(ctx, node)) |
| 309 | continue; |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 310 | mutex_lock(&cbe_spu_info[node].list_mutex); |
Arnd Bergmann | c5fc8d2 | 2007-07-20 21:39:48 +0200 | [diff] [blame] | 311 | list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) { |
| 312 | if ((!mem_aff || spu->has_mem_affinity) && |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 313 | sched_spu(spu)) { |
| 314 | mutex_unlock(&cbe_spu_info[node].list_mutex); |
Arnd Bergmann | c5fc8d2 | 2007-07-20 21:39:48 +0200 | [diff] [blame] | 315 | return spu; |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 316 | } |
Arnd Bergmann | c5fc8d2 | 2007-07-20 21:39:48 +0200 | [diff] [blame] | 317 | } |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 318 | mutex_unlock(&cbe_spu_info[node].list_mutex); |
Arnd Bergmann | c5fc8d2 | 2007-07-20 21:39:48 +0200 | [diff] [blame] | 319 | } |
| 320 | return NULL; |
| 321 | } |
| 322 | |
| 323 | static void aff_set_ref_point_location(struct spu_gang *gang) |
| 324 | { |
| 325 | int mem_aff, gs, lowest_offset; |
| 326 | struct spu_context *ctx; |
| 327 | struct spu *tmp; |
| 328 | |
| 329 | mem_aff = gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM; |
| 330 | lowest_offset = 0; |
| 331 | gs = 0; |
| 332 | |
| 333 | list_for_each_entry(tmp, &gang->aff_list_head, aff_list) |
| 334 | gs++; |
| 335 | |
| 336 | list_for_each_entry_reverse(ctx, &gang->aff_ref_ctx->aff_list, |
| 337 | aff_list) { |
| 338 | if (&ctx->aff_list == &gang->aff_list_head) |
| 339 | break; |
| 340 | lowest_offset = ctx->aff_offset; |
| 341 | } |
| 342 | |
Andre Detsch | 683e3ab | 2007-07-31 09:48:11 +1000 | [diff] [blame] | 343 | gang->aff_ref_spu = aff_ref_location(gang->aff_ref_ctx, mem_aff, gs, |
| 344 | lowest_offset); |
Arnd Bergmann | c5fc8d2 | 2007-07-20 21:39:48 +0200 | [diff] [blame] | 345 | } |
| 346 | |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 347 | static struct spu *ctx_location(struct spu *ref, int offset, int node) |
Arnd Bergmann | c5fc8d2 | 2007-07-20 21:39:48 +0200 | [diff] [blame] | 348 | { |
| 349 | struct spu *spu; |
| 350 | |
| 351 | spu = NULL; |
| 352 | if (offset >= 0) { |
| 353 | list_for_each_entry(spu, ref->aff_list.prev, aff_list) { |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 354 | BUG_ON(spu->node != node); |
Arnd Bergmann | c5fc8d2 | 2007-07-20 21:39:48 +0200 | [diff] [blame] | 355 | if (offset == 0) |
| 356 | break; |
| 357 | if (sched_spu(spu)) |
| 358 | offset--; |
| 359 | } |
| 360 | } else { |
| 361 | list_for_each_entry_reverse(spu, ref->aff_list.next, aff_list) { |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 362 | BUG_ON(spu->node != node); |
Arnd Bergmann | c5fc8d2 | 2007-07-20 21:39:48 +0200 | [diff] [blame] | 363 | if (offset == 0) |
| 364 | break; |
| 365 | if (sched_spu(spu)) |
| 366 | offset++; |
| 367 | } |
| 368 | } |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 369 | |
Arnd Bergmann | c5fc8d2 | 2007-07-20 21:39:48 +0200 | [diff] [blame] | 370 | return spu; |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * affinity_check is called each time a context is going to be scheduled. |
| 375 | * It returns the spu ptr on which the context must run. |
| 376 | */ |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 377 | static int has_affinity(struct spu_context *ctx) |
Arnd Bergmann | c5fc8d2 | 2007-07-20 21:39:48 +0200 | [diff] [blame] | 378 | { |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 379 | struct spu_gang *gang = ctx->gang; |
Arnd Bergmann | c5fc8d2 | 2007-07-20 21:39:48 +0200 | [diff] [blame] | 380 | |
| 381 | if (list_empty(&ctx->aff_list)) |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 382 | return 0; |
| 383 | |
Arnd Bergmann | c5fc8d2 | 2007-07-20 21:39:48 +0200 | [diff] [blame] | 384 | if (!gang->aff_ref_spu) { |
| 385 | if (!(gang->aff_flags & AFF_MERGED)) |
| 386 | aff_merge_remaining_ctxs(gang); |
| 387 | if (!(gang->aff_flags & AFF_OFFSETS_SET)) |
| 388 | aff_set_offsets(gang); |
| 389 | aff_set_ref_point_location(gang); |
| 390 | } |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 391 | |
| 392 | return gang->aff_ref_spu != NULL; |
Arnd Bergmann | c5fc8d2 | 2007-07-20 21:39:48 +0200 | [diff] [blame] | 393 | } |
| 394 | |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 395 | /** |
| 396 | * spu_unbind_context - unbind spu context from physical spu |
| 397 | * @spu: physical spu to unbind from |
| 398 | * @ctx: context to unbind |
Christoph Hellwig | 202557d | 2007-02-13 21:36:49 +0100 | [diff] [blame] | 399 | */ |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 400 | static void spu_unbind_context(struct spu *spu, struct spu_context *ctx) |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 401 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 402 | pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__, |
| 403 | spu->pid, spu->number, spu->node); |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 404 | spuctx_switch_state(ctx, SPU_UTIL_SYSTEM); |
Christoph Hellwig | fe2f896 | 2007-06-29 10:58:07 +1000 | [diff] [blame] | 405 | |
Arnd Bergmann | aa6d5b2 | 2007-07-20 21:39:44 +0200 | [diff] [blame] | 406 | if (spu->ctx->flags & SPU_CREATE_NOSCHED) |
| 407 | atomic_dec(&cbe_spu_info[spu->node].reserved_spus); |
Andre Detsch | 36ddbb1 | 2007-09-19 14:38:12 +1000 | [diff] [blame] | 408 | |
| 409 | if (ctx->gang){ |
| 410 | mutex_lock(&ctx->gang->aff_mutex); |
| 411 | if (has_affinity(ctx)) { |
| 412 | if (atomic_dec_and_test(&ctx->gang->aff_sched_count)) |
| 413 | ctx->gang->aff_ref_spu = NULL; |
| 414 | } |
| 415 | mutex_unlock(&ctx->gang->aff_mutex); |
| 416 | } |
| 417 | |
Arnd Bergmann | 8676727 | 2006-10-04 17:26:21 +0200 | [diff] [blame] | 418 | spu_switch_notify(spu, NULL); |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 419 | spu_unmap_mappings(ctx); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 420 | spu_save(&ctx->csa, spu); |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 421 | spu->timestamp = jiffies; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 422 | ctx->state = SPU_STATE_SAVED; |
| 423 | spu->ibox_callback = NULL; |
| 424 | spu->wbox_callback = NULL; |
Arnd Bergmann | 5110459 | 2005-12-05 22:52:25 -0500 | [diff] [blame] | 425 | spu->stop_callback = NULL; |
Arnd Bergmann | a33a7d7 | 2006-03-23 00:00:11 +0100 | [diff] [blame] | 426 | spu->mfc_callback = NULL; |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 427 | spu_associate_mm(spu, NULL); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 428 | spu->pid = 0; |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 429 | spu->tgid = 0; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 430 | ctx->ops = &spu_backing_ops; |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 431 | spu->flags = 0; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 432 | spu->ctx = NULL; |
Christoph Hellwig | e9f8a0b | 2007-06-29 10:58:03 +1000 | [diff] [blame] | 433 | |
| 434 | ctx->stats.slb_flt += |
| 435 | (spu->stats.slb_flt - ctx->stats.slb_flt_base); |
| 436 | ctx->stats.class2_intr += |
| 437 | (spu->stats.class2_intr - ctx->stats.class2_intr_base); |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 438 | |
| 439 | /* This maps the underlying spu state to idle */ |
| 440 | spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED); |
| 441 | ctx->spu = NULL; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 442 | } |
| 443 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 444 | /** |
| 445 | * spu_add_to_rq - add a context to the runqueue |
| 446 | * @ctx: context to add |
| 447 | */ |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame] | 448 | static void __spu_add_to_rq(struct spu_context *ctx) |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 449 | { |
Christoph Hellwig | 2744997 | 2007-06-29 10:58:06 +1000 | [diff] [blame] | 450 | /* |
| 451 | * Unfortunately this code path can be called from multiple threads |
| 452 | * on behalf of a single context due to the way the problem state |
| 453 | * mmap support works. |
| 454 | * |
| 455 | * Fortunately we need to wake up all these threads at the same time |
| 456 | * and can simply skip the runqueue addition for every but the first |
| 457 | * thread getting into this codepath. |
| 458 | * |
| 459 | * It's still quite hacky, and long-term we should proxy all other |
| 460 | * threads through the owner thread so that spu_run is in control |
| 461 | * of all the scheduling activity for a given context. |
| 462 | */ |
| 463 | if (list_empty(&ctx->rq)) { |
| 464 | list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]); |
| 465 | set_bit(ctx->prio, spu_prio->bitmap); |
| 466 | if (!spu_prio->nr_waiting++) |
| 467 | __mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK); |
| 468 | } |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 469 | } |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 470 | |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 471 | static void spu_add_to_rq(struct spu_context *ctx) |
| 472 | { |
| 473 | spin_lock(&spu_prio->runq_lock); |
| 474 | __spu_add_to_rq(ctx); |
| 475 | spin_unlock(&spu_prio->runq_lock); |
| 476 | } |
| 477 | |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame] | 478 | static void __spu_del_from_rq(struct spu_context *ctx) |
Christoph Hellwig | a475c2f | 2007-04-23 21:08:11 +0200 | [diff] [blame] | 479 | { |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame] | 480 | int prio = ctx->prio; |
| 481 | |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 482 | if (!list_empty(&ctx->rq)) { |
Christoph Hellwig | c77239b | 2007-06-29 10:58:05 +1000 | [diff] [blame] | 483 | if (!--spu_prio->nr_waiting) |
| 484 | del_timer(&spusched_timer); |
Christoph Hellwig | a475c2f | 2007-04-23 21:08:11 +0200 | [diff] [blame] | 485 | list_del_init(&ctx->rq); |
Christoph Hellwig | c77239b | 2007-06-29 10:58:05 +1000 | [diff] [blame] | 486 | |
| 487 | if (list_empty(&spu_prio->runq[prio])) |
| 488 | clear_bit(prio, spu_prio->bitmap); |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 489 | } |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 490 | } |
| 491 | |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 492 | void spu_del_from_rq(struct spu_context *ctx) |
| 493 | { |
| 494 | spin_lock(&spu_prio->runq_lock); |
| 495 | __spu_del_from_rq(ctx); |
| 496 | spin_unlock(&spu_prio->runq_lock); |
| 497 | } |
| 498 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 499 | static void spu_prio_wait(struct spu_context *ctx) |
| 500 | { |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 501 | DEFINE_WAIT(wait); |
| 502 | |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 503 | /* |
| 504 | * The caller must explicitly wait for a context to be loaded |
| 505 | * if the nosched flag is set. If NOSCHED is not set, the caller |
| 506 | * queues the context and waits for an spu event or error. |
| 507 | */ |
| 508 | BUG_ON(!(ctx->flags & SPU_CREATE_NOSCHED)); |
| 509 | |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame] | 510 | spin_lock(&spu_prio->runq_lock); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 511 | prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 512 | if (!signal_pending(current)) { |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame] | 513 | __spu_add_to_rq(ctx); |
| 514 | spin_unlock(&spu_prio->runq_lock); |
Christoph Hellwig | 650f8b0 | 2007-02-13 21:36:50 +0100 | [diff] [blame] | 515 | mutex_unlock(&ctx->state_mutex); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 516 | schedule(); |
Christoph Hellwig | 650f8b0 | 2007-02-13 21:36:50 +0100 | [diff] [blame] | 517 | mutex_lock(&ctx->state_mutex); |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame] | 518 | spin_lock(&spu_prio->runq_lock); |
| 519 | __spu_del_from_rq(ctx); |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 520 | } |
Luke Browning | 4e0f4ed | 2007-04-23 21:08:13 +0200 | [diff] [blame] | 521 | spin_unlock(&spu_prio->runq_lock); |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 522 | __set_current_state(TASK_RUNNING); |
| 523 | remove_wait_queue(&ctx->stop_wq, &wait); |
Arnd Bergmann | 2a911f0 | 2005-12-05 22:52:26 -0500 | [diff] [blame] | 524 | } |
| 525 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 526 | static struct spu *spu_get_idle(struct spu_context *ctx) |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 527 | { |
Andre Detsch | 36ddbb1 | 2007-09-19 14:38:12 +1000 | [diff] [blame] | 528 | struct spu *spu, *aff_ref_spu; |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 529 | int node, n; |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 530 | |
Andre Detsch | 36ddbb1 | 2007-09-19 14:38:12 +1000 | [diff] [blame] | 531 | if (ctx->gang) { |
| 532 | mutex_lock(&ctx->gang->aff_mutex); |
| 533 | if (has_affinity(ctx)) { |
| 534 | aff_ref_spu = ctx->gang->aff_ref_spu; |
| 535 | atomic_inc(&ctx->gang->aff_sched_count); |
| 536 | mutex_unlock(&ctx->gang->aff_mutex); |
| 537 | node = aff_ref_spu->node; |
Arnd Bergmann | cbc23d3 | 2007-07-20 21:39:49 +0200 | [diff] [blame] | 538 | |
Andre Detsch | 36ddbb1 | 2007-09-19 14:38:12 +1000 | [diff] [blame] | 539 | mutex_lock(&cbe_spu_info[node].list_mutex); |
| 540 | spu = ctx_location(aff_ref_spu, ctx->aff_offset, node); |
| 541 | if (spu && spu->alloc_state == SPU_FREE) |
| 542 | goto found; |
| 543 | mutex_unlock(&cbe_spu_info[node].list_mutex); |
| 544 | |
| 545 | mutex_lock(&ctx->gang->aff_mutex); |
| 546 | if (atomic_dec_and_test(&ctx->gang->aff_sched_count)) |
| 547 | ctx->gang->aff_ref_spu = NULL; |
| 548 | mutex_unlock(&ctx->gang->aff_mutex); |
| 549 | |
| 550 | return NULL; |
| 551 | } |
| 552 | mutex_unlock(&ctx->gang->aff_mutex); |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 553 | } |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 554 | node = cpu_to_node(raw_smp_processor_id()); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 555 | for (n = 0; n < MAX_NUMNODES; n++, node++) { |
| 556 | node = (node < MAX_NUMNODES) ? node : 0; |
Christoph Hellwig | ea1ae59 | 2007-06-29 10:57:56 +1000 | [diff] [blame] | 557 | if (!node_allowed(ctx, node)) |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 558 | continue; |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 559 | |
| 560 | mutex_lock(&cbe_spu_info[node].list_mutex); |
| 561 | list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) { |
| 562 | if (spu->alloc_state == SPU_FREE) |
| 563 | goto found; |
| 564 | } |
| 565 | mutex_unlock(&cbe_spu_info[node].list_mutex); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 566 | } |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 567 | |
| 568 | return NULL; |
| 569 | |
| 570 | found: |
| 571 | spu->alloc_state = SPU_USED; |
| 572 | mutex_unlock(&cbe_spu_info[node].list_mutex); |
| 573 | pr_debug("Got SPU %d %d\n", spu->number, spu->node); |
| 574 | spu_init_channels(spu); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 575 | return spu; |
| 576 | } |
| 577 | |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 578 | /** |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 579 | * find_victim - find a lower priority context to preempt |
| 580 | * @ctx: canidate context for running |
| 581 | * |
| 582 | * Returns the freed physical spu to run the new context on. |
| 583 | */ |
| 584 | static struct spu *find_victim(struct spu_context *ctx) |
| 585 | { |
| 586 | struct spu_context *victim = NULL; |
| 587 | struct spu *spu; |
| 588 | int node, n; |
| 589 | |
| 590 | /* |
| 591 | * Look for a possible preemption candidate on the local node first. |
| 592 | * If there is no candidate look at the other nodes. This isn't |
Julio M. Merino Vidal | 9b1d21f | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 593 | * exactly fair, but so far the whole spu scheduler tries to keep |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 594 | * a strong node affinity. We might want to fine-tune this in |
| 595 | * the future. |
| 596 | */ |
| 597 | restart: |
| 598 | node = cpu_to_node(raw_smp_processor_id()); |
| 599 | for (n = 0; n < MAX_NUMNODES; n++, node++) { |
| 600 | node = (node < MAX_NUMNODES) ? node : 0; |
Christoph Hellwig | ea1ae59 | 2007-06-29 10:57:56 +1000 | [diff] [blame] | 601 | if (!node_allowed(ctx, node)) |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 602 | continue; |
| 603 | |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 604 | mutex_lock(&cbe_spu_info[node].list_mutex); |
| 605 | list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) { |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 606 | struct spu_context *tmp = spu->ctx; |
| 607 | |
Christoph Hellwig | c0e7b4a | 2007-09-19 14:38:12 +1000 | [diff] [blame] | 608 | if (tmp && tmp->prio > ctx->prio && |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 609 | !(tmp->flags & SPU_CREATE_NOSCHED) && |
Christoph Hellwig | fe443ef | 2007-06-29 10:57:52 +1000 | [diff] [blame] | 610 | (!victim || tmp->prio > victim->prio)) |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 611 | victim = spu->ctx; |
| 612 | } |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 613 | mutex_unlock(&cbe_spu_info[node].list_mutex); |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 614 | |
| 615 | if (victim) { |
| 616 | /* |
| 617 | * This nests ctx->state_mutex, but we always lock |
| 618 | * higher priority contexts before lower priority |
| 619 | * ones, so this is safe until we introduce |
| 620 | * priority inheritance schemes. |
Luke Browning | 9156953 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 621 | * |
| 622 | * XXX if the highest priority context is locked, |
| 623 | * this can loop a long time. Might be better to |
| 624 | * look at another context or give up after X retries. |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 625 | */ |
| 626 | if (!mutex_trylock(&victim->state_mutex)) { |
| 627 | victim = NULL; |
| 628 | goto restart; |
| 629 | } |
| 630 | |
| 631 | spu = victim->spu; |
Luke Browning | b192541 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 632 | if (!spu || victim->prio <= ctx->prio) { |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 633 | /* |
| 634 | * This race can happen because we've dropped |
Luke Browning | b192541 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 635 | * the active list mutex. Not a problem, just |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 636 | * restart the search. |
| 637 | */ |
| 638 | mutex_unlock(&victim->state_mutex); |
| 639 | victim = NULL; |
| 640 | goto restart; |
| 641 | } |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 642 | |
| 643 | mutex_lock(&cbe_spu_info[node].list_mutex); |
| 644 | cbe_spu_info[node].nr_active--; |
Christoph Hellwig | c0e7b4a | 2007-09-19 14:38:12 +1000 | [diff] [blame] | 645 | spu_unbind_context(spu, victim); |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 646 | mutex_unlock(&cbe_spu_info[node].list_mutex); |
| 647 | |
Christoph Hellwig | e9f8a0b | 2007-06-29 10:58:03 +1000 | [diff] [blame] | 648 | victim->stats.invol_ctx_switch++; |
Christoph Hellwig | fe2f896 | 2007-06-29 10:58:07 +1000 | [diff] [blame] | 649 | spu->stats.invol_ctx_switch++; |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 650 | spu_add_to_rq(victim); |
| 651 | |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 652 | mutex_unlock(&victim->state_mutex); |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 653 | |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 654 | return spu; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | return NULL; |
| 659 | } |
| 660 | |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 661 | static void __spu_schedule(struct spu *spu, struct spu_context *ctx) |
| 662 | { |
| 663 | int node = spu->node; |
| 664 | int success = 0; |
| 665 | |
| 666 | spu_set_timeslice(ctx); |
| 667 | |
| 668 | mutex_lock(&cbe_spu_info[node].list_mutex); |
| 669 | if (spu->ctx == NULL) { |
| 670 | spu_bind_context(spu, ctx); |
| 671 | cbe_spu_info[node].nr_active++; |
| 672 | spu->alloc_state = SPU_USED; |
| 673 | success = 1; |
| 674 | } |
| 675 | mutex_unlock(&cbe_spu_info[node].list_mutex); |
| 676 | |
| 677 | if (success) |
| 678 | wake_up_all(&ctx->run_wq); |
| 679 | else |
| 680 | spu_add_to_rq(ctx); |
| 681 | } |
| 682 | |
| 683 | static void spu_schedule(struct spu *spu, struct spu_context *ctx) |
| 684 | { |
Christoph Hellwig | c9101bd | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 685 | /* not a candidate for interruptible because it's called either |
| 686 | from the scheduler thread or from spu_deactivate */ |
| 687 | mutex_lock(&ctx->state_mutex); |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 688 | __spu_schedule(spu, ctx); |
| 689 | spu_release(ctx); |
| 690 | } |
| 691 | |
| 692 | static void spu_unschedule(struct spu *spu, struct spu_context *ctx) |
| 693 | { |
| 694 | int node = spu->node; |
| 695 | |
| 696 | mutex_lock(&cbe_spu_info[node].list_mutex); |
| 697 | cbe_spu_info[node].nr_active--; |
| 698 | spu->alloc_state = SPU_FREE; |
| 699 | spu_unbind_context(spu, ctx); |
| 700 | ctx->stats.invol_ctx_switch++; |
| 701 | spu->stats.invol_ctx_switch++; |
| 702 | mutex_unlock(&cbe_spu_info[node].list_mutex); |
| 703 | } |
| 704 | |
Christoph Hellwig | 52f04fc | 2007-02-13 21:54:27 +0100 | [diff] [blame] | 705 | /** |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 706 | * spu_activate - find a free spu for a context and execute it |
| 707 | * @ctx: spu context to schedule |
| 708 | * @flags: flags (currently ignored) |
| 709 | * |
Christoph Hellwig | 0887309 | 2007-04-23 21:08:06 +0200 | [diff] [blame] | 710 | * 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] | 711 | * add the context to the runqueue so it gets woken up once an spu |
| 712 | * is available. |
| 713 | */ |
Christoph Hellwig | 26bec67 | 2007-02-13 21:54:24 +0100 | [diff] [blame] | 714 | int spu_activate(struct spu_context *ctx, unsigned long flags) |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 715 | { |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 716 | struct spu *spu; |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 717 | |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 718 | /* |
| 719 | * If there are multiple threads waiting for a single context |
| 720 | * only one actually binds the context while the others will |
| 721 | * only be able to acquire the state_mutex once the context |
| 722 | * already is in runnable state. |
| 723 | */ |
| 724 | if (ctx->spu) |
| 725 | return 0; |
Christoph Hellwig | 2744997 | 2007-06-29 10:58:06 +1000 | [diff] [blame] | 726 | |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 727 | spu_activate_top: |
| 728 | if (signal_pending(current)) |
| 729 | return -ERESTARTSYS; |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 730 | |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 731 | spu = spu_get_idle(ctx); |
| 732 | /* |
| 733 | * If this is a realtime thread we try to get it running by |
| 734 | * preempting a lower priority thread. |
| 735 | */ |
| 736 | if (!spu && rt_prio(ctx->prio)) |
| 737 | spu = find_victim(ctx); |
| 738 | if (spu) { |
| 739 | unsigned long runcntl; |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 740 | |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 741 | runcntl = ctx->ops->runcntl_read(ctx); |
| 742 | __spu_schedule(spu, ctx); |
| 743 | if (runcntl & SPU_RUNCNTL_RUNNABLE) |
| 744 | spuctx_switch_state(ctx, SPU_UTIL_USER); |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | if (ctx->flags & SPU_CREATE_NOSCHED) { |
Christoph Hellwig | 50b520d | 2007-03-10 00:05:36 +0100 | [diff] [blame] | 750 | spu_prio_wait(ctx); |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 751 | goto spu_activate_top; |
| 752 | } |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 753 | |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 754 | spu_add_to_rq(ctx); |
| 755 | |
| 756 | return 0; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 757 | } |
| 758 | |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 759 | /** |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 760 | * grab_runnable_context - try to find a runnable context |
| 761 | * |
| 762 | * Remove the highest priority context on the runqueue and return it |
| 763 | * to the caller. Returns %NULL if no runnable context was found. |
| 764 | */ |
Christoph Hellwig | ea1ae59 | 2007-06-29 10:57:56 +1000 | [diff] [blame] | 765 | static struct spu_context *grab_runnable_context(int prio, int node) |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 766 | { |
Christoph Hellwig | ea1ae59 | 2007-06-29 10:57:56 +1000 | [diff] [blame] | 767 | struct spu_context *ctx; |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 768 | int best; |
| 769 | |
| 770 | spin_lock(&spu_prio->runq_lock); |
Masato Noguchi | 7e90b74 | 2007-07-20 21:39:43 +0200 | [diff] [blame] | 771 | best = find_first_bit(spu_prio->bitmap, prio); |
Christoph Hellwig | ea1ae59 | 2007-06-29 10:57:56 +1000 | [diff] [blame] | 772 | while (best < prio) { |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 773 | struct list_head *rq = &spu_prio->runq[best]; |
| 774 | |
Christoph Hellwig | ea1ae59 | 2007-06-29 10:57:56 +1000 | [diff] [blame] | 775 | list_for_each_entry(ctx, rq, rq) { |
| 776 | /* XXX(hch): check for affinity here aswell */ |
| 777 | if (__node_allowed(ctx, node)) { |
| 778 | __spu_del_from_rq(ctx); |
| 779 | goto found; |
| 780 | } |
| 781 | } |
| 782 | best++; |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 783 | } |
Christoph Hellwig | ea1ae59 | 2007-06-29 10:57:56 +1000 | [diff] [blame] | 784 | ctx = NULL; |
| 785 | found: |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 786 | spin_unlock(&spu_prio->runq_lock); |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 787 | return ctx; |
| 788 | } |
| 789 | |
| 790 | static int __spu_deactivate(struct spu_context *ctx, int force, int max_prio) |
| 791 | { |
| 792 | struct spu *spu = ctx->spu; |
| 793 | struct spu_context *new = NULL; |
| 794 | |
| 795 | if (spu) { |
Christoph Hellwig | ea1ae59 | 2007-06-29 10:57:56 +1000 | [diff] [blame] | 796 | new = grab_runnable_context(max_prio, spu->node); |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 797 | if (new || force) { |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 798 | spu_unschedule(spu, ctx); |
| 799 | if (new) { |
| 800 | if (new->flags & SPU_CREATE_NOSCHED) |
| 801 | wake_up(&new->stop_wq); |
| 802 | else { |
| 803 | spu_release(ctx); |
| 804 | spu_schedule(spu, new); |
Christoph Hellwig | c9101bd | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 805 | /* this one can't easily be made |
| 806 | interruptible */ |
| 807 | mutex_lock(&ctx->state_mutex); |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 808 | } |
| 809 | } |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 810 | } |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | return new != NULL; |
| 814 | } |
| 815 | |
| 816 | /** |
Christoph Hellwig | 678b2ff | 2007-02-13 21:54:25 +0100 | [diff] [blame] | 817 | * spu_deactivate - unbind a context from it's physical spu |
| 818 | * @ctx: spu context to unbind |
| 819 | * |
| 820 | * Unbind @ctx from the physical spu it is running on and schedule |
| 821 | * the highest priority context to run on the freed physical spu. |
| 822 | */ |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 823 | void spu_deactivate(struct spu_context *ctx) |
| 824 | { |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 825 | __spu_deactivate(ctx, 1, MAX_PRIO); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 826 | } |
| 827 | |
Christoph Hellwig | ae7b4c5 | 2007-02-13 21:54:26 +0100 | [diff] [blame] | 828 | /** |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 829 | * spu_yield - yield a physical spu if others are waiting |
Christoph Hellwig | ae7b4c5 | 2007-02-13 21:54:26 +0100 | [diff] [blame] | 830 | * @ctx: spu context to yield |
| 831 | * |
| 832 | * Check if there is a higher priority context waiting and if yes |
| 833 | * unbind @ctx from the physical spu and schedule the highest |
| 834 | * priority context to run on the freed physical spu instead. |
| 835 | */ |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 836 | void spu_yield(struct spu_context *ctx) |
| 837 | { |
Christoph Hellwig | e5c0b9e | 2007-06-05 11:25:59 +1000 | [diff] [blame] | 838 | if (!(ctx->flags & SPU_CREATE_NOSCHED)) { |
| 839 | mutex_lock(&ctx->state_mutex); |
Andre Detsch | 27ec41d | 2007-07-20 21:39:33 +0200 | [diff] [blame] | 840 | __spu_deactivate(ctx, 0, MAX_PRIO); |
Christoph Hellwig | e5c0b9e | 2007-06-05 11:25:59 +1000 | [diff] [blame] | 841 | mutex_unlock(&ctx->state_mutex); |
| 842 | } |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 843 | } |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 844 | |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 845 | static noinline void spusched_tick(struct spu_context *ctx) |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 846 | { |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 847 | struct spu_context *new = NULL; |
| 848 | struct spu *spu = NULL; |
| 849 | u32 status; |
| 850 | |
Christoph Hellwig | c9101bd | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 851 | if (spu_acquire(ctx)) |
| 852 | BUG(); /* a kernel thread never has signals pending */ |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 853 | |
| 854 | if (ctx->state != SPU_STATE_RUNNABLE) |
| 855 | goto out; |
| 856 | if (spu_stopped(ctx, &status)) |
| 857 | goto out; |
Christoph Hellwig | df09cf3 | 2007-06-29 10:57:58 +1000 | [diff] [blame] | 858 | if (ctx->flags & SPU_CREATE_NOSCHED) |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 859 | goto out; |
Christoph Hellwig | df09cf3 | 2007-06-29 10:57:58 +1000 | [diff] [blame] | 860 | if (ctx->policy == SCHED_FIFO) |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 861 | goto out; |
Christoph Hellwig | df09cf3 | 2007-06-29 10:57:58 +1000 | [diff] [blame] | 862 | |
| 863 | if (--ctx->time_slice) |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 864 | goto out; |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 865 | |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 866 | spu = ctx->spu; |
| 867 | new = grab_runnable_context(ctx->prio + 1, spu->node); |
| 868 | if (new) { |
| 869 | spu_unschedule(spu, ctx); |
| 870 | spu_add_to_rq(ctx); |
Christoph Hellwig | bb5db29 | 2007-06-04 23:26:51 +1000 | [diff] [blame] | 871 | } else { |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 872 | ctx->time_slice++; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 873 | } |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 874 | out: |
| 875 | spu_release(ctx); |
| 876 | |
| 877 | if (new) |
| 878 | spu_schedule(spu, new); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 879 | } |
| 880 | |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 881 | /** |
| 882 | * count_active_contexts - count nr of active tasks |
| 883 | * |
| 884 | * Return the number of tasks currently running or waiting to run. |
| 885 | * |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 886 | * Note that we don't take runq_lock / list_mutex here. Reading |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 887 | * a single 32bit value is atomic on powerpc, and we don't care |
| 888 | * about memory ordering issues here. |
| 889 | */ |
| 890 | static unsigned long count_active_contexts(void) |
| 891 | { |
| 892 | int nr_active = 0, node; |
| 893 | |
| 894 | for (node = 0; node < MAX_NUMNODES; node++) |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 895 | nr_active += cbe_spu_info[node].nr_active; |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 896 | nr_active += spu_prio->nr_waiting; |
| 897 | |
| 898 | return nr_active; |
| 899 | } |
| 900 | |
| 901 | /** |
Aegis Lin | 90608a2 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 902 | * spu_calc_load - update the avenrun load estimates. |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 903 | * |
| 904 | * No locking against reading these values from userspace, as for |
| 905 | * the CPU loadavg code. |
| 906 | */ |
Aegis Lin | 90608a2 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 907 | static void spu_calc_load(void) |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 908 | { |
| 909 | unsigned long active_tasks; /* fixed-point */ |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 910 | |
Aegis Lin | 90608a2 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 911 | active_tasks = count_active_contexts() * FIXED_1; |
| 912 | CALC_LOAD(spu_avenrun[0], EXP_1, active_tasks); |
| 913 | CALC_LOAD(spu_avenrun[1], EXP_5, active_tasks); |
| 914 | CALC_LOAD(spu_avenrun[2], EXP_15, active_tasks); |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 915 | } |
| 916 | |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 917 | static void spusched_wake(unsigned long data) |
| 918 | { |
| 919 | mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK); |
| 920 | wake_up_process(spusched_task); |
Aegis Lin | 90608a2 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | static void spuloadavg_wake(unsigned long data) |
| 924 | { |
| 925 | mod_timer(&spuloadavg_timer, jiffies + LOAD_FREQ); |
| 926 | spu_calc_load(); |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | static int spusched_thread(void *unused) |
| 930 | { |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 931 | struct spu *spu; |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 932 | int node; |
| 933 | |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 934 | while (!kthread_should_stop()) { |
| 935 | set_current_state(TASK_INTERRUPTIBLE); |
| 936 | schedule(); |
| 937 | for (node = 0; node < MAX_NUMNODES; node++) { |
Luke Browning | e65c2f6 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 938 | struct mutex *mtx = &cbe_spu_info[node].list_mutex; |
| 939 | |
| 940 | mutex_lock(mtx); |
| 941 | list_for_each_entry(spu, &cbe_spu_info[node].spus, |
| 942 | cbe_list) { |
| 943 | struct spu_context *ctx = spu->ctx; |
| 944 | |
| 945 | if (ctx) { |
| 946 | mutex_unlock(mtx); |
| 947 | spusched_tick(ctx); |
| 948 | mutex_lock(mtx); |
| 949 | } |
| 950 | } |
| 951 | mutex_unlock(mtx); |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 955 | return 0; |
| 956 | } |
| 957 | |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 958 | void spuctx_switch_state(struct spu_context *ctx, |
| 959 | enum spu_utilization_state new_state) |
| 960 | { |
| 961 | unsigned long long curtime; |
| 962 | signed long long delta; |
| 963 | struct timespec ts; |
| 964 | struct spu *spu; |
| 965 | enum spu_utilization_state old_state; |
| 966 | |
| 967 | ktime_get_ts(&ts); |
| 968 | curtime = timespec_to_ns(&ts); |
| 969 | delta = curtime - ctx->stats.tstamp; |
| 970 | |
| 971 | WARN_ON(!mutex_is_locked(&ctx->state_mutex)); |
| 972 | WARN_ON(delta < 0); |
| 973 | |
| 974 | spu = ctx->spu; |
| 975 | old_state = ctx->stats.util_state; |
| 976 | ctx->stats.util_state = new_state; |
| 977 | ctx->stats.tstamp = curtime; |
| 978 | |
| 979 | /* |
| 980 | * Update the physical SPU utilization statistics. |
| 981 | */ |
| 982 | if (spu) { |
| 983 | ctx->stats.times[old_state] += delta; |
| 984 | spu->stats.times[old_state] += delta; |
| 985 | spu->stats.util_state = new_state; |
| 986 | spu->stats.tstamp = curtime; |
| 987 | } |
| 988 | } |
| 989 | |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 990 | #define LOAD_INT(x) ((x) >> FSHIFT) |
| 991 | #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100) |
| 992 | |
| 993 | static int show_spu_loadavg(struct seq_file *s, void *private) |
| 994 | { |
| 995 | int a, b, c; |
| 996 | |
| 997 | a = spu_avenrun[0] + (FIXED_1/200); |
| 998 | b = spu_avenrun[1] + (FIXED_1/200); |
| 999 | c = spu_avenrun[2] + (FIXED_1/200); |
| 1000 | |
| 1001 | /* |
| 1002 | * Note that last_pid doesn't really make much sense for the |
Julio M. Merino Vidal | 9b1d21f | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 1003 | * SPU loadavg (it even seems very odd on the CPU side...), |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 1004 | * but we include it here to have a 100% compatible interface. |
| 1005 | */ |
| 1006 | seq_printf(s, "%d.%02d %d.%02d %d.%02d %ld/%d %d\n", |
| 1007 | LOAD_INT(a), LOAD_FRAC(a), |
| 1008 | LOAD_INT(b), LOAD_FRAC(b), |
| 1009 | LOAD_INT(c), LOAD_FRAC(c), |
| 1010 | count_active_contexts(), |
| 1011 | atomic_read(&nr_spu_contexts), |
| 1012 | current->nsproxy->pid_ns->last_pid); |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | static int spu_loadavg_open(struct inode *inode, struct file *file) |
| 1017 | { |
| 1018 | return single_open(file, show_spu_loadavg, NULL); |
| 1019 | } |
| 1020 | |
| 1021 | static const struct file_operations spu_loadavg_fops = { |
| 1022 | .open = spu_loadavg_open, |
| 1023 | .read = seq_read, |
| 1024 | .llseek = seq_lseek, |
| 1025 | .release = single_release, |
| 1026 | }; |
| 1027 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 1028 | int __init spu_sched_init(void) |
| 1029 | { |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 1030 | struct proc_dir_entry *entry; |
| 1031 | int err = -ENOMEM, i; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 1032 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 1033 | spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL); |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 1034 | if (!spu_prio) |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 1035 | goto out; |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 1036 | |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 1037 | for (i = 0; i < MAX_PRIO; i++) { |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 1038 | INIT_LIST_HEAD(&spu_prio->runq[i]); |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 1039 | __clear_bit(i, spu_prio->bitmap); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 1040 | } |
Christoph Hellwig | 079cdb6 | 2007-02-13 21:54:23 +0100 | [diff] [blame] | 1041 | spin_lock_init(&spu_prio->runq_lock); |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 1042 | |
Christoph Hellwig | c77239b | 2007-06-29 10:58:05 +1000 | [diff] [blame] | 1043 | setup_timer(&spusched_timer, spusched_wake, 0); |
Aegis Lin | 90608a2 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 1044 | setup_timer(&spuloadavg_timer, spuloadavg_wake, 0); |
Christoph Hellwig | c77239b | 2007-06-29 10:58:05 +1000 | [diff] [blame] | 1045 | |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 1046 | spusched_task = kthread_run(spusched_thread, NULL, "spusched"); |
| 1047 | if (IS_ERR(spusched_task)) { |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 1048 | err = PTR_ERR(spusched_task); |
| 1049 | goto out_free_spu_prio; |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 1050 | } |
Jeremy Kerr | f3f59be | 2007-06-29 10:57:54 +1000 | [diff] [blame] | 1051 | |
Aegis Lin | 90608a2 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 1052 | mod_timer(&spuloadavg_timer, 0); |
| 1053 | |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 1054 | entry = create_proc_entry("spu_loadavg", 0, NULL); |
| 1055 | if (!entry) |
| 1056 | goto out_stop_kthread; |
| 1057 | entry->proc_fops = &spu_loadavg_fops; |
| 1058 | |
Jeremy Kerr | f3f59be | 2007-06-29 10:57:54 +1000 | [diff] [blame] | 1059 | pr_debug("spusched: tick: %d, min ticks: %d, default ticks: %d\n", |
| 1060 | SPUSCHED_TICK, MIN_SPU_TIMESLICE, DEF_SPU_TIMESLICE); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 1061 | return 0; |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 1062 | |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 1063 | out_stop_kthread: |
| 1064 | kthread_stop(spusched_task); |
| 1065 | out_free_spu_prio: |
| 1066 | kfree(spu_prio); |
| 1067 | out: |
| 1068 | return err; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 1069 | } |
| 1070 | |
Sebastian Siewior | d145031 | 2007-07-20 21:39:29 +0200 | [diff] [blame] | 1071 | void spu_sched_exit(void) |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 1072 | { |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 1073 | struct spu *spu; |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 1074 | int node; |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 1075 | |
Christoph Hellwig | 65de66f | 2007-06-29 10:58:02 +1000 | [diff] [blame] | 1076 | remove_proc_entry("spu_loadavg", NULL); |
| 1077 | |
Christoph Hellwig | c77239b | 2007-06-29 10:58:05 +1000 | [diff] [blame] | 1078 | del_timer_sync(&spusched_timer); |
Aegis Lin | 90608a2 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 1079 | del_timer_sync(&spuloadavg_timer); |
Christoph Hellwig | 3790180 | 2007-06-29 10:57:51 +1000 | [diff] [blame] | 1080 | kthread_stop(spusched_task); |
| 1081 | |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 1082 | for (node = 0; node < MAX_NUMNODES; node++) { |
Christoph Hellwig | 486acd4 | 2007-07-20 21:39:54 +0200 | [diff] [blame] | 1083 | mutex_lock(&cbe_spu_info[node].list_mutex); |
| 1084 | list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) |
| 1085 | if (spu->alloc_state != SPU_FREE) |
| 1086 | spu->alloc_state = SPU_FREE; |
| 1087 | mutex_unlock(&cbe_spu_info[node].list_mutex); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 1088 | } |
Mark Nutter | a68cf98 | 2006-10-04 17:26:12 +0200 | [diff] [blame] | 1089 | kfree(spu_prio); |
Arnd Bergmann | 8b3d666 | 2005-11-15 15:53:52 -0500 | [diff] [blame] | 1090 | } |