25 #include <linux/errno.h>
26 #include <linux/sched.h>
27 #include <linux/kernel.h>
29 #include <linux/slab.h>
33 #include <linux/stddef.h>
44 #include <asm/mmu_context.h>
49 #define CREATE_TRACE_POINTS
59 static unsigned long spu_avenrun[3];
68 #define NORMAL_PRIO 120
74 #define SPUSCHED_TICK (10)
82 #define MIN_SPU_TIMESLICE max(5 * HZ / (1000 * SPUSCHED_TICK), 1)
83 #define DEF_SPU_TIMESLICE (100 * HZ / (1000 * SPUSCHED_TICK))
85 #define MAX_USER_PRIO (MAX_PRIO - MAX_RT_PRIO)
86 #define SCALE_PRIO(x, prio) \
87 max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_SPU_TIMESLICE)
153 if (ctx->
state == SPU_STATE_RUNNABLE) {
154 node = ctx->
spu->node;
179 static int node_allowed(
struct spu_context *ctx,
int node)
184 rval = __node_allowed(ctx, node);
205 if (spu->alloc_state != SPU_FREE) {
228 if (ctx->
flags & SPU_CREATE_NOSCHED)
231 ctx->
stats.slb_flt_base = spu->stats.slb_flt;
232 ctx->
stats.class2_intr_base = spu->stats.class2_intr;
236 spin_lock_irq(&spu->register_lock);
247 spin_unlock_irq(&spu->register_lock);
263 static inline int sched_spu(
struct spu *spu)
267 return (!spu->ctx || !(spu->ctx->flags & SPU_CREATE_NOSCHED));
270 static void aff_merge_remaining_ctxs(
struct spu_gang *
gang)
281 static void aff_set_offsets(
struct spu_gang *gang)
304 static struct spu *aff_ref_location(
struct spu_context *ctx,
int mem_aff,
305 int group_size,
int lowest_offset)
328 if (!node_allowed(ctx, node))
334 if (spu->ctx && spu->ctx->gang && !spu->ctx->aff_offset
335 && spu->ctx->gang->aff_ref_spu)
336 available_spus -= spu->ctx->gang->contexts;
339 if (available_spus < ctx->gang->
contexts) {
345 if ((!mem_aff || spu->has_mem_affinity) &&
356 static void aff_set_ref_point_location(
struct spu_gang *gang)
358 int mem_aff,
gs, lowest_offset;
362 mem_aff = gang->
aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM;
373 lowest_offset = ctx->aff_offset;
380 static struct spu *ctx_location(
struct spu *ref,
int offset,
int node)
387 BUG_ON(spu->node != node);
395 BUG_ON(spu->node != node);
422 aff_merge_remaining_ctxs(gang);
424 aff_set_offsets(gang);
425 aff_set_ref_point_location(gang);
436 static void spu_unbind_context(
struct spu *spu,
struct spu_context *ctx)
444 if (spu->ctx->flags & SPU_CREATE_NOSCHED)
460 spin_lock_irq(&spu->register_lock);
462 ctx->
state = SPU_STATE_SAVED;
463 spu->ibox_callback =
NULL;
464 spu->wbox_callback =
NULL;
465 spu->stop_callback =
NULL;
466 spu->mfc_callback =
NULL;
472 spin_unlock_irq(&spu->register_lock);
476 ctx->
stats.slb_flt +=
477 (spu->stats.slb_flt - ctx->
stats.slb_flt_base);
478 ctx->
stats.class2_intr +=
479 (spu->stats.class2_intr - ctx->
stats.class2_intr_base);
493 static void __spu_add_to_rq(
struct spu_context *ctx)
508 if (list_empty(&ctx->
rq)) {
519 __spu_add_to_rq(ctx);
523 static void __spu_del_from_rq(
struct spu_context *ctx)
527 if (!list_empty(&ctx->
rq)) {
530 list_del_init(&ctx->
rq);
532 if (list_empty(&spu_prio->
runq[prio]))
540 __spu_del_from_rq(ctx);
557 if (!signal_pending(
current)) {
558 __spu_add_to_rq(ctx);
564 __spu_del_from_rq(ctx);
571 static struct spu *spu_get_idle(
struct spu_context *ctx)
573 struct spu *spu, *aff_ref_spu;
580 if (has_affinity(ctx)) {
581 aff_ref_spu = ctx->
gang->aff_ref_spu;
584 node = aff_ref_spu->node;
587 spu = ctx_location(aff_ref_spu, ctx->
aff_offset, node);
588 if (spu && spu->alloc_state == SPU_FREE)
600 if (!node_allowed(ctx, node))
605 if (spu->alloc_state == SPU_FREE)
616 spu->alloc_state = SPU_USED;
629 static struct spu *find_victim(
struct spu_context *ctx)
648 if (!node_allowed(ctx, node))
656 !(tmp->
flags & SPU_CREATE_NOSCHED) &&
657 (!victim || tmp->
prio > victim->
prio)) {
683 if (!spu || victim->
prio <= ctx->
prio) {
699 spu_unbind_context(spu, victim);
702 victim->
stats.invol_ctx_switch++;
703 spu->stats.invol_ctx_switch++;
705 spu_add_to_rq(victim);
717 static void __spu_schedule(
struct spu *spu,
struct spu_context *ctx)
719 int node = spu->node;
725 if (spu->ctx ==
NULL) {
726 spu_bind_context(spu, ctx);
728 spu->alloc_state = SPU_USED;
739 static void spu_schedule(
struct spu *spu,
struct spu_context *ctx)
745 __spu_schedule(spu, ctx);
762 static void spu_unschedule(
struct spu *spu,
struct spu_context *ctx,
765 int node = spu->node;
770 spu->alloc_state = SPU_FREE;
771 spu_unbind_context(spu, ctx);
772 ctx->
stats.invol_ctx_switch++;
773 spu->stats.invol_ctx_switch++;
803 spu = spu_get_idle(ctx);
808 if (!spu && rt_prio(ctx->
prio))
809 spu = find_victim(ctx);
811 unsigned long runcntl;
813 runcntl = ctx->
ops->runcntl_read(ctx);
814 __spu_schedule(spu, ctx);
815 if (runcntl & SPU_RUNCNTL_RUNNABLE)
821 if (ctx->
flags & SPU_CREATE_NOSCHED) {
823 goto spu_activate_top;
837 static struct spu_context *grab_runnable_context(
int prio,
int node)
844 while (best < prio) {
849 if (__node_allowed(ctx, node)) {
850 __spu_del_from_rq(ctx);
862 static int __spu_deactivate(
struct spu_context *ctx,
int force,
int max_prio)
864 struct spu *spu = ctx->
spu;
868 new = grab_runnable_context(max_prio, spu->node);
870 spu_unschedule(spu, ctx,
new ==
NULL);
872 if (new->flags & SPU_CREATE_NOSCHED)
876 spu_schedule(spu,
new);
912 if (!(ctx->
flags & SPU_CREATE_NOSCHED)) {
922 struct spu *spu =
NULL;
924 if (spu_acquire(ctx))
927 if (ctx->
state != SPU_STATE_RUNNABLE)
929 if (ctx->
flags & SPU_CREATE_NOSCHED)
941 new = grab_runnable_context(ctx->
prio + 1, spu->node);
943 spu_unschedule(spu, ctx, 0);
955 spu_schedule(spu,
new);
967 static unsigned long count_active_contexts(
void)
969 int nr_active = 0,
node;
984 static void spu_calc_load(
void)
988 active_tasks = count_active_contexts() *
FIXED_1;
994 static void spusched_wake(
unsigned long data)
1000 static void spuloadavg_wake(
unsigned long data)
1006 static int spusched_thread(
void *
unused)
1040 unsigned long long curtime;
1041 signed long long delta;
1044 enum spu_utilization_state old_state;
1048 curtime = timespec_to_ns(&ts);
1049 delta = curtime - ctx->
stats.tstamp;
1055 old_state = ctx->
stats.util_state;
1057 ctx->
stats.tstamp = curtime;
1064 spu->stats.times[old_state] +=
delta;
1066 spu->stats.tstamp = curtime;
1068 if (old_state == SPU_UTIL_USER)
1070 if (new_state == SPU_UTIL_USER)
1075 #define LOAD_INT(x) ((x) >> FSHIFT)
1076 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
1078 static int show_spu_loadavg(
struct seq_file *
s,
void *
private)
1082 a = spu_avenrun[0] + (
FIXED_1/200);
1083 b = spu_avenrun[1] + (
FIXED_1/200);
1084 c = spu_avenrun[2] + (
FIXED_1/200);
1091 seq_printf(s,
"%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
1095 count_active_contexts(),
1097 current->nsproxy->pid_ns->last_pid);
1107 .open = spu_loadavg_open,
1123 INIT_LIST_HEAD(&spu_prio->
runq[i]);
1129 setup_timer(&spuloadavg_timer, spuloadavg_wake, 0);
1132 if (IS_ERR(spusched_task)) {
1133 err = PTR_ERR(spusched_task);
1134 goto out_free_spu_prio;
1139 entry = proc_create(
"spu_loadavg", 0,
NULL, &spu_loadavg_fops);
1141 goto out_stop_kthread;
1143 pr_debug(
"spusched: tick: %d, min ticks: %d, default ticks: %d\n",
1169 if (spu->alloc_state != SPU_FREE)
1170 spu->alloc_state = SPU_FREE;