[ACPI] merge 3549 4320 4485 4588 4980 5483 5651 acpica asus fops pnpacpi branches...
[firefly-linux-kernel-4.4.55.git] / drivers / acpi / processor_idle.c
index 26a3a4016115364467b5feff71415c7e88f4d016..be2dae52f6fa47a6b74dbca63c47a553236cfd27 100644 (file)
@@ -37,6 +37,7 @@
 #include <linux/acpi.h>
 #include <linux/dmi.h>
 #include <linux/moduleparam.h>
+#include <linux/sched.h>       /* need_resched() */
 
 #include <asm/io.h>
 #include <asm/uaccess.h>
@@ -94,22 +95,57 @@ static int set_max_cstate(struct dmi_system_id *id)
 }
 
 static struct dmi_system_id __initdata processor_power_dmi_table[] = {
-       {set_max_cstate, "IBM ThinkPad R40e", {
-                                              DMI_MATCH(DMI_BIOS_VENDOR,
-                                                        "IBM"),
-                                              DMI_MATCH(DMI_BIOS_VERSION,
-                                                        "1SET60WW")},
-        (void *)1},
-       {set_max_cstate, "Medion 41700", {
-                                         DMI_MATCH(DMI_BIOS_VENDOR,
-                                                   "Phoenix Technologies LTD"),
-                                         DMI_MATCH(DMI_BIOS_VERSION,
-                                                   "R01-A1J")}, (void *)1},
-       {set_max_cstate, "Clevo 5600D", {
-                                        DMI_MATCH(DMI_BIOS_VENDOR,
-                                                  "Phoenix Technologies LTD"),
-                                        DMI_MATCH(DMI_BIOS_VERSION,
-                                                  "SHE845M0.86C.0013.D.0302131307")},
+       { set_max_cstate, "IBM ThinkPad R40e", {
+         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
+         DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW")}, (void *)1},
+       { set_max_cstate, "IBM ThinkPad R40e", {
+         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
+         DMI_MATCH(DMI_BIOS_VERSION,"1SET43WW") }, (void*)1},
+       { set_max_cstate, "IBM ThinkPad R40e", {
+         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
+         DMI_MATCH(DMI_BIOS_VERSION,"1SET45WW") }, (void*)1},
+       { set_max_cstate, "IBM ThinkPad R40e", {
+         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
+         DMI_MATCH(DMI_BIOS_VERSION,"1SET47WW") }, (void*)1},
+       { set_max_cstate, "IBM ThinkPad R40e", {
+         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
+         DMI_MATCH(DMI_BIOS_VERSION,"1SET50WW") }, (void*)1},
+       { set_max_cstate, "IBM ThinkPad R40e", {
+         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
+         DMI_MATCH(DMI_BIOS_VERSION,"1SET52WW") }, (void*)1},
+       { set_max_cstate, "IBM ThinkPad R40e", {
+         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
+         DMI_MATCH(DMI_BIOS_VERSION,"1SET55WW") }, (void*)1},
+       { set_max_cstate, "IBM ThinkPad R40e", {
+         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
+         DMI_MATCH(DMI_BIOS_VERSION,"1SET56WW") }, (void*)1},
+       { set_max_cstate, "IBM ThinkPad R40e", {
+         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
+         DMI_MATCH(DMI_BIOS_VERSION,"1SET59WW") }, (void*)1},
+       { set_max_cstate, "IBM ThinkPad R40e", {
+         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
+         DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW") }, (void*)1},
+       { set_max_cstate, "IBM ThinkPad R40e", {
+         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
+         DMI_MATCH(DMI_BIOS_VERSION,"1SET61WW") }, (void*)1},
+       { set_max_cstate, "IBM ThinkPad R40e", {
+         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
+         DMI_MATCH(DMI_BIOS_VERSION,"1SET62WW") }, (void*)1},
+       { set_max_cstate, "IBM ThinkPad R40e", {
+         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
+         DMI_MATCH(DMI_BIOS_VERSION,"1SET64WW") }, (void*)1},
+       { set_max_cstate, "IBM ThinkPad R40e", {
+         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
+         DMI_MATCH(DMI_BIOS_VERSION,"1SET65WW") }, (void*)1},
+       { set_max_cstate, "IBM ThinkPad R40e", {
+         DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
+         DMI_MATCH(DMI_BIOS_VERSION,"1SET68WW") }, (void*)1},
+       { set_max_cstate, "Medion 41700", {
+         DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
+         DMI_MATCH(DMI_BIOS_VERSION,"R01-A1J")}, (void *)1},
+       { set_max_cstate, "Clevo 5600D", {
+         DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
+         DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
         (void *)2},
        {},
 };
@@ -166,6 +202,15 @@ acpi_processor_power_activate(struct acpi_processor *pr,
        return;
 }
 
+static void acpi_safe_halt(void)
+{
+       clear_thread_flag(TIF_POLLING_NRFLAG);
+       smp_mb__after_clear_bit();
+       if (!need_resched())
+               safe_halt();
+       set_thread_flag(TIF_POLLING_NRFLAG);
+}
+
 static atomic_t c3_cpu_count;
 
 static void acpi_processor_idle(void)
@@ -176,7 +221,7 @@ static void acpi_processor_idle(void)
        int sleep_ticks = 0;
        u32 t1, t2 = 0;
 
-       pr = processors[raw_smp_processor_id()];
+       pr = processors[smp_processor_id()];
        if (!pr)
                return;
 
@@ -196,8 +241,13 @@ static void acpi_processor_idle(void)
        }
 
        cx = pr->power.state;
-       if (!cx)
-               goto easy_out;
+       if (!cx) {
+               if (pm_idle_save)
+                       pm_idle_save();
+               else
+                       acpi_safe_halt();
+               return;
+       }
 
        /*
         * Check BM Activity
@@ -259,6 +309,17 @@ static void acpi_processor_idle(void)
                }
        }
 
+#ifdef CONFIG_HOTPLUG_CPU
+       /*
+        * Check for P_LVL2_UP flag before entering C2 and above on
+        * an SMP system. We do it here instead of doing it at _CST/P_LVL
+        * detection phase, to work cleanly with logical CPU hotplug.
+        */
+       if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) && 
+           !pr->flags.has_cst && !acpi_fadt.plvl2_up)
+               cx = &pr->power.states[ACPI_STATE_C1];
+#endif
+
        cx->usage++;
 
        /*
@@ -266,6 +327,16 @@ static void acpi_processor_idle(void)
         * ------
         * Invoke the current Cx state to put the processor to sleep.
         */
+       if (cx->type == ACPI_STATE_C2 || cx->type == ACPI_STATE_C3) {
+               clear_thread_flag(TIF_POLLING_NRFLAG);
+               smp_mb__after_clear_bit();
+               if (need_resched()) {
+                       set_thread_flag(TIF_POLLING_NRFLAG);
+                       local_irq_enable();
+                       return;
+               }
+       }
+
        switch (cx->type) {
 
        case ACPI_STATE_C1:
@@ -277,7 +348,8 @@ static void acpi_processor_idle(void)
                if (pm_idle_save)
                        pm_idle_save();
                else
-                       safe_halt();
+                       acpi_safe_halt();
+
                /*
                 * TBD: Can't get time duration while in C1, as resumes
                 *      go to an ISR rather than here.  Need to instrument
@@ -297,6 +369,7 @@ static void acpi_processor_idle(void)
                t2 = inl(acpi_fadt.xpm_tmr_blk.address);
                /* Re-enable interrupts */
                local_irq_enable();
+               set_thread_flag(TIF_POLLING_NRFLAG);
                /* Compute time (ticks) that we were actually asleep */
                sleep_ticks =
                    ticks_elapsed(t1, t2) - cx->latency_ticks - C2_OVERHEAD;
@@ -336,6 +409,7 @@ static void acpi_processor_idle(void)
 
                /* Re-enable interrupts */
                local_irq_enable();
+               set_thread_flag(TIF_POLLING_NRFLAG);
                /* Compute time (ticks) that we were actually asleep */
                sleep_ticks =
                    ticks_elapsed(t1, t2) - cx->latency_ticks - C3_OVERHEAD;
@@ -348,6 +422,15 @@ static void acpi_processor_idle(void)
 
        next_state = pr->power.state;
 
+#ifdef CONFIG_HOTPLUG_CPU
+       /* Don't do promotion/demotion */
+       if ((cx->type == ACPI_STATE_C1) && (num_online_cpus() > 1) &&
+           !pr->flags.has_cst && !acpi_fadt.plvl2_up) {
+               next_state = cx;
+               goto end;
+       }
+#endif
+
        /*
         * Promotion?
         * ----------
@@ -413,16 +496,6 @@ static void acpi_processor_idle(void)
         */
        if (next_state != pr->power.state)
                acpi_processor_power_activate(pr, next_state);
-
-       return;
-
-      easy_out:
-       /* do C1 instead of busy loop */
-       if (pm_idle_save)
-               pm_idle_save();
-       else
-               safe_halt();
-       return;
 }
 
 static int acpi_processor_set_power_policy(struct acpi_processor *pr)
@@ -504,8 +577,6 @@ static int acpi_processor_set_power_policy(struct acpi_processor *pr)
 
 static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
 {
-       int i;
-
        ACPI_FUNCTION_TRACE("acpi_processor_get_power_info_fadt");
 
        if (!pr)
@@ -514,18 +585,18 @@ static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
        if (!pr->pblk)
                return_VALUE(-ENODEV);
 
-       for (i = 0; i < ACPI_PROCESSOR_MAX_POWER; i++)
-               memset(pr->power.states, 0, sizeof(struct acpi_processor_cx));
-
        /* if info is obtained from pblk/fadt, type equals state */
-       pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
        pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
        pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
 
-       /* the C0 state only exists as a filler in our array,
-        * and all processors need to support C1 */
-       pr->power.states[ACPI_STATE_C0].valid = 1;
-       pr->power.states[ACPI_STATE_C1].valid = 1;
+#ifndef CONFIG_HOTPLUG_CPU
+       /*
+        * Check for P_LVL2_UP flag before entering C2 and above on
+        * an SMP system. 
+        */
+       if ((num_online_cpus() > 1) && !acpi_fadt.plvl2_up)
+               return_VALUE(-ENODEV);
+#endif
 
        /* determine C2 and C3 address from pblk */
        pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
@@ -545,18 +616,13 @@ static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
 
 static int acpi_processor_get_power_info_default_c1(struct acpi_processor *pr)
 {
-       int i;
-
        ACPI_FUNCTION_TRACE("acpi_processor_get_power_info_default_c1");
 
-       for (i = 0; i < ACPI_PROCESSOR_MAX_POWER; i++)
-               memset(&(pr->power.states[i]), 0,
-                      sizeof(struct acpi_processor_cx));
+       /* Zero initialize all the C-states info. */
+       memset(pr->power.states, 0, sizeof(pr->power.states));
 
-       /* if info is obtained from pblk/fadt, type equals state */
+       /* set the first C-State to C1 */
        pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
-       pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
-       pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
 
        /* the C0 state only exists as a filler in our array,
         * and all processors need to support C1 */
@@ -570,6 +636,7 @@ static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
 {
        acpi_status status = 0;
        acpi_integer count;
+       int current_count;
        int i;
        struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
        union acpi_object *cst;
@@ -579,10 +646,12 @@ static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
        if (nocst)
                return_VALUE(-ENODEV);
 
-       pr->power.count = 0;
-       for (i = 0; i < ACPI_PROCESSOR_MAX_POWER; i++)
-               memset(&(pr->power.states[i]), 0,
-                      sizeof(struct acpi_processor_cx));
+       current_count = 1;
+
+       /* Zero initialize C2 onwards and prepare for fresh CST lookup */
+       for (i = 2; i < ACPI_PROCESSOR_MAX_POWER; i++)
+               memset(&(pr->power.states[i]), 0, 
+                               sizeof(struct acpi_processor_cx));
 
        status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
        if (ACPI_FAILURE(status)) {
@@ -610,16 +679,6 @@ static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
                goto end;
        }
 
-       /* We support up to ACPI_PROCESSOR_MAX_POWER. */
-       if (count > ACPI_PROCESSOR_MAX_POWER) {
-               printk(KERN_WARNING
-                      "Limiting number of power states to max (%d)\n",
-                      ACPI_PROCESSOR_MAX_POWER);
-               printk(KERN_WARNING
-                      "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
-               count = ACPI_PROCESSOR_MAX_POWER;
-       }
-
        /* Tell driver that at least _CST is supported. */
        pr->flags.has_cst = 1;
 
@@ -663,7 +722,7 @@ static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
                    (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO))
                        continue;
 
-               if ((cx.type < ACPI_STATE_C1) || (cx.type > ACPI_STATE_C3))
+               if ((cx.type < ACPI_STATE_C2) || (cx.type > ACPI_STATE_C3))
                        continue;
 
                obj = (union acpi_object *)&(element->package.elements[2]);
@@ -678,16 +737,29 @@ static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
 
                cx.power = obj->integer.value;
 
-               (pr->power.count)++;
-               memcpy(&(pr->power.states[pr->power.count]), &cx, sizeof(cx));
+               current_count++;
+               memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
+
+               /*
+                * We support total ACPI_PROCESSOR_MAX_POWER - 1
+                * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
+                */
+               if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
+                       printk(KERN_WARNING
+                              "Limiting number of power states to max (%d)\n",
+                              ACPI_PROCESSOR_MAX_POWER);
+                       printk(KERN_WARNING
+                              "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
+                       break;
+               }
        }
 
        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
-                         pr->power.count));
+                         current_count));
 
        /* Validate number of power states discovered */
-       if (pr->power.count < 2)
-               status = -ENODEV;
+       if (current_count < 2)
+               status = -EFAULT;
 
       end:
        acpi_os_free(buffer.pointer);
@@ -803,6 +875,15 @@ static int acpi_processor_power_verify(struct acpi_processor *pr)
        unsigned int i;
        unsigned int working = 0;
 
+#ifdef ARCH_APICTIMER_STOPS_ON_C3
+       struct cpuinfo_x86 *c = cpu_data + pr->id;
+       cpumask_t mask = cpumask_of_cpu(pr->id);
+
+       if (c->x86_vendor == X86_VENDOR_INTEL) {
+               on_each_cpu(switch_ipi_to_APIC_timer, &mask, 1, 1);
+       }
+#endif
+
        for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
                struct acpi_processor_cx *cx = &pr->power.states[i];
 
@@ -817,6 +898,12 @@ static int acpi_processor_power_verify(struct acpi_processor *pr)
 
                case ACPI_STATE_C3:
                        acpi_processor_power_verify_c3(pr, cx);
+#ifdef ARCH_APICTIMER_STOPS_ON_C3
+                       if (c->x86_vendor == X86_VENDOR_INTEL) {
+                               on_each_cpu(switch_APIC_timer_to_ipi,
+                                               &mask, 1, 1);
+                       }
+#endif
                        break;
                }
 
