Path: blob/master/arch/powerpc/platforms/cell/cpufreq_spudemand.c
10818 views
/*1* spu aware cpufreq governor for the cell processor2*3* © Copyright IBM Corporation 2006-20084*5* Author: Christian Krafft <[email protected]>6*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License as published by9* the Free Software Foundation; either version 2, or (at your option)10* any later version.11*12* This program is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15* GNU General Public License for more details.16*17* You should have received a copy of the GNU General Public License18* along with this program; if not, write to the Free Software19* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.20*/2122#include <linux/cpufreq.h>23#include <linux/sched.h>24#include <linux/timer.h>25#include <linux/workqueue.h>26#include <asm/atomic.h>27#include <asm/machdep.h>28#include <asm/spu.h>2930#define POLL_TIME 100000 /* in µs */31#define EXP 753 /* exp(-1) in fixed-point */3233struct spu_gov_info_struct {34unsigned long busy_spus; /* fixed-point */35struct cpufreq_policy *policy;36struct delayed_work work;37unsigned int poll_int; /* µs */38};39static DEFINE_PER_CPU(struct spu_gov_info_struct, spu_gov_info);4041static int calc_freq(struct spu_gov_info_struct *info)42{43int cpu;44int busy_spus;4546cpu = info->policy->cpu;47busy_spus = atomic_read(&cbe_spu_info[cpu_to_node(cpu)].busy_spus);4849CALC_LOAD(info->busy_spus, EXP, busy_spus * FIXED_1);50pr_debug("cpu %d: busy_spus=%d, info->busy_spus=%ld\n",51cpu, busy_spus, info->busy_spus);5253return info->policy->max * info->busy_spus / FIXED_1;54}5556static void spu_gov_work(struct work_struct *work)57{58struct spu_gov_info_struct *info;59int delay;60unsigned long target_freq;6162info = container_of(work, struct spu_gov_info_struct, work.work);6364/* after cancel_delayed_work_sync we unset info->policy */65BUG_ON(info->policy == NULL);6667target_freq = calc_freq(info);68__cpufreq_driver_target(info->policy, target_freq, CPUFREQ_RELATION_H);6970delay = usecs_to_jiffies(info->poll_int);71schedule_delayed_work_on(info->policy->cpu, &info->work, delay);72}7374static void spu_gov_init_work(struct spu_gov_info_struct *info)75{76int delay = usecs_to_jiffies(info->poll_int);77INIT_DELAYED_WORK_DEFERRABLE(&info->work, spu_gov_work);78schedule_delayed_work_on(info->policy->cpu, &info->work, delay);79}8081static void spu_gov_cancel_work(struct spu_gov_info_struct *info)82{83cancel_delayed_work_sync(&info->work);84}8586static int spu_gov_govern(struct cpufreq_policy *policy, unsigned int event)87{88unsigned int cpu = policy->cpu;89struct spu_gov_info_struct *info, *affected_info;90int i;91int ret = 0;9293info = &per_cpu(spu_gov_info, cpu);9495switch (event) {96case CPUFREQ_GOV_START:97if (!cpu_online(cpu)) {98printk(KERN_ERR "cpu %d is not online\n", cpu);99ret = -EINVAL;100break;101}102103if (!policy->cur) {104printk(KERN_ERR "no cpu specified in policy\n");105ret = -EINVAL;106break;107}108109/* initialize spu_gov_info for all affected cpus */110for_each_cpu(i, policy->cpus) {111affected_info = &per_cpu(spu_gov_info, i);112affected_info->policy = policy;113}114115info->poll_int = POLL_TIME;116117/* setup timer */118spu_gov_init_work(info);119120break;121122case CPUFREQ_GOV_STOP:123/* cancel timer */124spu_gov_cancel_work(info);125126/* clean spu_gov_info for all affected cpus */127for_each_cpu (i, policy->cpus) {128info = &per_cpu(spu_gov_info, i);129info->policy = NULL;130}131132break;133}134135return ret;136}137138static struct cpufreq_governor spu_governor = {139.name = "spudemand",140.governor = spu_gov_govern,141.owner = THIS_MODULE,142};143144/*145* module init and destoy146*/147148static int __init spu_gov_init(void)149{150int ret;151152ret = cpufreq_register_governor(&spu_governor);153if (ret)154printk(KERN_ERR "registration of governor failed\n");155return ret;156}157158static void __exit spu_gov_exit(void)159{160cpufreq_unregister_governor(&spu_governor);161}162163164module_init(spu_gov_init);165module_exit(spu_gov_exit);166167MODULE_LICENSE("GPL");168MODULE_AUTHOR("Christian Krafft <[email protected]>");169170171172