[CPUFREQ] CPU frequency display in /proc/cpuinfo
[powerpc.git] / drivers / cpufreq / cpufreq.c
index 10b0149..a9163d0 100644 (file)
@@ -4,6 +4,9 @@
  *  Copyright (C) 2001 Russell King
  *            (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
  *
+ *  Oct 2005 - Ashok Raj <ashok.raj@intel.com>
+ *                     Added handling for CPU hotplug
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
@@ -35,14 +38,6 @@ static struct cpufreq_driver         *cpufreq_driver;
 static struct cpufreq_policy   *cpufreq_cpu_data[NR_CPUS];
 static DEFINE_SPINLOCK(cpufreq_driver_lock);
 
-
-/* we keep a copy of all ->add'ed CPU's struct sys_device here;
- * as it is only accessed in ->add and ->remove, no lock or reference
- * count is necessary.
- */
-static struct sys_device       *cpu_sys_devices[NR_CPUS];
-
-
 /* internal prototypes */
 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
 static void handle_update(void *data);
@@ -574,6 +569,9 @@ static int cpufreq_add_dev (struct sys_device * sys_dev)
        unsigned long flags;
        unsigned int j;
 
+       if (cpu_is_offline(cpu))
+               return 0;
+
        cpufreq_debug_disable_ratelimit();
        dprintk("adding CPU %u\n", cpu);
 
@@ -582,7 +580,6 @@ static int cpufreq_add_dev (struct sys_device * sys_dev)
         * CPU because it is in the same boat. */
        policy = cpufreq_cpu_get(cpu);
        if (unlikely(policy)) {
-               cpu_sys_devices[cpu] = sys_dev;
                dprintk("CPU already managed, adding link\n");
                sysfs_create_link(&sys_dev->kobj, &policy->kobj, "cpufreq");
                cpufreq_debug_enable_ratelimit();
@@ -595,12 +592,11 @@ static int cpufreq_add_dev (struct sys_device * sys_dev)
                goto module_out;
        }
 
-       policy = kmalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
+       policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
        if (!policy) {
                ret = -ENOMEM;
                goto nomem_out;
        }
-       memset(policy, 0, sizeof(struct cpufreq_policy));
 
        policy->cpu = cpu;
        policy->cpus = cpumask_of_cpu(cpu);
@@ -627,7 +623,7 @@ static int cpufreq_add_dev (struct sys_device * sys_dev)
 
        ret = kobject_register(&policy->kobj);
        if (ret)
-               goto err_out;
+               goto err_out_driver_exit;
 
        /* set up files for this cpu device */
        drv_attr = cpufreq_driver->attr;
@@ -657,7 +653,6 @@ static int cpufreq_add_dev (struct sys_device * sys_dev)
        }
 
        module_put(cpufreq_driver->owner);
-       cpu_sys_devices[cpu] = sys_dev;
        dprintk("initialization complete\n");
        cpufreq_debug_enable_ratelimit();
        
@@ -673,12 +668,16 @@ err_out_unregister:
        kobject_unregister(&policy->kobj);
        wait_for_completion(&policy->kobj_unregister);
 
+err_out_driver_exit:
+       if (cpufreq_driver->exit)
+               cpufreq_driver->exit(policy);
+
 err_out:
        kfree(policy);
 
 nomem_out:
        module_put(cpufreq_driver->owner);
- module_out:
+module_out:
        cpufreq_debug_enable_ratelimit();
        return ret;
 }
@@ -695,6 +694,7 @@ static int cpufreq_remove_dev (struct sys_device * sys_dev)
        unsigned long flags;
        struct cpufreq_policy *data;
 #ifdef CONFIG_SMP
+       struct sys_device *cpu_sys_dev;
        unsigned int j;
 #endif
 
@@ -706,7 +706,6 @@ static int cpufreq_remove_dev (struct sys_device * sys_dev)
 
        if (!data) {
                spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
-               cpu_sys_devices[cpu] = NULL;
                cpufreq_debug_enable_ratelimit();
                return -EINVAL;
        }
@@ -721,14 +720,12 @@ static int cpufreq_remove_dev (struct sys_device * sys_dev)
                dprintk("removing link\n");
                spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
                sysfs_remove_link(&sys_dev->kobj, "cpufreq");
-               cpu_sys_devices[cpu] = NULL;
                cpufreq_cpu_put(data);
                cpufreq_debug_enable_ratelimit();
                return 0;
        }
 #endif
 