@@ -837,12 +924,13 @@ static int acpi_processor_get_power_info(struct acpi_processor *pr)
        /* NOTE: the idle thread may not be running while calling
         * this function */
 
+       /* Adding C1 state */
+       acpi_processor_get_power_info_default_c1(pr);
        result = acpi_processor_get_power_info_cst(pr);
-       if ((result) || (acpi_processor_power_verify(pr) < 2)) {
-               result = acpi_processor_get_power_info_fadt(pr);
-               if ((result) || (acpi_processor_power_verify(pr) < 2))
-                       result = acpi_processor_get_power_info_default_c1(pr);
-       }
+       if (result == -ENODEV)
+               acpi_processor_get_power_info_fadt(pr);
+
+       pr->power.count = acpi_processor_power_verify(pr);
 
        /*
         * Set Default Policy
@@ -863,7 +951,8 @@ static int acpi_processor_get_power_info(struct acpi_processor *pr)
        for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
                if (pr->power.states[i].valid) {
                        pr->power.count = i;
-                       pr->flags.power = 1;
+                       if (pr->power.states[i].type >= ACPI_STATE_C2)
+                               pr->flags.power = 1;
                }
        }
 
@@ -1010,8 +1099,6 @@ int acpi_processor_power_init(struct acpi_processor *pr,
                }
        }
 
-       acpi_processor_power_init_pdc(&(pr->power), pr->id);
-       acpi_processor_set_pdc(pr, pr->power.pdc);
        acpi_processor_get_power_info(pr);
 
        /*