Revert "ARM: tegra: tegratab: dummy change"
[linux-2.6.git] / drivers / acpi / processor_perflib.c
index c4efc0c..0af48a8 100644 (file)
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/cpufreq.h>
+#include <linux/slab.h>
 
-#ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
-#include <linux/proc_fs.h>
-#include <linux/seq_file.h>
-#include <linux/mutex.h>
-
-#include <asm/uaccess.h>
+#ifdef CONFIG_X86
+#include <asm/cpufeature.h>
 #endif
 
 #include <acpi/acpi_bus.h>
+#include <acpi/acpi_drivers.h>
 #include <acpi/processor.h>
 
-#define ACPI_PROCESSOR_COMPONENT       0x01000000
+#define PREFIX "ACPI: "
+
 #define ACPI_PROCESSOR_CLASS           "processor"
 #define ACPI_PROCESSOR_FILE_PERFORMANCE        "performance"
 #define _COMPONENT             ACPI_PROCESSOR_COMPONENT
@@ -60,10 +59,21 @@ static DEFINE_MUTEX(performance_mutex);
  * policy is adjusted accordingly.
  */
 
+/* ignore_ppc:
+ * -1 -> cpufreq low level drivers not initialized -> _PSS, etc. not called yet
+ *       ignore _PPC
+ *  0 -> cpufreq low level drivers initialized -> consider _PPC values
+ *  1 -> ignore _PPC totally -> forced by user through boot param
+ */
+static int ignore_ppc = -1;
+module_param(ignore_ppc, int, 0644);
+MODULE_PARM_DESC(ignore_ppc, "If the frequency of your machine gets wrongly" \
+                "limited by BIOS, this should help");
+
 #define PPC_REGISTERED   1
 #define PPC_IN_USE       2
 
-static int acpi_processor_ppc_status = 0;
+static int acpi_processor_ppc_status;
 
 static int acpi_processor_ppc_notifier(struct notifier_block *nb,
                                       unsigned long event, void *data)
