Frequency changes for XScale PXA255
Hi, everyone.
I am trying to make a module for RT Linux which will periodically change frequency and voltage of the microprocessor. Maybe I am going to ask something what is I easy, so I am sorry for disturbance, but it is not easy for me because my field of interesting is not kernel, so I need help. I have following problem.
In the function init_module , which is started when the module is inserted, I can set the maximum frequency for the policy and using the _ _cpufreq_driver_target function I can change frequency and the voltage of the microprocessor to any value which is in the limits of the policy. After that, I start a thread, which is periodic, and in the thread new values for the frequency should be set.
When the periodic task begins, and the function __cpufreq_driver_target starts executing, there is a message that it is not possible to continue because of the line BUG_ON(irqs_disabled()); This line is in the function cpufreq_notify_transition. It is obvious that it is because interrupts are disabled, and I tried to enable interrupts before calling the function __cpufreq_driver_target,
but it didn't help at all. I wanted to see what would happen if I delete this line and the function was stopped on the next line, down_read(&cpufreq_notifier_rwsem);.
My questions are:
1. Why does the function cpufreq_notify_transition need interrupts?
2. Does anybody can tell me what I have done wrong? I was trying to make this module based on the cpufreq_ondemand, but it is obvious that I am doing something wrong.
3. If I do not make mistake, governor serves only when it is started, turned on and if its limits are changed, right? I can call driver function from any other function, can't I?
4. I am a little confused, because from the init_module function I could change frequency, and from the thread itself I couldn't. What should I check to see what is different in those two cases? (I have already checked flags of the micro and interrupts are disabled in the thread, and enabled in the init_module)
Thanks in advance
Miroslav Vasic
Hi,
Quoted 5 lines · Miroslav Vasic
starts executing, there is a message that it is not possible to continue because of the line BUG_ON(irqs_disabled()); This line is in the function
cpufreq_notify_transition. It is obvious that it is because interrupts are disabled, and I tried to enable interrupts before calling the function__cpufreq_driver_target,
Something strange is going on, or else your "thread" would run with IRQs enabled. Could you post the code, please?
Dominik
Hi, Dominik.
In the attachment you will see the c code of my module. Following you can see where I put some printks commands (functions: pxa_set_target and cpufreq_notify_transition), which I use to follow where my module halts. Every change comparing to the original function is bolded. After that is the contents of the console when I start the thread.
static int pxa_set_target(struct cpufreq_policy *policy,
unsigned int target_freq, unsigned int relation)
{
int idx;
cpumask_t cpus_allowed;
int cpu = policy->cpu;
struct cpufreq_freqs freqs;
pxa_freqs_t *pxa_freq_settings;
struct cpufreq_frequency_table *pxa_freqs_table; unsigned long flags;
unsigned int unused;
unsigned int preset_mdrefr, postset_mdrefr; void *ramstart;
/*my change*/ printk(KERN_INFO "Entering in PXA_set_target \n");
/*
* Save this threads cpus_allowed mask.
*/
cpus_allowed = current->cpus_allowed;
/*
* Bind to the specified CPU. When this call returns,
* we should be running on the right CPU.
*/
set_cpus_allowed(current, cpumask_of_cpu(cpu)); BUG_ON(cpu != smp_processor_id());
/* Get the current policy */
pxa_select_freq_table(policy, &pxa_freq_settings, &pxa_freqs_table);
/* Lookup the next frequency */
if (cpufreq_frequency_table_target(policy, pxa_freqs_table,
target_freq, relation, &idx)) {
return -EINVAL;
} freqs.old = get_clk_frequency_khz(0);
freqs.new = pxa_freq_settings[idx].khz;
freqs.cpu = policy->cpu;
// if(freq_debug) {
printk(KERN_INFO "Changing CPU frequency to %d.%03d Mhz (SDRAM %d
Mhz).\n",
freqs.new/1000, freqs.new%1000, (pxa_freq_settings[idx].div2) ?
(pxa_freq_settings[idx].membus/2000) :
(pxa_freq_settings[idx].membus/1000));// } //I wanted to see this information, so I deleted IF command
ramstart = phys_to_virt(0xa0000000);
/*
* Tell everyone what we're about to do...
* you should add a notify client with any platform specific
* Vcc changing capability
*/
printk(KERN_INFO "The first notify_transition \n"); cpufreq_notify_transition1(&freqs, CPUFREQ_PRECHANGE); printk(KERN_INFO "The first notify_transition done \n");
/* Calculate the next MDREFR. If we're slowing down the SDRAM clock
* we need to preset the smaller DRI before the change. If we're
speeding
* up we need to set the larger DRI value after the change. */
preset_mdrefr = postset_mdrefr = MDREFR;
if((MDREFR & MDREFR_DRI_MASK) >MDREFR_DRI(pxa_freq_settings[idx].membus)) {
preset_mdrefr = (preset_mdrefr & ~MDREFR_DRI_MASK) |
MDREFR_DRI(pxa_freq_settings[idx].membus);
}
postset_mdrefr = (postset_mdrefr & ~MDREFR_DRI_MASK) |
MDREFR_DRI(pxa_freq_settings[idx].membus);
/* If we're dividing the memory clock by two for the SDRAM clock, this
* must be set prior to the change. Clearing the divide must be done
* after the change.
*/
if(pxa_freq_settings[idx].div2) {
preset_mdrefr |= MDREFR_DB2_MASK;
postset_mdrefr |= MDREFR_DB2_MASK;
} else {
postset_mdrefr &= ~MDREFR_DB2_MASK;
}
printk(KERN_INFO "local_irq_save \n");
local_irq_save(flags);
printk(KERN_INFO "local_irq_saved \n");
/* Set new the CCCR */
CCCR = pxa_freq_settings[idx].cccr; __asm__ __volatile__(" \
ldr r4, [%1] ; /* load MDREFR */ \
b 2f ; \
.align 5 ; \
1: \
str %4, [%1] ; /* preset the MDREFR */ \
mcr p14, 0, %2, c6, c0, 0 ; /* set CCLKCFG[FCS] */ \
str %5, [%1] ; /* postset the MDREFR */ \
\
b 3f ; \
2: b 1b ; \
3: nop ; \"
: "=&r"
(unused)
: "r" (&MDREFR), "r" (CCLKCFG_TURBO|CCLKCFG_FCS), "r" (ramstart), \
"r" (preset_mdrefr), "r" (postset_mdrefr)
: "r4", "r5"); printk(KERN_INFO "local_irq_restore \n");
local_irq_restore(flags);
printk(KERN_INFO "local_irq_restored \n");
/*
* Restore the CPUs allowed mask.
*/
set_cpus_allowed(current, cpus_allowed);
/*
* Tell everyone what we've just done...
* you should add a notify client with any platform specific
* SDRAM refresh timer adjustments
*/
printk(KERN_INFO "The second notify_transition \n"); cpufreq_notify_transition1(&freqs, CPUFREQ_POSTCHANGE); printk(KERN_INFO "The second notify_transition done \n");
return 0;
}
void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
{
// BUG_ON(irqs_disabled()); //I have tried without this command
freqs->flags = cpufreq_driver->flags; dprintk("notification %u of frequency transition to %u kHz\n", state,freqs->new);
printk(KERN_INFO "NOTIFY TRANSITION \n"); down_read(&cpufreq_notifier_rwsem); printk(KERN_INFO "NOTIFIER SEMAPHORE DOWN \n"); switch (state) {
case CPUFREQ_PRECHANGE:
/* detect if the driver reported a value as "old frequency" which
* is not equal to what the cpufreq core thinks is "old frequency".
*/
printk(KERN_INFO "state is CPUFREQ_PRECHANGE \n");
if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
if ((likely(cpufreq_cpu_data[freqs->cpu])) &&
(likely(cpufreq_cpu_data[freqs->cpu]->cpu == freqs->cpu)) &&
(likely(cpufreq_cpu_data[freqs->cpu]->cur)) &&
(unlikely(freqs->old != cpufreq_cpu_data[freqs->cpu]->cur)))
{
dprintk(KERN_WARNING "Warning: CPU frequency is %u, "
"cpufreq assumed %u kHz.\n", freqs->old,
cpufreq_cpu_data[freqs->cpu]->cur);
freqs->old = cpufreq_cpu_data[freqs->cpu]->cur;
}
}
notifier_call_chain(&cpufreq_transition_notifier_list,
CPUFREQ_PRECHANGE, freqs);
adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
break;
case CPUFREQ_POSTCHANGE:
printk(KERN_INFO "state is CPUFREQ_POSTCHANGE \n");
adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
notifier_call_chain(&cpufreq_transition_notifier_list,
CPUFREQ_POSTCHANGE, freqs);
if ((likely(cpufreq_cpu_data[freqs->cpu])) &&
(likely(cpufreq_cpu_data[freqs->cpu]->cpu == freqs->cpu)))
cpufreq_cpu_data[freqs->cpu]->cur = freqs->new;
break; }
up_read(&cpufreq_notifier_rwsem);
printk(KERN_INFO "NOTIFIER SEMAPHORE UP \n");
}
bash-3.00# insmod ./modul.ko
Starting the module
POLICY
max frequency for the policy 398131
current frequency99532
value 0
flags 1610612755
Changing
Entering in PXA_set_target
Changing CPU frequency to 132.710 Mhz (SDRAM 66 Mhz). The first notify_transition
NOTIFY TRANSITION
NOTIFIER SEMAPHORE DOWN
state is CPUFREQ_PRECHANGE
viper: setting CPU core voltage to 1.0V at 132.710MHz NOTIFIER SEMAPHORE UP
The first notify_transition done local_irq_savelocal_irq_savedlocal_irq_restorelocal_irq_restored
The second notify_transition
NOTIFY TRANSITION
NOTIFIER SEMAPHORE DOWN
state is CPUFREQ_POSTCHANGE
NOTIFIER SEMAPHORE UP
The second notify_transition done
Done
current frequency132710
Starting the thread
Started
-bash-3.00# current frequency 132710
value 128
Changing
Entering in PXA_set_target
Changing CPU frequency to 99.532 Mhz (SDRAM 49 Mhz). The first notify_transition
NOTIFY TRANSITION
Here everything stops.
If you need me anything to explain better, please tell me.
Thanks,
Miroslav Vasic
/*
* drivers/cpufreq/cpufreq_myway.c
*
*/
//#include <linux/kernel_stat.h>
#include <linux/cpufreq.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <rtl_time.h>
#define DEF_FREQUENCY_UP_THRESHOLD (80)
#define MIN_FREQUENCY_UP_THRESHOLD (0)
#define MAX_FREQUENCY_UP_THRESHOLD (100)
#define DEF_FREQUENCY_DOWN_THRESHOLD (20)
#define MIN_FREQUENCY_DOWN_THRESHOLD (0)
#define MAX_FREQUENCY_DOWN_THRESHOLD (100)
static unsigned int def_sampling_rate;
#define MIN_SAMPLING_RATE (def_sampling_rate / 2)
#define MAX_SAMPLING_RATE (500 * def_sampling_rate)
#define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
#define DEF_SAMPLING_DOWN_FACTOR (10)
#define TRANSITION_LATENCY_LIMIT (10 * 1000)
#define sampling_rate_in_HZ(x) (((x * HZ) < (1000 * 1000))?1:((x * HZ) / (1000 * 1000)))
unsigned int counter=0;
unsigned long counter1=0;
unsigned long counter2=0;
struct timeval mik;
struct timeval mik1;
unsigned long flagsor;
unsigned long flags;
static void do_dbs_timer(void *data);
struct cpu_dbs_info_s {
struct cpufreq_policy *cur_policy;
unsigned int prev_cpu_idle_up;
unsigned int prev_cpu_idle_down;
unsigned int enable;
};static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
static unsigned int dbs_enable; /* number of CPUs using this policy */
static DECLARE_MUTEX (dbs_sem);
static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
struct dbs_tuners {
unsigned int sampling_rate;
unsigned int sampling_down_factor;
unsigned int up_threshold;
unsigned int down_threshold;
};static struct dbs_tuners dbs_tuners_ins = {
.up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
.down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
.sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
};
/************************** sysfs interface ************************/ static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
{
return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
}static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
{
return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
}#define define_one_ro(_name) \
static struct freq_attr _name = \__ATTR(_name, 0444, show_##_name, NULL)
define_one_ro(sampling_rate_max);define_one_ro(sampling_rate_min);
/* cpufreq_ondemand Governor Tunables */
#define show_one(file_name, object) \
static ssize_t show_##file_name \
(struct cpufreq_policy *unused, char *buf) \
{ \
return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
}show_one(sampling_rate, sampling_rate); show_one(sampling_down_factor, sampling_down_factor); show_one(up_threshold, up_threshold); show_one(down_threshold, down_threshold);
static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
const char *buf, size_t count)
{
unsigned int input;
int ret;
ret = sscanf (buf, "%u", &input);
if (ret != 1 )
return -EINVAL;
down(&dbs_sem);
dbs_tuners_ins.sampling_down_factor = input;
up(&dbs_sem);
return count;
}static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
const char *buf, size_t count)
{
unsigned int input;
int ret;
ret = sscanf (buf, "%u", &input);
down(&dbs_sem);
if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
up(&dbs_sem);
return -EINVAL;
}
dbs_tuners_ins.sampling_rate = input;
up(&dbs_sem);
return count;
}static ssize_t store_up_threshold(struct cpufreq_policy *unused,
const char *buf, size_t count)
{
unsigned int input;
int ret;
ret = sscanf (buf, "%u", &input);
down(&dbs_sem);
if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
input < MIN_FREQUENCY_UP_THRESHOLD ||
input <= dbs_tuners_ins.down_threshold) {
up(&dbs_sem);
return -EINVAL;
}
dbs_tuners_ins.up_threshold = input;
up(&dbs_sem);
return count;
}static ssize_t store_down_threshold(struct cpufreq_policy *unused,
const char *buf, size_t count)
{
unsigned int input;
int ret;
ret = sscanf (buf, "%u", &input);
down(&dbs_sem);
if (ret != 1 || input > MAX_FREQUENCY_DOWN_THRESHOLD ||
input < MIN_FREQUENCY_DOWN_THRESHOLD ||
input >= dbs_tuners_ins.up_threshold) {
up(&dbs_sem);
return -EINVAL;
}
dbs_tuners_ins.down_threshold = input;
up(&dbs_sem);
return count;
}#define define_one_rw(_name) \
static struct freq_attr _name = \__ATTR(_name, 0644, show_##_name, store_##_name)
define_one_rw(sampling_rate);define_one_rw(sampling_down_factor);define_one_rw(up_threshold);define_one_rw(down_threshold);
static struct attribute * dbs_attributes[] = {
&sampling_rate_max.attr,
&sampling_rate_min.attr,
&sampling_rate.attr,
&sampling_down_factor.attr,
&up_threshold.attr,
&down_threshold.attr,
NULL
};
static struct attribute_group dbs_attr_group = {
.attrs = dbs_attributes,
.name = "myway",
};/************************** sysfs end ************************/
/* my way to change frequency */
static void myway_function(int cpu)
{
struct cpu_dbs_info_s *this_dbs_info;
struct cpufreq_policy *policy;
this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
if (!this_dbs_info->enable)
return;
policy = this_dbs_info->cur_policy;
counter=counter+1;
switch (counter)
{
case 1:
__cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_H);
printk(KERN_ALERT "Frequency: 99 MHz ");
break;
case 2:
__cpufreq_driver_target(policy, 150000, CPUFREQ_RELATION_H);
printk(KERN_ALERT "Frequency: 150 ");
break;
case 3:
__cpufreq_driver_target(policy, 200000, CPUFREQ_RELATION_H);
printk(KERN_ALERT "Frequency: 200");
break;
case 4:
__cpufreq_driver_target(policy, 300000, CPUFREQ_RELATION_H);
printk(KERN_ALERT "Frequency: 300 ");
break;
case 5:
__cpufreq_driver_target(policy, policy->max, CPUFREQ_RELATION_H);
printk(KERN_ALERT "Frequency: 400 ");
break;
default:
counter=0;
break;
}
}static void do_dbs_timer(void *data)
{
int i;
down(&dbs_sem);
for (i = 0; i < NR_CPUS; i++)
if (cpu_online(i))
myway_function(i); //call to change frequency
schedule_delayed_work(&dbs_work, sampling_rate_in_HZ(5000000));
up(&dbs_sem);
} static inline void dbs_timer_init(void)
{
INIT_WORK(&dbs_work, do_dbs_timer, NULL);
schedule_delayed_work(&dbs_work,sampling_rate_in_HZ(5000000));
return;
}static inline void dbs_timer_exit(void)
{
cancel_delayed_work(&dbs_work);
return;
}static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
unsigned int event)
{
unsigned int cpu = policy->cpu;
struct cpu_dbs_info_s *this_dbs_info;
this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
printk("START Guvernera \n");
switch (event) {
case CPUFREQ_GOV_START:
break;
case CPUFREQ_GOV_STOP:
down(&dbs_sem);
this_dbs_info->enable = 0;
sysfs_remove_group(&policy->kobj, &dbs_attr_group);
dbs_enable--;
/*
* Stop the timerschedule work, when this governor
* is used for first time
*/
if (dbs_enable == 0)
dbs_timer_exit();
up(&dbs_sem);
break;
case CPUFREQ_GOV_LIMITS:
down(&dbs_sem);
if (policy->max < this_dbs_info->cur_policy->cur)
__cpufreq_driver_target(
this_dbs_info->cur_policy,
policy->max, CPUFREQ_RELATION_H);
else if (policy->min > this_dbs_info->cur_policy->cur)
__cpufreq_driver_target(
this_dbs_info->cur_policy,
policy->min, CPUFREQ_RELATION_L);
up(&dbs_sem);
break;
}
return 0;
}struct cpufreq_governor cpufreq_gov_dbs = {
.name = "myway",
.governor = cpufreq_governor_dbs,
.owner = THIS_MODULE,
};EXPORT_SYMBOL(cpufreq_gov_dbs);
static int __init cpufreq_gov_dbs_init(void)
{
return cpufreq_register_governor(&cpufreq_gov_dbs);
}static void __exit cpufreq_gov_dbs_exit(void)
{
/* Make sure that the scheduled work is indeed not running */
flush_scheduled_work();
cpufreq_unregister_governor(&cpufreq_gov_dbs);
}MODULE_AUTHOR ("Miroslav Vasic");MODULE_DESCRIPTION ("'cpufreq_myway'");MODULE_LICENSE ("GPL");
pthread_t thread;
void *thread_code(void *t)
{
struct timespec next;
int count = 0;
int proba=0;
struct cpufreq_policy *policy = cpufreq_cpu_get(0);
struct cpufreq_cpuinfo *info;
int value=0;
clock_gettime(CLOCK_REALTIME, &next);
while (1) {
timespec_add_ns(&next, 5000*1000);
rtl_clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME,&next, NULL);
count++;
if ((count % 1000)==0)
{
proba++;
info=&policy->cpuinfo;
policy->max=info->max_freq;
//printk("max frequency for the policy %d \n",policy->max);
printk("current frequency %d \n",policy->cur);
value=irqs_disabled();
printk("value %d \n", value); // to see that interrupts are disabled
/*local_irq_enable();
value=irqs_disabled();
printk("value after enable %d \n", value);*/
switch(proba)
{
case 1:
printk("Changing \n");
cpufreq_driver_target(policy, 99532, CPUFREQ_RELATION_H);
printk("Done \n");
break;
case 2:
printk("Changing \n");
cpufreq_driver_target(policy, 132710, CPUFREQ_RELATION_H);
printk("Done \n");
break;
case 3:
printk("Changing \n");
cpufreq_driver_target(policy, 199065, CPUFREQ_RELATION_H);
printk("Done \n");
break;
case 4:
printk("Changing \n");
cpufreq_driver_target(policy, 265421, CPUFREQ_RELATION_H);
printk("Done \n");
break;
case 5:
printk("Changing \n");
cpufreq_driver_target(policy, 398131, CPUFREQ_RELATION_H);
printk("Done \n");
break;
default:
proba=0;
break;
}
};
}
return NULL;
}int init_module(void)
{
struct cpufreq_cpuinfo *info;
struct cpufreq_policy *policy = cpufreq_cpu_get(0); int value=0;
printk("Starting the module\n");
printk("POLICY \n");
info=&policy->cpuinfo;
policy->max=info->max_freq;
printk("max frequency for the policy %d \n",policy->max);
printk("current frequency%d \n",policy->cur);
value=irqs_disabled();
printk("value %d \n", value);
local_save_flags(flagsor);
printk("flags %ld \n", flagsor); // i had had idea to use these flags inside the thread, but it didn't work
printk("Changing \n");
__cpufreq_driver_target(policy, 132710, CPUFREQ_RELATION_H); //just for fun, let's change the frequency
printk("Done \n"); printk("current frequency%d \n",policy->cur); printk("Starting the thread \n"); pthread_create(&thread, NULL, thread_code, (void *)0); printk("Started \n");
return 0;
}
void cleanup_module(void)
{
printk("killing the module\n");
cpufreq_gov_dbs_exit(); printk("killing the tread 2\n");
pthread_cancel(thread);
pthread_join(thread, NULL);
printk("killed \n");
return;
}
_______________________________________________
Cpufreq mailing list
[email protected]
http://lists.linux.org.uk/mailman/listinfo/cpufreq
Hi,
Sorry for not getting back to you earlier. What's the status of your modules? A few comments:
Quoted 10 lines · Miroslav Vasic
static int pxa_set_target(struct
cpufreq_policy*policy,
unsigned inttarget_freq, unsigned int relation){ /* * Bind to the specified CPU. When this call returns, * we should be running on the right CPU. */ set_cpus_allowed(current, cpumask_of_cpu(cpu)); BUG_ON(cpu != smp_processor_id());
You don't need to do that this early -- the next few instructions can still be run on any CPU.
Quoted 9 lines
/* Get the current policy */
pxa_select_freq_table(policy, &pxa_freq_settings, &pxa_freqs_table);
/* Lookup the next frequency */
if (cpufreq_frequency_table_target(policy, pxa_freqs_table,
target_freq, relation, &idx)) {
return -EINVAL;
}up to exactly here. Unless get-clk_frequency_khz(0) means "let's ask for the speed of CPU 0", then this code is even more broken. It is broken, BTW, for it does no error handling (i.e. resetting set_cups_allowed().)
Quoted 2 lines
printk(KERN_INFO "The first notify_transition \n"); cpufreq_notify_transition1(&freqs, CPUFREQ_PRECHANGE);
^
artifact or part of the code?
Dominik