Path: blob/master/arch/powerpc/platforms/cell/spufs/sched.c
26498 views
// SPDX-License-Identifier: GPL-2.0-or-later1/* sched.c - SPU scheduler.2*3* Copyright (C) IBM 20054* Author: Mark Nutter <[email protected]>5*6* 2006-03-31 NUMA domains added.7*/89#undef DEBUG1011#include <linux/errno.h>12#include <linux/sched/signal.h>13#include <linux/sched/loadavg.h>14#include <linux/sched/rt.h>15#include <linux/kernel.h>16#include <linux/mm.h>17#include <linux/slab.h>18#include <linux/completion.h>19#include <linux/vmalloc.h>20#include <linux/smp.h>21#include <linux/stddef.h>22#include <linux/unistd.h>23#include <linux/numa.h>24#include <linux/mutex.h>25#include <linux/notifier.h>26#include <linux/kthread.h>27#include <linux/pid_namespace.h>28#include <linux/proc_fs.h>29#include <linux/seq_file.h>3031#include <asm/io.h>32#include <asm/mmu_context.h>33#include <asm/spu.h>34#include <asm/spu_csa.h>35#include <asm/spu_priv1.h>36#include "spufs.h"37#define CREATE_TRACE_POINTS38#include "sputrace.h"3940struct spu_prio_array {41DECLARE_BITMAP(bitmap, MAX_PRIO);42struct list_head runq[MAX_PRIO];43spinlock_t runq_lock;44int nr_waiting;45};4647static unsigned long spu_avenrun[3];48static struct spu_prio_array *spu_prio;49static struct task_struct *spusched_task;50static struct timer_list spusched_timer;51static struct timer_list spuloadavg_timer;5253/*54* Priority of a normal, non-rt, non-niced'd process (aka nice level 0).55*/56#define NORMAL_PRIO 1205758/*59* Frequency of the spu scheduler tick. By default we do one SPU scheduler60* tick for every 10 CPU scheduler ticks.61*/62#define SPUSCHED_TICK (10)6364/*65* These are the 'tuning knobs' of the scheduler:66*67* Minimum timeslice is 5 msecs (or 1 spu scheduler tick, whichever is68* larger), default timeslice is 100 msecs, maximum timeslice is 800 msecs.69*/70#define MIN_SPU_TIMESLICE max(5 * HZ / (1000 * SPUSCHED_TICK), 1)71#define DEF_SPU_TIMESLICE (100 * HZ / (1000 * SPUSCHED_TICK))7273#define SCALE_PRIO(x, prio) \74max(x * (MAX_PRIO - prio) / (NICE_WIDTH / 2), MIN_SPU_TIMESLICE)7576/*77* scale user-nice values [ -20 ... 0 ... 19 ] to time slice values:78* [800ms ... 100ms ... 5ms]79*80* The higher a thread's priority, the bigger timeslices81* it gets during one round of execution. But even the lowest82* priority thread gets MIN_TIMESLICE worth of execution time.83*/84void spu_set_timeslice(struct spu_context *ctx)85{86if (ctx->prio < NORMAL_PRIO)87ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE * 4, ctx->prio);88else89ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE, ctx->prio);90}9192/*93* Update scheduling information from the owning thread.94*/95void __spu_update_sched_info(struct spu_context *ctx)96{97/*98* assert that the context is not on the runqueue, so it is safe99* to change its scheduling parameters.100*/101BUG_ON(!list_empty(&ctx->rq));102103/*104* 32-Bit assignments are atomic on powerpc, and we don't care about105* memory ordering here because retrieving the controlling thread is106* per definition racy.107*/108ctx->tid = current->pid;109110/*111* We do our own priority calculations, so we normally want112* ->static_prio to start with. Unfortunately this field113* contains junk for threads with a realtime scheduling114* policy so we have to look at ->prio in this case.115*/116if (rt_prio(current->prio))117ctx->prio = current->prio;118else119ctx->prio = current->static_prio;120ctx->policy = current->policy;121122/*123* TO DO: the context may be loaded, so we may need to activate124* it again on a different node. But it shouldn't hurt anything125* to update its parameters, because we know that the scheduler126* is not actively looking at this field, since it is not on the127* runqueue. The context will be rescheduled on the proper node128* if it is timesliced or preempted.129*/130cpumask_copy(&ctx->cpus_allowed, current->cpus_ptr);131132/* Save the current cpu id for spu interrupt routing. */133ctx->last_ran = raw_smp_processor_id();134}135136void spu_update_sched_info(struct spu_context *ctx)137{138int node;139140if (ctx->state == SPU_STATE_RUNNABLE) {141node = ctx->spu->node;142143/*144* Take list_mutex to sync with find_victim().145*/146mutex_lock(&cbe_spu_info[node].list_mutex);147__spu_update_sched_info(ctx);148mutex_unlock(&cbe_spu_info[node].list_mutex);149} else {150__spu_update_sched_info(ctx);151}152}153154static int __node_allowed(struct spu_context *ctx, int node)155{156if (nr_cpus_node(node)) {157const struct cpumask *mask = cpumask_of_node(node);158159if (cpumask_intersects(mask, &ctx->cpus_allowed))160return 1;161}162163return 0;164}165166static int node_allowed(struct spu_context *ctx, int node)167{168int rval;169170spin_lock(&spu_prio->runq_lock);171rval = __node_allowed(ctx, node);172spin_unlock(&spu_prio->runq_lock);173174return rval;175}176177void do_notify_spus_active(void)178{179int node;180181/*182* Wake up the active spu_contexts.183*/184for_each_online_node(node) {185struct spu *spu;186187mutex_lock(&cbe_spu_info[node].list_mutex);188list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {189if (spu->alloc_state != SPU_FREE) {190struct spu_context *ctx = spu->ctx;191set_bit(SPU_SCHED_NOTIFY_ACTIVE,192&ctx->sched_flags);193mb();194wake_up_all(&ctx->stop_wq);195}196}197mutex_unlock(&cbe_spu_info[node].list_mutex);198}199}200201/**202* spu_bind_context - bind spu context to physical spu203* @spu: physical spu to bind to204* @ctx: context to bind205*/206static void spu_bind_context(struct spu *spu, struct spu_context *ctx)207{208spu_context_trace(spu_bind_context__enter, ctx, spu);209210spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);211212if (ctx->flags & SPU_CREATE_NOSCHED)213atomic_inc(&cbe_spu_info[spu->node].reserved_spus);214215ctx->stats.slb_flt_base = spu->stats.slb_flt;216ctx->stats.class2_intr_base = spu->stats.class2_intr;217218spu_associate_mm(spu, ctx->owner);219220spin_lock_irq(&spu->register_lock);221spu->ctx = ctx;222spu->flags = 0;223ctx->spu = spu;224ctx->ops = &spu_hw_ops;225spu->pid = current->pid;226spu->tgid = current->tgid;227spu->ibox_callback = spufs_ibox_callback;228spu->wbox_callback = spufs_wbox_callback;229spu->stop_callback = spufs_stop_callback;230spu->mfc_callback = spufs_mfc_callback;231spin_unlock_irq(&spu->register_lock);232233spu_unmap_mappings(ctx);234235spu_switch_log_notify(spu, ctx, SWITCH_LOG_START, 0);236spu_restore(&ctx->csa, spu);237spu->timestamp = jiffies;238ctx->state = SPU_STATE_RUNNABLE;239240spuctx_switch_state(ctx, SPU_UTIL_USER);241}242243/*244* Must be used with the list_mutex held.245*/246static inline int sched_spu(struct spu *spu)247{248BUG_ON(!mutex_is_locked(&cbe_spu_info[spu->node].list_mutex));249250return (!spu->ctx || !(spu->ctx->flags & SPU_CREATE_NOSCHED));251}252253static void aff_merge_remaining_ctxs(struct spu_gang *gang)254{255struct spu_context *ctx;256257list_for_each_entry(ctx, &gang->aff_list_head, aff_list) {258if (list_empty(&ctx->aff_list))259list_add(&ctx->aff_list, &gang->aff_list_head);260}261gang->aff_flags |= AFF_MERGED;262}263264static void aff_set_offsets(struct spu_gang *gang)265{266struct spu_context *ctx;267int offset;268269offset = -1;270list_for_each_entry_reverse(ctx, &gang->aff_ref_ctx->aff_list,271aff_list) {272if (&ctx->aff_list == &gang->aff_list_head)273break;274ctx->aff_offset = offset--;275}276277offset = 0;278list_for_each_entry(ctx, gang->aff_ref_ctx->aff_list.prev, aff_list) {279if (&ctx->aff_list == &gang->aff_list_head)280break;281ctx->aff_offset = offset++;282}283284gang->aff_flags |= AFF_OFFSETS_SET;285}286287static struct spu *aff_ref_location(struct spu_context *ctx, int mem_aff,288int group_size, int lowest_offset)289{290struct spu *spu;291int node, n;292293/*294* TODO: A better algorithm could be used to find a good spu to be295* used as reference location for the ctxs chain.296*/297node = cpu_to_node(raw_smp_processor_id());298for (n = 0; n < MAX_NUMNODES; n++, node++) {299/*300* "available_spus" counts how many spus are not potentially301* going to be used by other affinity gangs whose reference302* context is already in place. Although this code seeks to303* avoid having affinity gangs with a summed amount of304* contexts bigger than the amount of spus in the node,305* this may happen sporadically. In this case, available_spus306* becomes negative, which is harmless.307*/308int available_spus;309310node = (node < MAX_NUMNODES) ? node : 0;311if (!node_allowed(ctx, node))312continue;313314available_spus = 0;315mutex_lock(&cbe_spu_info[node].list_mutex);316list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {317if (spu->ctx && spu->ctx->gang && !spu->ctx->aff_offset318&& spu->ctx->gang->aff_ref_spu)319available_spus -= spu->ctx->gang->contexts;320available_spus++;321}322if (available_spus < ctx->gang->contexts) {323mutex_unlock(&cbe_spu_info[node].list_mutex);324continue;325}326327list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {328if ((!mem_aff || spu->has_mem_affinity) &&329sched_spu(spu)) {330mutex_unlock(&cbe_spu_info[node].list_mutex);331return spu;332}333}334mutex_unlock(&cbe_spu_info[node].list_mutex);335}336return NULL;337}338339static void aff_set_ref_point_location(struct spu_gang *gang)340{341int mem_aff, gs, lowest_offset;342struct spu_context *tmp, *ctx;343344mem_aff = gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM;345lowest_offset = 0;346gs = 0;347348list_for_each_entry(tmp, &gang->aff_list_head, aff_list)349gs++;350351list_for_each_entry_reverse(ctx, &gang->aff_ref_ctx->aff_list,352aff_list) {353if (&ctx->aff_list == &gang->aff_list_head)354break;355lowest_offset = ctx->aff_offset;356}357358gang->aff_ref_spu = aff_ref_location(gang->aff_ref_ctx, mem_aff, gs,359lowest_offset);360}361362static struct spu *ctx_location(struct spu *ref, int offset, int node)363{364struct spu *spu;365366spu = NULL;367if (offset >= 0) {368list_for_each_entry(spu, ref->aff_list.prev, aff_list) {369BUG_ON(spu->node != node);370if (offset == 0)371break;372if (sched_spu(spu))373offset--;374}375} else {376list_for_each_entry_reverse(spu, ref->aff_list.next, aff_list) {377BUG_ON(spu->node != node);378if (offset == 0)379break;380if (sched_spu(spu))381offset++;382}383}384385return spu;386}387388/*389* affinity_check is called each time a context is going to be scheduled.390* It returns the spu ptr on which the context must run.391*/392static int has_affinity(struct spu_context *ctx)393{394struct spu_gang *gang = ctx->gang;395396if (list_empty(&ctx->aff_list))397return 0;398399if (atomic_read(&ctx->gang->aff_sched_count) == 0)400ctx->gang->aff_ref_spu = NULL;401402if (!gang->aff_ref_spu) {403if (!(gang->aff_flags & AFF_MERGED))404aff_merge_remaining_ctxs(gang);405if (!(gang->aff_flags & AFF_OFFSETS_SET))406aff_set_offsets(gang);407aff_set_ref_point_location(gang);408}409410return gang->aff_ref_spu != NULL;411}412413/**414* spu_unbind_context - unbind spu context from physical spu415* @spu: physical spu to unbind from416* @ctx: context to unbind417*/418static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)419{420u32 status;421422spu_context_trace(spu_unbind_context__enter, ctx, spu);423424spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);425426if (spu->ctx->flags & SPU_CREATE_NOSCHED)427atomic_dec(&cbe_spu_info[spu->node].reserved_spus);428429if (ctx->gang)430/*431* If ctx->gang->aff_sched_count is positive, SPU affinity is432* being considered in this gang. Using atomic_dec_if_positive433* allow us to skip an explicit check for affinity in this gang434*/435atomic_dec_if_positive(&ctx->gang->aff_sched_count);436437spu_unmap_mappings(ctx);438spu_save(&ctx->csa, spu);439spu_switch_log_notify(spu, ctx, SWITCH_LOG_STOP, 0);440441spin_lock_irq(&spu->register_lock);442spu->timestamp = jiffies;443ctx->state = SPU_STATE_SAVED;444spu->ibox_callback = NULL;445spu->wbox_callback = NULL;446spu->stop_callback = NULL;447spu->mfc_callback = NULL;448spu->pid = 0;449spu->tgid = 0;450ctx->ops = &spu_backing_ops;451spu->flags = 0;452spu->ctx = NULL;453spin_unlock_irq(&spu->register_lock);454455spu_associate_mm(spu, NULL);456457ctx->stats.slb_flt +=458(spu->stats.slb_flt - ctx->stats.slb_flt_base);459ctx->stats.class2_intr +=460(spu->stats.class2_intr - ctx->stats.class2_intr_base);461462/* This maps the underlying spu state to idle */463spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED);464ctx->spu = NULL;465466if (spu_stopped(ctx, &status))467wake_up_all(&ctx->stop_wq);468}469470/**471* spu_add_to_rq - add a context to the runqueue472* @ctx: context to add473*/474static void __spu_add_to_rq(struct spu_context *ctx)475{476/*477* Unfortunately this code path can be called from multiple threads478* on behalf of a single context due to the way the problem state479* mmap support works.480*481* Fortunately we need to wake up all these threads at the same time482* and can simply skip the runqueue addition for every but the first483* thread getting into this codepath.484*485* It's still quite hacky, and long-term we should proxy all other486* threads through the owner thread so that spu_run is in control487* of all the scheduling activity for a given context.488*/489if (list_empty(&ctx->rq)) {490list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]);491set_bit(ctx->prio, spu_prio->bitmap);492if (!spu_prio->nr_waiting++)493mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);494}495}496497static void spu_add_to_rq(struct spu_context *ctx)498{499spin_lock(&spu_prio->runq_lock);500__spu_add_to_rq(ctx);501spin_unlock(&spu_prio->runq_lock);502}503504static void __spu_del_from_rq(struct spu_context *ctx)505{506int prio = ctx->prio;507508if (!list_empty(&ctx->rq)) {509if (!--spu_prio->nr_waiting)510timer_delete(&spusched_timer);511list_del_init(&ctx->rq);512513if (list_empty(&spu_prio->runq[prio]))514clear_bit(prio, spu_prio->bitmap);515}516}517518void spu_del_from_rq(struct spu_context *ctx)519{520spin_lock(&spu_prio->runq_lock);521__spu_del_from_rq(ctx);522spin_unlock(&spu_prio->runq_lock);523}524525static void spu_prio_wait(struct spu_context *ctx)526{527DEFINE_WAIT(wait);528529/*530* The caller must explicitly wait for a context to be loaded531* if the nosched flag is set. If NOSCHED is not set, the caller532* queues the context and waits for an spu event or error.533*/534BUG_ON(!(ctx->flags & SPU_CREATE_NOSCHED));535536spin_lock(&spu_prio->runq_lock);537prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);538if (!signal_pending(current)) {539__spu_add_to_rq(ctx);540spin_unlock(&spu_prio->runq_lock);541mutex_unlock(&ctx->state_mutex);542schedule();543mutex_lock(&ctx->state_mutex);544spin_lock(&spu_prio->runq_lock);545__spu_del_from_rq(ctx);546}547spin_unlock(&spu_prio->runq_lock);548__set_current_state(TASK_RUNNING);549remove_wait_queue(&ctx->stop_wq, &wait);550}551552static struct spu *spu_get_idle(struct spu_context *ctx)553{554struct spu *spu, *aff_ref_spu;555int node, n;556557spu_context_nospu_trace(spu_get_idle__enter, ctx);558559if (ctx->gang) {560mutex_lock(&ctx->gang->aff_mutex);561if (has_affinity(ctx)) {562aff_ref_spu = ctx->gang->aff_ref_spu;563atomic_inc(&ctx->gang->aff_sched_count);564mutex_unlock(&ctx->gang->aff_mutex);565node = aff_ref_spu->node;566567mutex_lock(&cbe_spu_info[node].list_mutex);568spu = ctx_location(aff_ref_spu, ctx->aff_offset, node);569if (spu && spu->alloc_state == SPU_FREE)570goto found;571mutex_unlock(&cbe_spu_info[node].list_mutex);572573atomic_dec(&ctx->gang->aff_sched_count);574goto not_found;575}576mutex_unlock(&ctx->gang->aff_mutex);577}578node = cpu_to_node(raw_smp_processor_id());579for (n = 0; n < MAX_NUMNODES; n++, node++) {580node = (node < MAX_NUMNODES) ? node : 0;581if (!node_allowed(ctx, node))582continue;583584mutex_lock(&cbe_spu_info[node].list_mutex);585list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {586if (spu->alloc_state == SPU_FREE)587goto found;588}589mutex_unlock(&cbe_spu_info[node].list_mutex);590}591592not_found:593spu_context_nospu_trace(spu_get_idle__not_found, ctx);594return NULL;595596found:597spu->alloc_state = SPU_USED;598mutex_unlock(&cbe_spu_info[node].list_mutex);599spu_context_trace(spu_get_idle__found, ctx, spu);600spu_init_channels(spu);601return spu;602}603604/**605* find_victim - find a lower priority context to preempt606* @ctx: candidate context for running607*608* Returns the freed physical spu to run the new context on.609*/610static struct spu *find_victim(struct spu_context *ctx)611{612struct spu_context *victim = NULL;613struct spu *spu;614int node, n;615616spu_context_nospu_trace(spu_find_victim__enter, ctx);617618/*619* Look for a possible preemption candidate on the local node first.620* If there is no candidate look at the other nodes. This isn't621* exactly fair, but so far the whole spu scheduler tries to keep622* a strong node affinity. We might want to fine-tune this in623* the future.624*/625restart:626node = cpu_to_node(raw_smp_processor_id());627for (n = 0; n < MAX_NUMNODES; n++, node++) {628node = (node < MAX_NUMNODES) ? node : 0;629if (!node_allowed(ctx, node))630continue;631632mutex_lock(&cbe_spu_info[node].list_mutex);633list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {634struct spu_context *tmp = spu->ctx;635636if (tmp && tmp->prio > ctx->prio &&637!(tmp->flags & SPU_CREATE_NOSCHED) &&638(!victim || tmp->prio > victim->prio)) {639victim = spu->ctx;640}641}642if (victim)643get_spu_context(victim);644mutex_unlock(&cbe_spu_info[node].list_mutex);645646if (victim) {647/*648* This nests ctx->state_mutex, but we always lock649* higher priority contexts before lower priority650* ones, so this is safe until we introduce651* priority inheritance schemes.652*653* XXX if the highest priority context is locked,654* this can loop a long time. Might be better to655* look at another context or give up after X retries.656*/657if (!mutex_trylock(&victim->state_mutex)) {658put_spu_context(victim);659victim = NULL;660goto restart;661}662663spu = victim->spu;664if (!spu || victim->prio <= ctx->prio) {665/*666* This race can happen because we've dropped667* the active list mutex. Not a problem, just668* restart the search.669*/670mutex_unlock(&victim->state_mutex);671put_spu_context(victim);672victim = NULL;673goto restart;674}675676spu_context_trace(__spu_deactivate__unload, ctx, spu);677678mutex_lock(&cbe_spu_info[node].list_mutex);679cbe_spu_info[node].nr_active--;680spu_unbind_context(spu, victim);681mutex_unlock(&cbe_spu_info[node].list_mutex);682683victim->stats.invol_ctx_switch++;684spu->stats.invol_ctx_switch++;685if (test_bit(SPU_SCHED_SPU_RUN, &victim->sched_flags))686spu_add_to_rq(victim);687688mutex_unlock(&victim->state_mutex);689put_spu_context(victim);690691return spu;692}693}694695return NULL;696}697698static void __spu_schedule(struct spu *spu, struct spu_context *ctx)699{700int node = spu->node;701int success = 0;702703spu_set_timeslice(ctx);704705mutex_lock(&cbe_spu_info[node].list_mutex);706if (spu->ctx == NULL) {707spu_bind_context(spu, ctx);708cbe_spu_info[node].nr_active++;709spu->alloc_state = SPU_USED;710success = 1;711}712mutex_unlock(&cbe_spu_info[node].list_mutex);713714if (success)715wake_up_all(&ctx->run_wq);716else717spu_add_to_rq(ctx);718}719720static void spu_schedule(struct spu *spu, struct spu_context *ctx)721{722/* not a candidate for interruptible because it's called either723from the scheduler thread or from spu_deactivate */724mutex_lock(&ctx->state_mutex);725if (ctx->state == SPU_STATE_SAVED)726__spu_schedule(spu, ctx);727spu_release(ctx);728}729730/**731* spu_unschedule - remove a context from a spu, and possibly release it.732* @spu: The SPU to unschedule from733* @ctx: The context currently scheduled on the SPU734* @free_spu Whether to free the SPU for other contexts735*736* Unbinds the context @ctx from the SPU @spu. If @free_spu is non-zero, the737* SPU is made available for other contexts (ie, may be returned by738* spu_get_idle). If this is zero, the caller is expected to schedule another739* context to this spu.740*741* Should be called with ctx->state_mutex held.742*/743static void spu_unschedule(struct spu *spu, struct spu_context *ctx,744int free_spu)745{746int node = spu->node;747748mutex_lock(&cbe_spu_info[node].list_mutex);749cbe_spu_info[node].nr_active--;750if (free_spu)751spu->alloc_state = SPU_FREE;752spu_unbind_context(spu, ctx);753ctx->stats.invol_ctx_switch++;754spu->stats.invol_ctx_switch++;755mutex_unlock(&cbe_spu_info[node].list_mutex);756}757758/**759* spu_activate - find a free spu for a context and execute it760* @ctx: spu context to schedule761* @flags: flags (currently ignored)762*763* Tries to find a free spu to run @ctx. If no free spu is available764* add the context to the runqueue so it gets woken up once an spu765* is available.766*/767int spu_activate(struct spu_context *ctx, unsigned long flags)768{769struct spu *spu;770771/*772* If there are multiple threads waiting for a single context773* only one actually binds the context while the others will774* only be able to acquire the state_mutex once the context775* already is in runnable state.776*/777if (ctx->spu)778return 0;779780spu_activate_top:781if (signal_pending(current))782return -ERESTARTSYS;783784spu = spu_get_idle(ctx);785/*786* If this is a realtime thread we try to get it running by787* preempting a lower priority thread.788*/789if (!spu && rt_prio(ctx->prio))790spu = find_victim(ctx);791if (spu) {792unsigned long runcntl;793794runcntl = ctx->ops->runcntl_read(ctx);795__spu_schedule(spu, ctx);796if (runcntl & SPU_RUNCNTL_RUNNABLE)797spuctx_switch_state(ctx, SPU_UTIL_USER);798799return 0;800}801802if (ctx->flags & SPU_CREATE_NOSCHED) {803spu_prio_wait(ctx);804goto spu_activate_top;805}806807spu_add_to_rq(ctx);808809return 0;810}811812/**813* grab_runnable_context - try to find a runnable context814*815* Remove the highest priority context on the runqueue and return it816* to the caller. Returns %NULL if no runnable context was found.817*/818static struct spu_context *grab_runnable_context(int prio, int node)819{820struct spu_context *ctx;821int best;822823spin_lock(&spu_prio->runq_lock);824best = find_first_bit(spu_prio->bitmap, prio);825while (best < prio) {826struct list_head *rq = &spu_prio->runq[best];827828list_for_each_entry(ctx, rq, rq) {829/* XXX(hch): check for affinity here as well */830if (__node_allowed(ctx, node)) {831__spu_del_from_rq(ctx);832goto found;833}834}835best++;836}837ctx = NULL;838found:839spin_unlock(&spu_prio->runq_lock);840return ctx;841}842843static int __spu_deactivate(struct spu_context *ctx, int force, int max_prio)844{845struct spu *spu = ctx->spu;846struct spu_context *new = NULL;847848if (spu) {849new = grab_runnable_context(max_prio, spu->node);850if (new || force) {851spu_unschedule(spu, ctx, new == NULL);852if (new) {853if (new->flags & SPU_CREATE_NOSCHED)854wake_up(&new->stop_wq);855else {856spu_release(ctx);857spu_schedule(spu, new);858/* this one can't easily be made859interruptible */860mutex_lock(&ctx->state_mutex);861}862}863}864}865866return new != NULL;867}868869/**870* spu_deactivate - unbind a context from its physical spu871* @ctx: spu context to unbind872*873* Unbind @ctx from the physical spu it is running on and schedule874* the highest priority context to run on the freed physical spu.875*/876void spu_deactivate(struct spu_context *ctx)877{878spu_context_nospu_trace(spu_deactivate__enter, ctx);879__spu_deactivate(ctx, 1, MAX_PRIO);880}881882/**883* spu_yield - yield a physical spu if others are waiting884* @ctx: spu context to yield885*886* Check if there is a higher priority context waiting and if yes887* unbind @ctx from the physical spu and schedule the highest888* priority context to run on the freed physical spu instead.889*/890void spu_yield(struct spu_context *ctx)891{892spu_context_nospu_trace(spu_yield__enter, ctx);893if (!(ctx->flags & SPU_CREATE_NOSCHED)) {894mutex_lock(&ctx->state_mutex);895__spu_deactivate(ctx, 0, MAX_PRIO);896mutex_unlock(&ctx->state_mutex);897}898}899900static noinline void spusched_tick(struct spu_context *ctx)901{902struct spu_context *new = NULL;903struct spu *spu = NULL;904905if (spu_acquire(ctx))906BUG(); /* a kernel thread never has signals pending */907908if (ctx->state != SPU_STATE_RUNNABLE)909goto out;910if (ctx->flags & SPU_CREATE_NOSCHED)911goto out;912if (ctx->policy == SCHED_FIFO)913goto out;914915if (--ctx->time_slice && test_bit(SPU_SCHED_SPU_RUN, &ctx->sched_flags))916goto out;917918spu = ctx->spu;919920spu_context_trace(spusched_tick__preempt, ctx, spu);921922new = grab_runnable_context(ctx->prio + 1, spu->node);923if (new) {924spu_unschedule(spu, ctx, 0);925if (test_bit(SPU_SCHED_SPU_RUN, &ctx->sched_flags))926spu_add_to_rq(ctx);927} else {928spu_context_nospu_trace(spusched_tick__newslice, ctx);929if (!ctx->time_slice)930ctx->time_slice++;931}932out:933spu_release(ctx);934935if (new)936spu_schedule(spu, new);937}938939/**940* count_active_contexts - count nr of active tasks941*942* Return the number of tasks currently running or waiting to run.943*944* Note that we don't take runq_lock / list_mutex here. Reading945* a single 32bit value is atomic on powerpc, and we don't care946* about memory ordering issues here.947*/948static unsigned long count_active_contexts(void)949{950int nr_active = 0, node;951952for (node = 0; node < MAX_NUMNODES; node++)953nr_active += cbe_spu_info[node].nr_active;954nr_active += spu_prio->nr_waiting;955956return nr_active;957}958959/**960* spu_calc_load - update the avenrun load estimates.961*962* No locking against reading these values from userspace, as for963* the CPU loadavg code.964*/965static void spu_calc_load(void)966{967unsigned long active_tasks; /* fixed-point */968969active_tasks = count_active_contexts() * FIXED_1;970spu_avenrun[0] = calc_load(spu_avenrun[0], EXP_1, active_tasks);971spu_avenrun[1] = calc_load(spu_avenrun[1], EXP_5, active_tasks);972spu_avenrun[2] = calc_load(spu_avenrun[2], EXP_15, active_tasks);973}974975static void spusched_wake(struct timer_list *unused)976{977mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);978wake_up_process(spusched_task);979}980981static void spuloadavg_wake(struct timer_list *unused)982{983mod_timer(&spuloadavg_timer, jiffies + LOAD_FREQ);984spu_calc_load();985}986987static int spusched_thread(void *unused)988{989struct spu *spu;990int node;991992while (!kthread_should_stop()) {993set_current_state(TASK_INTERRUPTIBLE);994schedule();995for (node = 0; node < MAX_NUMNODES; node++) {996struct mutex *mtx = &cbe_spu_info[node].list_mutex;997998mutex_lock(mtx);999list_for_each_entry(spu, &cbe_spu_info[node].spus,1000cbe_list) {1001struct spu_context *ctx = spu->ctx;10021003if (ctx) {1004get_spu_context(ctx);1005mutex_unlock(mtx);1006spusched_tick(ctx);1007mutex_lock(mtx);1008put_spu_context(ctx);1009}1010}1011mutex_unlock(mtx);1012}1013}10141015return 0;1016}10171018void spuctx_switch_state(struct spu_context *ctx,1019enum spu_utilization_state new_state)1020{1021unsigned long long curtime;1022signed long long delta;1023struct spu *spu;1024enum spu_utilization_state old_state;1025int node;10261027curtime = ktime_get_ns();1028delta = curtime - ctx->stats.tstamp;10291030WARN_ON(!mutex_is_locked(&ctx->state_mutex));1031WARN_ON(delta < 0);10321033spu = ctx->spu;1034old_state = ctx->stats.util_state;1035ctx->stats.util_state = new_state;1036ctx->stats.tstamp = curtime;10371038/*1039* Update the physical SPU utilization statistics.1040*/1041if (spu) {1042ctx->stats.times[old_state] += delta;1043spu->stats.times[old_state] += delta;1044spu->stats.util_state = new_state;1045spu->stats.tstamp = curtime;1046node = spu->node;1047if (old_state == SPU_UTIL_USER)1048atomic_dec(&cbe_spu_info[node].busy_spus);1049if (new_state == SPU_UTIL_USER)1050atomic_inc(&cbe_spu_info[node].busy_spus);1051}1052}10531054#ifdef CONFIG_PROC_FS1055static int show_spu_loadavg(struct seq_file *s, void *private)1056{1057int a, b, c;10581059a = spu_avenrun[0] + (FIXED_1/200);1060b = spu_avenrun[1] + (FIXED_1/200);1061c = spu_avenrun[2] + (FIXED_1/200);10621063/*1064* Note that last_pid doesn't really make much sense for the1065* SPU loadavg (it even seems very odd on the CPU side...),1066* but we include it here to have a 100% compatible interface.1067*/1068seq_printf(s, "%d.%02d %d.%02d %d.%02d %ld/%d %d\n",1069LOAD_INT(a), LOAD_FRAC(a),1070LOAD_INT(b), LOAD_FRAC(b),1071LOAD_INT(c), LOAD_FRAC(c),1072count_active_contexts(),1073atomic_read(&nr_spu_contexts),1074idr_get_cursor(&task_active_pid_ns(current)->idr) - 1);1075return 0;1076}1077#endif10781079int __init spu_sched_init(void)1080{1081struct proc_dir_entry *entry;1082int err = -ENOMEM, i;10831084spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);1085if (!spu_prio)1086goto out;10871088for (i = 0; i < MAX_PRIO; i++) {1089INIT_LIST_HEAD(&spu_prio->runq[i]);1090__clear_bit(i, spu_prio->bitmap);1091}1092spin_lock_init(&spu_prio->runq_lock);10931094timer_setup(&spusched_timer, spusched_wake, 0);1095timer_setup(&spuloadavg_timer, spuloadavg_wake, 0);10961097spusched_task = kthread_run(spusched_thread, NULL, "spusched");1098if (IS_ERR(spusched_task)) {1099err = PTR_ERR(spusched_task);1100goto out_free_spu_prio;1101}11021103mod_timer(&spuloadavg_timer, 0);11041105entry = proc_create_single("spu_loadavg", 0, NULL, show_spu_loadavg);1106if (!entry)1107goto out_stop_kthread;11081109pr_debug("spusched: tick: %d, min ticks: %d, default ticks: %d\n",1110SPUSCHED_TICK, MIN_SPU_TIMESLICE, DEF_SPU_TIMESLICE);1111return 0;11121113out_stop_kthread:1114kthread_stop(spusched_task);1115out_free_spu_prio:1116kfree(spu_prio);1117out:1118return err;1119}11201121void spu_sched_exit(void)1122{1123struct spu *spu;1124int node;11251126remove_proc_entry("spu_loadavg", NULL);11271128timer_delete_sync(&spusched_timer);1129timer_delete_sync(&spuloadavg_timer);1130kthread_stop(spusched_task);11311132for (node = 0; node < MAX_NUMNODES; node++) {1133mutex_lock(&cbe_spu_info[node].list_mutex);1134list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list)1135if (spu->alloc_state != SPU_FREE)1136spu->alloc_state = SPU_FREE;1137mutex_unlock(&cbe_spu_info[node].list_mutex);1138}1139kfree(spu_prio);1140}114111421143