@@ -72,12 +82,20 @@ static int acpi_processor_ppc_notifier(struct notifier_block *nb,
        struct acpi_processor *pr;
        unsigned int ppc = 0;
 
-       mutex_lock(&performance_mutex);
+       if (event == CPUFREQ_START && ignore_ppc <= 0) {
+               ignore_ppc = 0;
+               return 0;
+       }
+
+       if (ignore_ppc)
+               return 0;
 
        if (event != CPUFREQ_INCOMPATIBLE)
-               goto out;
+               return 0;
+
+       mutex_lock(&performance_mutex);
 
-       pr = processors[policy->cpu];
+       pr = per_cpu(processors, policy->cpu);
        if (!pr || !pr->performance)
                goto out;
 
@@ -103,7 +121,7 @@ static struct notifier_block acpi_ppc_notifier_block = {
 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
 {
        acpi_status status = 0;
-       unsigned long ppc = 0;
+       unsigned long long ppc = 0;
 
 
        if (!pr)
@@ -123,20 +141,86 @@ static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
                return -ENODEV;
        }
 
+       pr_debug("CPU %d: _PPC is %d - frequency %s limited\n", pr->id,
+                      (int)ppc, ppc ? "" : "not");
+
        pr->performance_platform_limit = (int)ppc;
 
        return 0;
 }
 
-int acpi_processor_ppc_has_changed(struct acpi_processor *pr)
+#define ACPI_PROCESSOR_NOTIFY_PERFORMANCE      0x80
+/*
+ * acpi_processor_ppc_ost: Notify firmware the _PPC evaluation status
+ * @handle: ACPI processor handle
+ * @status: the status code of _PPC evaluation
+ *     0: success. OSPM is now using the performance state specificed.
+ *     1: failure. OSPM has not changed the number of P-states in use
+ */
+static void acpi_processor_ppc_ost(acpi_handle handle, int status)
+{
+       union acpi_object params[2] = {
+               {.type = ACPI_TYPE_INTEGER,},
+               {.type = ACPI_TYPE_INTEGER,},
+       };
+       struct acpi_object_list arg_list = {2, params};
+       acpi_handle temp;
+
+       params[0].integer.value = ACPI_PROCESSOR_NOTIFY_PERFORMANCE;
+       params[1].integer.value =  status;
+
+       /* when there is no _OST , skip it */
+       if (ACPI_FAILURE(acpi_get_handle(handle, "_OST", &temp)))
+               return;
+
+       acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
+       return;
+}
+
+int acpi_processor_ppc_has_changed(struct acpi_processor *pr, int event_flag)
 {
-       int ret = acpi_processor_get_platform_limit(pr);
+       int ret;
+
+       if (ignore_ppc) {
+               /*
+                * Only when it is notification event, the _OST object
+                * will be evaluated. Otherwise it is skipped.
+                */
+               if (event_flag)
+                       acpi_processor_ppc_ost(pr->handle, 1);
+               return 0;
+       }
+
+       ret = acpi_processor_get_platform_limit(pr);
+       /*
+        * Only when it is notification event, the _OST object
+        * will be evaluated. Otherwise it is skipped.
+        */
+       if (event_flag) {
+               if (ret < 0)
+                       acpi_processor_ppc_ost(pr->handle, 1);
+               else
+                       acpi_processor_ppc_ost(pr->handle, 0);
+       }
        if (ret < 0)
                return (ret);
        else
                return cpufreq_update_policy(pr->id);
 }
 
+int acpi_processor_get_bios_limit(int cpu, unsigned int *limit)
+{
+       struct acpi_processor *pr;
+
+       pr = per_cpu(processors, cpu);
+       if (!pr || !pr->performance || !pr->performance->state_count)
+               return -ENODEV;
+       *limit = pr->performance->states[pr->performance_platform_limit].
+               core_frequency * 1000;
+       return 0;
+}
+EXPORT_SYMBOL(acpi_processor_get_bios_limit);
+
 void acpi_processor_ppc_init(void)
 {
        if (!cpufreq_register_notifier
@@ -156,6 +240,28 @@ void acpi_processor_ppc_exit(void)
        acpi_processor_ppc_status &= ~PPC_REGISTERED;
 }
 
+/*
+ * Do a quick check if the systems looks like it should use ACPI
+ * cpufreq. We look at a _PCT method being available, but don't
+ * do a whole lot of sanity checks.
+ */
+void acpi_processor_load_module(struct acpi_processor *pr)
+{
+       static int requested;
+       acpi_status status = 0;
+       struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
+
+       if (!arch_has_acpi_pdc() || requested)
+               return;
+       status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
+       if (!ACPI_FAILURE(status)) {
+               printk(KERN_INFO PREFIX "Requesting acpi_cpufreq\n");
+               request_module_nowait("acpi_cpufreq");
+               requested = 1;
+       }
+       kfree(buffer.pointer);
+}
+
 static int acpi_processor_get_performance_control(struct acpi_processor *pr)
 {
        int result = 0;
@@ -281,9 +387,15 @@ static int acpi_processor_get_performance_states(struct acpi_processor *pr)
                                  (u32) px->bus_master_latency,
                                  (u32) px->control, (u32) px->status));
 
-               if (!px->core_frequency) {
-                       printk(KERN_ERR PREFIX
-                                   "Invalid _PSS data: freq is zero\n");
+               /*
+                * Check that ACPI's u64 MHz will be valid as u32 KHz in cpufreq
+                */
+               if (!px->core_frequency ||
+                   ((u32)(px->core_frequency * 1000) !=
+                    (px->core_frequency * 1000))) {
+                       printk(KERN_ERR FW_BUG PREFIX
+                              "Invalid BIOS _PSS frequency: 0x%llx MHz\n",
+                              px->core_frequency);
                        result = -EFAULT;
                        kfree(pr->performance->states);
                        goto end;
@@ -302,7 +414,6 @@ static int acpi_processor_get_performance_info(struct acpi_processor *pr)
        acpi_status status = AE_OK;
        acpi_handle handle = NULL;
 
-
        if (!pr || !pr->performance || !pr->handle)
                return -EINVAL;
 
@@ -315,13 +426,31 @@ static int acpi_processor_get_performance_info(struct acpi_processor *pr)
 
        result = acpi_processor_get_performance_control(pr);
        if (result)
-               return result;
+               goto update_bios;
 
        result = acpi_processor_get_performance_states(pr);
        if (result)
-               return result;
+               goto update_bios;
 
-       return 0;
+       /* We need to call _PPC once when cpufreq starts */
+       if (ignore_ppc != 1)
+               result = acpi_processor_get_platform_limit(pr);
+
+       return result;
+
+       /*
+        * Having _PPC but missing frequencies (_PSS, _PCT) is a very good hint that
+        * the BIOS is older than the CPU and does not know its frequencies
+        */
+ update_bios:
+#ifdef CONFIG_X86
+       if (ACPI_SUCCESS(acpi_get_handle(pr->handle, "_PPC", &handle))){
+               if(boot_cpu_has(X86_FEATURE_EST))
+                       printk(KERN_WARNING FW_BUG "BIOS needs update for CPU "
+                              "frequency support\n");
+       }
+#endif
+       return result;
 }
 
 int acpi_processor_notify_smm(struct module *calling_module)
@@ -336,8 +465,8 @@ int acpi_processor_notify_smm(struct module *calling_module)
        if (!try_module_get(calling_module))
                return -EINVAL;
 
-       /* is_done is set to negative if an error occured,
-        * and to postitive if _no_ error occured, but SMM
+       /* is_done is set to negative if an error occurred,
+        * and to postitive if _no_ error occurred, but SMM
         * was already notified. This avoids double notification
         * which might lead to unexpected results...
         */
@@ -385,101 +514,6 @@ int acpi_processor_notify_smm(struct module *calling_module)
 
 EXPORT_SYMBOL(acpi_processor_notify_smm);
 
-#ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
-/* /proc/acpi/processor/../performance interface (DEPRECATED) */
-
-static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file);
-static struct file_operations acpi_processor_perf_fops = {
-       .open = acpi_processor_perf_open_fs,
-       .read = seq_read,
-       .llseek = seq_lseek,
-       .release = single_release,
-};
-
-static int acpi_processor_perf_seq_show(struct seq_file *seq, void *offset)
-{
-       struct acpi_processor *pr = seq->private;
-       int i;
-
-
-       if (!pr)
-               goto end;
-
-       if (!pr->performance) {
-               seq_puts(seq, "<not supported>\n");
-               goto end;
-       }
-
-       seq_printf(seq, "state count:             %d\n"
-                  "active state:            P%d\n",
-                  pr->performance->state_count, pr->performance->state);
-
-       seq_puts(seq, "states:\n");
-       for (i = 0; i < pr->performance->state_count; i++)
-               seq_printf(seq,
-                          "   %cP%d:                  %d MHz, %d mW, %d uS\n",
-                          (i == pr->performance->state ? '*' : ' '), i,
-                          (u32) pr->performance->states[i].core_frequency,
-                          (u32) pr->performance->states[i].power,
-                          (u32) pr->performance->states[i].transition_latency);
-
-      end:
-       return 0;
-}
-
-static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file)
-{
-       return single_open(file, acpi_processor_perf_seq_show,
-                          PDE(inode)->data);
-}
-
-static void acpi_cpufreq_add_file(struct acpi_processor *pr)
-{
-       struct proc_dir_entry *entry = NULL;
-       struct acpi_device *device = NULL;
-
-
-       if (acpi_bus_get_device(pr->handle, &device))
-               return;
-
-       /* add file 'performance' [R/W] */
-       entry = create_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
-                                 S_IFREG | S_IRUGO,
-                                 acpi_device_dir(device));
-       if (entry){
-               entry->proc_fops = &acpi_processor_perf_fops;
-               entry->data = acpi_driver_data(device);
-               entry->owner = THIS_MODULE;
-       }
-       return;
-}
-
-static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
-{
-       struct acpi_device *device = NULL;
-
-
-       if (acpi_bus_get_device(pr->handle, &device))
-               return;
-
-       /* remove file 'performance' */
-       remove_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
-                         acpi_device_dir(device));
-
-       return;
-}
-
-#else
-static void acpi_cpufreq_add_file(struct acpi_processor *pr)
-{
-       return;
-}
-static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
-{
-       return;
-}
-#endif                         /* CONFIG_X86_ACPI_CPUFREQ_PROC_INTF */
-
 static int acpi_processor_get_psd(struct acpi_processor        *pr)
 {
        int result = 0;
@@ -497,13 +531,13 @@ static int acpi_processor_get_psd(struct acpi_processor   *pr)
 
        psd = buffer.pointer;
        if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
-               ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
+               printk(KERN_ERR PREFIX "Invalid _PSD data\n");
                result = -EFAULT;
                goto end;
        }
 
        if (psd->package.count != 1) {
-               ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
+               printk(KERN_ERR PREFIX "Invalid _PSD data\n");
                result = -EFAULT;
                goto end;
        }