-       cpu_sys_devices[cpu] = NULL;
 
        if (!kobject_get(&data->kobj)) {
                spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
@@ -757,7 +754,8 @@ static int cpufreq_remove_dev (struct sys_device * sys_dev)
                        if (j == cpu)
                                continue;
                        dprintk("removing link for cpu %u\n", j);
-                       sysfs_remove_link(&cpu_sys_devices[j]->kobj, "cpufreq");
+                       cpu_sys_dev = get_cpu_sysdev(j);
+                       sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
                        cpufreq_cpu_put(data);
                }
        }
@@ -768,7 +766,6 @@ static int cpufreq_remove_dev (struct sys_device * sys_dev)
        down(&data->lock);
        if (cpufreq_driver->target)
                __cpufreq_governor(data, CPUFREQ_GOV_STOP);
-       cpufreq_driver->target = NULL;
        up(&data->lock);
 
        kobject_unregister(&data->kobj);
@@ -825,6 +822,30 @@ static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigne
 }
 
 
+/** 
+ * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
+ * @cpu: CPU number
+ *
+ * This is the last known freq, without actually getting it from the driver.
+ * Return value will be same as what is shown in scaling_cur_freq in sysfs.
+ */
+unsigned int cpufreq_quick_get(unsigned int cpu)
+{
+       struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
+       unsigned int ret = 0;
+
+       if (policy) {
+               down(&policy->lock);
+               ret = policy->cur;
+               up(&policy->lock);
+               cpufreq_cpu_put(policy);
+       }
+
+       return (ret);
+}
+EXPORT_SYMBOL(cpufreq_quick_get);
+
+
 /** 
  * cpufreq_get - get the current CPU frequency (in kHz)
  * @cpu: CPU number
@@ -1115,17 +1136,19 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
                            unsigned int relation)
 {
        int retval = -EINVAL;
+
        lock_cpu_hotplug();
        dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
                target_freq, relation);
        if (cpu_online(policy->cpu) && cpufreq_driver->target)
                retval = cpufreq_driver->target(policy, target_freq, relation);
+
        unlock_cpu_hotplug();
+
        return retval;
 }
 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
 
-
 int cpufreq_driver_target(struct cpufreq_policy *policy,
                          unsigned int target_freq,
                          unsigned int relation)
@@ -1412,6 +1435,45 @@ int cpufreq_update_policy(unsigned int cpu)
 }
 EXPORT_SYMBOL(cpufreq_update_policy);
 
+static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
+                                       unsigned long action, void *hcpu)
+{
+       unsigned int cpu = (unsigned long)hcpu;
+       struct cpufreq_policy *policy;
+       struct sys_device *sys_dev;
+
+       sys_dev = get_cpu_sysdev(cpu);
+
+       if (sys_dev) {
+               switch (action) {
+               case CPU_ONLINE:
+                       cpufreq_add_dev(sys_dev);
+                       break;
+               case CPU_DOWN_PREPARE:
+                       /*
+                        * We attempt to put this cpu in lowest frequency
+                        * possible before going down. This will permit
+                        * hardware-managed P-State to switch other related
+                        * threads to min or higher speeds if possible.
+                        */
+                       policy = cpufreq_cpu_data[cpu];
+                       if (policy) {
+                               cpufreq_driver_target(policy, policy->min,
+                                               CPUFREQ_RELATION_H);
+                       }
+                       break;
+               case CPU_DEAD:
+                       cpufreq_remove_dev(sys_dev);
+                       break;
+               }
+       }
+       return NOTIFY_OK;
+}
+
+static struct notifier_block cpufreq_cpu_notifier =
+{
+    .notifier_call = cpufreq_cpu_callback,
+};
 
 /*********************************************************************
  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
@@ -1472,6 +1534,7 @@ int cpufreq_register_driver(struct cpufreq_driver *driver_data)
        }
 
        if (!ret) {
+               register_cpu_notifier(&cpufreq_cpu_notifier);
                dprintk("driver %s up and running\n", driver_data->name);
                cpufreq_debug_enable_ratelimit();
        }
@@ -1503,6 +1566,7 @@ int cpufreq_unregister_driver(struct cpufreq_driver *driver)
        dprintk("unregistering driver %s\n", driver->name);
 
        sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
+       unregister_cpu_notifier(&cpufreq_cpu_notifier);
 
        spin_lock_irqsave(&cpufreq_driver_lock, flags);
        cpufreq_driver = NULL;