@@ -516,47 +550,58 @@ static int acpi_processor_get_psd(struct acpi_processor   *pr)
        status = acpi_extract_package(&(psd->package.elements[0]),
                &format, &state);
        if (ACPI_FAILURE(status)) {
-               ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
+               printk(KERN_ERR PREFIX "Invalid _PSD data\n");
                result = -EFAULT;
                goto end;
        }
 
        if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
-               ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:num_entries\n"));
+               printk(KERN_ERR PREFIX "Unknown _PSD:num_entries\n");
                result = -EFAULT;
                goto end;
        }
 
        if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
-               ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:revision\n"));
+               printk(KERN_ERR PREFIX "Unknown _PSD:revision\n");
                result = -EFAULT;
                goto end;
        }
 
+       if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
+           pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
+           pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
+               printk(KERN_ERR PREFIX "Invalid _PSD:coord_type\n");
+               result = -EFAULT;
+               goto end;
+       }
 end:
        kfree(buffer.pointer);
        return result;
 }
 
 int acpi_processor_preregister_performance(
-               struct acpi_processor_performance **performance)
+               struct acpi_processor_performance __percpu *performance)
 {
        int count, count_target;
        int retval = 0;
        unsigned int i, j;
-       cpumask_t covered_cpus;
+       cpumask_var_t covered_cpus;
        struct acpi_processor *pr;
        struct acpi_psd_package *pdomain;
        struct acpi_processor *match_pr;
        struct acpi_psd_package *match_pdomain;
 
-       mutex_lock(&performance_mutex);
+       if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
+               return -ENOMEM;
 
-       retval = 0;
+       mutex_lock(&performance_mutex);
 
-       /* Call _PSD for all CPUs */
+       /*
+        * Check if another driver has already registered, and abort before
+        * changing pr->performance if it has. Check input data as well.
+        */
        for_each_possible_cpu(i) {
-               pr = processors[i];
+               pr = per_cpu(processors, i);
                if (!pr) {
                        /* Look only at processors in ACPI namespace */
                        continue;
@@ -564,16 +609,23 @@ int acpi_processor_preregister_performance(
 
                if (pr->performance) {
                        retval = -EBUSY;
-                       continue;
+                       goto err_out;
                }
 
-               if (!performance || !performance[i]) {
+               if (!performance || !per_cpu_ptr(performance, i)) {
                        retval = -EINVAL;
-                       continue;
+                       goto err_out;
                }
+       }
+
+       /* Call _PSD for all CPUs */
+       for_each_possible_cpu(i) {
+               pr = per_cpu(processors, i);
+               if (!pr)
+                       continue;
 
-               pr->performance = performance[i];
-               cpu_set(i, pr->performance->shared_cpu_map);
+               pr->performance = per_cpu_ptr(performance, i);
+               cpumask_set_cpu(i, pr->performance->shared_cpu_map);
                if (acpi_processor_get_psd(pr)) {
                        retval = -EINVAL;
                        continue;
@@ -587,37 +639,16 @@ int acpi_processor_preregister_performance(
         * domain info.
         */
        for_each_possible_cpu(i) {
-               pr = processors[i];
+               pr = per_cpu(processors, i);
                if (!pr)
                        continue;
 
-               /* Basic validity check for domain info */
-               pdomain = &(pr->performance->domain_info);
-               if ((pdomain->revision != ACPI_PSD_REV0_REVISION) ||
-                   (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES)) {
-                       retval = -EINVAL;
-                       goto err_ret;
-               }
-               if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
-                   pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
-                   pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
-                       retval = -EINVAL;
-                       goto err_ret;
-               }
-       }
-
-       cpus_clear(covered_cpus);
-       for_each_possible_cpu(i) {
-               pr = processors[i];
-               if (!pr)
-                       continue;
-
-               if (cpu_isset(i, covered_cpus))
+               if (cpumask_test_cpu(i, covered_cpus))
                        continue;
 
                pdomain = &(pr->performance->domain_info);
-               cpu_set(i, pr->performance->shared_cpu_map);
-               cpu_set(i, covered_cpus);
+               cpumask_set_cpu(i, pr->performance->shared_cpu_map);
+               cpumask_set_cpu(i, covered_cpus);
                if (pdomain->num_processors <= 1)
                        continue;
 
@@ -635,7 +666,7 @@ int acpi_processor_preregister_performance(
                        if (i == j)
                                continue;
 
-                       match_pr = processors[j];
+                       match_pr = per_cpu(processors, j);
                        if (!match_pr)
                                continue;
 
@@ -655,8 +686,8 @@ int acpi_processor_preregister_performance(
                                goto err_ret;
                        }
 
-                       cpu_set(j, covered_cpus);
-                       cpu_set(j, pr->performance->shared_cpu_map);
+                       cpumask_set_cpu(j, covered_cpus);
+                       cpumask_set_cpu(j, pr->performance->shared_cpu_map);
                        count++;
                }
 
@@ -664,7 +695,7 @@ int acpi_processor_preregister_performance(
                        if (i == j)
                                continue;
 
-                       match_pr = processors[j];
+                       match_pr = per_cpu(processors, j);
                        if (!match_pr)
                                continue;
 
@@ -674,45 +705,45 @@ int acpi_processor_preregister_performance(
 
                        match_pr->performance->shared_type = 
                                        pr->performance->shared_type;
-                       match_pr->performance->shared_cpu_map =
-                               pr->performance->shared_cpu_map;
+                       cpumask_copy(match_pr->performance->shared_cpu_map,
+                                    pr->performance->shared_cpu_map);
                }
        }
 
 err_ret:
        for_each_possible_cpu(i) {
-               pr = processors[i];
+               pr = per_cpu(processors, i);
                if (!pr || !pr->performance)
                        continue;
 
                /* Assume no coordination on any error parsing domain info */
                if (retval) {
-                       cpus_clear(pr->performance->shared_cpu_map);
-                       cpu_set(i, pr->performance->shared_cpu_map);
+                       cpumask_clear(pr->performance->shared_cpu_map);
+                       cpumask_set_cpu(i, pr->performance->shared_cpu_map);
                        pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
                }
                pr->performance = NULL; /* Will be set for real in register */
        }
 
+err_out:
        mutex_unlock(&performance_mutex);
+       free_cpumask_var(covered_cpus);
        return retval;
 }
 EXPORT_SYMBOL(acpi_processor_preregister_performance);
 
-
 int
 acpi_processor_register_performance(struct acpi_processor_performance
                                    *performance, unsigned int cpu)
 {
        struct acpi_processor *pr;
 
-
        if (!(acpi_processor_ppc_status & PPC_REGISTERED))
                return -EINVAL;
 
        mutex_lock(&performance_mutex);
 
-       pr = processors[cpu];
+       pr = per_cpu(processors, cpu);
        if (!pr) {
                mutex_unlock(&performance_mutex);
                return -ENODEV;
@@ -733,8 +764,6 @@ acpi_processor_register_performance(struct acpi_processor_performance
                return -EIO;
        }
 
-       acpi_cpufreq_add_file(pr);
-
        mutex_unlock(&performance_mutex);
        return 0;
 }
@@ -747,10 +776,9 @@ acpi_processor_unregister_performance(struct acpi_processor_performance
 {
        struct acpi_processor *pr;
 
-
        mutex_lock(&performance_mutex);
 
-       pr = processors[cpu];
+       pr = per_cpu(processors, cpu);
        if (!pr) {
                mutex_unlock(&performance_mutex);
                return;
@@ -760,8 +788,6 @@ acpi_processor_unregister_performance(struct acpi_processor_performance
                kfree(pr->performance->states);
        pr->performance = NULL;
 
-       acpi_cpufreq_remove_file(pr);
-
        mutex_unlock(&performance_mutex);
 
        return;