GNU Linux-libre 4.9.308-gnu1
[releases.git] / drivers / acpi / processor_idle.c
1 /*
2  * processor_idle - idle state submodule to the ACPI processor driver
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *  Copyright (C) 2004, 2005 Dominik Brodowski <linux@brodo.de>
7  *  Copyright (C) 2004  Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
8  *                      - Added processor hotplug support
9  *  Copyright (C) 2005  Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
10  *                      - Added support for C3 on SMP
11  *
12  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13  *
14  *  This program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License as published by
16  *  the Free Software Foundation; either version 2 of the License, or (at
17  *  your option) any later version.
18  *
19  *  This program is distributed in the hope that it will be useful, but
20  *  WITHOUT ANY WARRANTY; without even the implied warranty of
21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  *  General Public License for more details.
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  */
26 #define pr_fmt(fmt) "ACPI: " fmt
27
28 #include <linux/module.h>
29 #include <linux/acpi.h>
30 #include <linux/dmi.h>
31 #include <linux/sched.h>       /* need_resched() */
32 #include <linux/sort.h>
33 #include <linux/tick.h>
34 #include <linux/cpuidle.h>
35 #include <linux/cpu.h>
36 #include <acpi/processor.h>
37
38 /*
39  * Include the apic definitions for x86 to have the APIC timer related defines
40  * available also for UP (on SMP it gets magically included via linux/smp.h).
41  * asm/acpi.h is not an option, as it would require more include magic. Also
42  * creating an empty asm-ia64/apic.h would just trade pest vs. cholera.
43  */
44 #ifdef CONFIG_X86
45 #include <asm/apic.h>
46 #endif
47
48 #define ACPI_PROCESSOR_CLASS            "processor"
49 #define _COMPONENT              ACPI_PROCESSOR_COMPONENT
50 ACPI_MODULE_NAME("processor_idle");
51
52 static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
53 module_param(max_cstate, uint, 0000);
54 static unsigned int nocst __read_mostly;
55 module_param(nocst, uint, 0000);
56 static int bm_check_disable __read_mostly;
57 module_param(bm_check_disable, uint, 0000);
58
59 static unsigned int latency_factor __read_mostly = 2;
60 module_param(latency_factor, uint, 0644);
61
62 static DEFINE_PER_CPU(struct cpuidle_device *, acpi_cpuidle_device);
63
64 struct cpuidle_driver acpi_idle_driver = {
65         .name =         "acpi_idle",
66         .owner =        THIS_MODULE,
67 };
68
69 #ifdef CONFIG_ACPI_PROCESSOR_CSTATE
70 static
71 DEFINE_PER_CPU(struct acpi_processor_cx * [CPUIDLE_STATE_MAX], acpi_cstate);
72
73 static int disabled_by_idle_boot_param(void)
74 {
75         return boot_option_idle_override == IDLE_POLL ||
76                 boot_option_idle_override == IDLE_HALT;
77 }
78
79 /*
80  * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
81  * For now disable this. Probably a bug somewhere else.
82  *
83  * To skip this limit, boot/load with a large max_cstate limit.
84  */
85 static int set_max_cstate(const struct dmi_system_id *id)
86 {
87         if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
88                 return 0;
89
90         pr_notice("%s detected - limiting to C%ld max_cstate."
91                   " Override with \"processor.max_cstate=%d\"\n", id->ident,
92                   (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
93
94         max_cstate = (long)id->driver_data;
95
96         return 0;
97 }
98
99 static const struct dmi_system_id processor_power_dmi_table[] = {
100         { set_max_cstate, "Clevo 5600D", {
101           DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
102           DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
103          (void *)2},
104         { set_max_cstate, "Pavilion zv5000", {
105           DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
106           DMI_MATCH(DMI_PRODUCT_NAME,"Pavilion zv5000 (DS502A#ABA)")},
107          (void *)1},
108         { set_max_cstate, "Asus L8400B", {
109           DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
110           DMI_MATCH(DMI_PRODUCT_NAME,"L8400B series Notebook PC")},
111          (void *)1},
112         {},
113 };
114
115
116 /*
117  * Callers should disable interrupts before the call and enable
118  * interrupts after return.
119  */
120 static void __cpuidle acpi_safe_halt(void)
121 {
122         if (!tif_need_resched()) {
123                 safe_halt();
124                 local_irq_disable();
125         }
126 }
127
128 #ifdef ARCH_APICTIMER_STOPS_ON_C3
129
130 /*
131  * Some BIOS implementations switch to C3 in the published C2 state.
132  * This seems to be a common problem on AMD boxen, but other vendors
133  * are affected too. We pick the most conservative approach: we assume
134  * that the local APIC stops in both C2 and C3.
135  */
136 static void lapic_timer_check_state(int state, struct acpi_processor *pr,
137                                    struct acpi_processor_cx *cx)
138 {
139         struct acpi_processor_power *pwr = &pr->power;
140         u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
141
142         if (cpu_has(&cpu_data(pr->id), X86_FEATURE_ARAT))
143                 return;
144
145         if (amd_e400_c1e_detected)
146                 type = ACPI_STATE_C1;
147
148         /*
149          * Check, if one of the previous states already marked the lapic
150          * unstable
151          */
152         if (pwr->timer_broadcast_on_state < state)
153                 return;
154
155         if (cx->type >= type)
156                 pr->power.timer_broadcast_on_state = state;
157 }
158
159 static void __lapic_timer_propagate_broadcast(void *arg)
160 {
161         struct acpi_processor *pr = (struct acpi_processor *) arg;
162
163         if (pr->power.timer_broadcast_on_state < INT_MAX)
164                 tick_broadcast_enable();
165         else
166                 tick_broadcast_disable();
167 }
168
169 static void lapic_timer_propagate_broadcast(struct acpi_processor *pr)
170 {
171         smp_call_function_single(pr->id, __lapic_timer_propagate_broadcast,
172                                  (void *)pr, 1);
173 }
174
175 /* Power(C) State timer broadcast control */
176 static void lapic_timer_state_broadcast(struct acpi_processor *pr,
177                                        struct acpi_processor_cx *cx,
178                                        int broadcast)
179 {
180         int state = cx - pr->power.states;
181
182         if (state >= pr->power.timer_broadcast_on_state) {
183                 if (broadcast)
184                         tick_broadcast_enter();
185                 else
186                         tick_broadcast_exit();
187         }
188 }
189
190 #else
191
192 static void lapic_timer_check_state(int state, struct acpi_processor *pr,
193                                    struct acpi_processor_cx *cstate) { }
194 static void lapic_timer_propagate_broadcast(struct acpi_processor *pr) { }
195 static void lapic_timer_state_broadcast(struct acpi_processor *pr,
196                                        struct acpi_processor_cx *cx,
197                                        int broadcast)
198 {
199 }
200
201 #endif
202
203 #if defined(CONFIG_X86)
204 static void tsc_check_state(int state)
205 {
206         switch (boot_cpu_data.x86_vendor) {
207         case X86_VENDOR_AMD:
208         case X86_VENDOR_INTEL:
209                 /*
210                  * AMD Fam10h TSC will tick in all
211                  * C/P/S0/S1 states when this bit is set.
212                  */
213                 if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
214                         return;
215
216                 /*FALL THROUGH*/
217         default:
218                 /* TSC could halt in idle, so notify users */
219                 if (state > ACPI_STATE_C1)
220                         mark_tsc_unstable("TSC halts in idle");
221         }
222 }
223 #else
224 static void tsc_check_state(int state) { return; }
225 #endif
226
227 static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
228 {
229
230         if (!pr->pblk)
231                 return -ENODEV;
232
233         /* if info is obtained from pblk/fadt, type equals state */
234         pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
235         pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
236
237 #ifndef CONFIG_HOTPLUG_CPU
238         /*
239          * Check for P_LVL2_UP flag before entering C2 and above on
240          * an SMP system.
241          */
242         if ((num_online_cpus() > 1) &&
243             !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
244                 return -ENODEV;
245 #endif
246
247         /* determine C2 and C3 address from pblk */
248         pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
249         pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
250
251         /* determine latencies from FADT */
252         pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.c2_latency;
253         pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.c3_latency;
254
255         /*
256          * FADT specified C2 latency must be less than or equal to
257          * 100 microseconds.
258          */
259         if (acpi_gbl_FADT.c2_latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
260                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
261                         "C2 latency too large [%d]\n", acpi_gbl_FADT.c2_latency));
262                 /* invalidate C2 */
263                 pr->power.states[ACPI_STATE_C2].address = 0;
264         }
265
266         /*
267          * FADT supplied C3 latency must be less than or equal to
268          * 1000 microseconds.
269          */
270         if (acpi_gbl_FADT.c3_latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
271                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
272                         "C3 latency too large [%d]\n", acpi_gbl_FADT.c3_latency));
273                 /* invalidate C3 */
274                 pr->power.states[ACPI_STATE_C3].address = 0;
275         }
276
277         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
278                           "lvl2[0x%08x] lvl3[0x%08x]\n",
279                           pr->power.states[ACPI_STATE_C2].address,
280                           pr->power.states[ACPI_STATE_C3].address));
281
282         return 0;
283 }
284
285 static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
286 {
287         if (!pr->power.states[ACPI_STATE_C1].valid) {
288                 /* set the first C-State to C1 */
289                 /* all processors need to support C1 */
290                 pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
291                 pr->power.states[ACPI_STATE_C1].valid = 1;
292                 pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
293         }
294         /* the C0 state only exists as a filler in our array */
295         pr->power.states[ACPI_STATE_C0].valid = 1;
296         return 0;
297 }
298
299 static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
300 {
301         acpi_status status;
302         u64 count;
303         int current_count;
304         int i, ret = 0;
305         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
306         union acpi_object *cst;
307
308         if (nocst)
309                 return -ENODEV;
310
311         current_count = 0;
312
313         status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
314         if (ACPI_FAILURE(status)) {
315                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
316                 return -ENODEV;
317         }
318
319         cst = buffer.pointer;
320
321         /* There must be at least 2 elements */
322         if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
323                 pr_err("not enough elements in _CST\n");
324                 ret = -EFAULT;
325                 goto end;
326         }
327
328         count = cst->package.elements[0].integer.value;
329
330         /* Validate number of power states. */
331         if (count < 1 || count != cst->package.count - 1) {
332                 pr_err("count given by _CST is not valid\n");
333                 ret = -EFAULT;
334                 goto end;
335         }
336
337         /* Tell driver that at least _CST is supported. */
338         pr->flags.has_cst = 1;
339
340         for (i = 1; i <= count; i++) {
341                 union acpi_object *element;
342                 union acpi_object *obj;
343                 struct acpi_power_register *reg;
344                 struct acpi_processor_cx cx;
345
346                 memset(&cx, 0, sizeof(cx));
347
348                 element = &(cst->package.elements[i]);
349                 if (element->type != ACPI_TYPE_PACKAGE)
350                         continue;
351
352                 if (element->package.count != 4)
353                         continue;
354
355                 obj = &(element->package.elements[0]);
356
357                 if (obj->type != ACPI_TYPE_BUFFER)
358                         continue;
359
360                 reg = (struct acpi_power_register *)obj->buffer.pointer;
361
362                 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
363                     (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
364                         continue;
365
366                 /* There should be an easy way to extract an integer... */
367                 obj = &(element->package.elements[1]);
368                 if (obj->type != ACPI_TYPE_INTEGER)
369                         continue;
370
371                 cx.type = obj->integer.value;
372                 /*
373                  * Some buggy BIOSes won't list C1 in _CST -
374                  * Let acpi_processor_get_power_info_default() handle them later
375                  */
376                 if (i == 1 && cx.type != ACPI_STATE_C1)
377                         current_count++;
378
379                 cx.address = reg->address;
380                 cx.index = current_count + 1;
381
382                 cx.entry_method = ACPI_CSTATE_SYSTEMIO;
383                 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
384                         if (acpi_processor_ffh_cstate_probe
385                                         (pr->id, &cx, reg) == 0) {
386                                 cx.entry_method = ACPI_CSTATE_FFH;
387                         } else if (cx.type == ACPI_STATE_C1) {
388                                 /*
389                                  * C1 is a special case where FIXED_HARDWARE
390                                  * can be handled in non-MWAIT way as well.
391                                  * In that case, save this _CST entry info.
392                                  * Otherwise, ignore this info and continue.
393                                  */
394                                 cx.entry_method = ACPI_CSTATE_HALT;
395                                 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
396                         } else {
397                                 continue;
398                         }
399                         if (cx.type == ACPI_STATE_C1 &&
400                             (boot_option_idle_override == IDLE_NOMWAIT)) {
401                                 /*
402                                  * In most cases the C1 space_id obtained from
403                                  * _CST object is FIXED_HARDWARE access mode.
404                                  * But when the option of idle=halt is added,
405                                  * the entry_method type should be changed from
406                                  * CSTATE_FFH to CSTATE_HALT.
407                                  * When the option of idle=nomwait is added,
408                                  * the C1 entry_method type should be
409                                  * CSTATE_HALT.
410                                  */
411                                 cx.entry_method = ACPI_CSTATE_HALT;
412                                 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
413                         }
414                 } else {
415                         snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
416                                  cx.address);
417                 }
418
419                 if (cx.type == ACPI_STATE_C1) {
420                         cx.valid = 1;
421                 }
422
423                 obj = &(element->package.elements[2]);
424                 if (obj->type != ACPI_TYPE_INTEGER)
425                         continue;
426
427                 cx.latency = obj->integer.value;
428
429                 obj = &(element->package.elements[3]);
430                 if (obj->type != ACPI_TYPE_INTEGER)
431                         continue;
432
433                 current_count++;
434                 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
435
436                 /*
437                  * We support total ACPI_PROCESSOR_MAX_POWER - 1
438                  * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
439                  */
440                 if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
441                         pr_warn("Limiting number of power states to max (%d)\n",
442                                 ACPI_PROCESSOR_MAX_POWER);
443                         pr_warn("Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
444                         break;
445                 }
446         }
447
448         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
449                           current_count));
450
451         /* Validate number of power states discovered */
452         if (current_count < 2)
453                 ret = -EFAULT;
454
455       end:
456         kfree(buffer.pointer);
457
458         return ret;
459 }
460
461 static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
462                                            struct acpi_processor_cx *cx)
463 {
464         static int bm_check_flag = -1;
465         static int bm_control_flag = -1;
466
467
468         if (!cx->address)
469                 return;
470
471         /*
472          * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
473          * DMA transfers are used by any ISA device to avoid livelock.
474          * Note that we could disable Type-F DMA (as recommended by
475          * the erratum), but this is known to disrupt certain ISA
476          * devices thus we take the conservative approach.
477          */
478         else if (errata.piix4.fdma) {
479                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
480                                   "C3 not supported on PIIX4 with Type-F DMA\n"));
481                 return;
482         }
483
484         /* All the logic here assumes flags.bm_check is same across all CPUs */
485         if (bm_check_flag == -1) {
486                 /* Determine whether bm_check is needed based on CPU  */
487                 acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
488                 bm_check_flag = pr->flags.bm_check;
489                 bm_control_flag = pr->flags.bm_control;
490         } else {
491                 pr->flags.bm_check = bm_check_flag;
492                 pr->flags.bm_control = bm_control_flag;
493         }
494
495         if (pr->flags.bm_check) {
496                 if (!pr->flags.bm_control) {
497                         if (pr->flags.has_cst != 1) {
498                                 /* bus mastering control is necessary */
499                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
500                                         "C3 support requires BM control\n"));
501                                 return;
502                         } else {
503                                 /* Here we enter C3 without bus mastering */
504                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
505                                         "C3 support without BM control\n"));
506                         }
507                 }
508         } else {
509                 /*
510                  * WBINVD should be set in fadt, for C3 state to be
511                  * supported on when bm_check is not required.
512                  */
513                 if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
514                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
515                                           "Cache invalidation should work properly"
516                                           " for C3 to be enabled on SMP systems\n"));
517                         return;
518                 }
519         }
520
521         /*
522          * Otherwise we've met all of our C3 requirements.
523          * Normalize the C3 latency to expidite policy.  Enable
524          * checking of bus mastering status (bm_check) so we can
525          * use this in our C3 policy
526          */
527         cx->valid = 1;
528
529         /*
530          * On older chipsets, BM_RLD needs to be set
531          * in order for Bus Master activity to wake the
532          * system from C3.  Newer chipsets handle DMA
533          * during C3 automatically and BM_RLD is a NOP.
534          * In either case, the proper way to
535          * handle BM_RLD is to set it and leave it set.
536          */
537         acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
538
539         return;
540 }
541
542 static int acpi_cst_latency_cmp(const void *a, const void *b)
543 {
544         const struct acpi_processor_cx *x = a, *y = b;
545
546         if (!(x->valid && y->valid))
547                 return 0;
548         if (x->latency > y->latency)
549                 return 1;
550         if (x->latency < y->latency)
551                 return -1;
552         return 0;
553 }
554 static void acpi_cst_latency_swap(void *a, void *b, int n)
555 {
556         struct acpi_processor_cx *x = a, *y = b;
557         u32 tmp;
558
559         if (!(x->valid && y->valid))
560                 return;
561         tmp = x->latency;
562         x->latency = y->latency;
563         y->latency = tmp;
564 }
565
566 static int acpi_processor_power_verify(struct acpi_processor *pr)
567 {
568         unsigned int i;
569         unsigned int working = 0;
570         unsigned int last_latency = 0;
571         unsigned int last_type = 0;
572         bool buggy_latency = false;
573
574         pr->power.timer_broadcast_on_state = INT_MAX;
575
576         for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
577                 struct acpi_processor_cx *cx = &pr->power.states[i];
578
579                 switch (cx->type) {
580                 case ACPI_STATE_C1:
581                         cx->valid = 1;
582                         break;
583
584                 case ACPI_STATE_C2:
585                         if (!cx->address)
586                                 break;
587                         cx->valid = 1;
588                         break;
589
590                 case ACPI_STATE_C3:
591                         acpi_processor_power_verify_c3(pr, cx);
592                         break;
593                 }
594                 if (!cx->valid)
595                         continue;
596                 if (cx->type >= last_type && cx->latency < last_latency)
597                         buggy_latency = true;
598                 last_latency = cx->latency;
599                 last_type = cx->type;
600
601                 lapic_timer_check_state(i, pr, cx);
602                 tsc_check_state(cx->type);
603                 working++;
604         }
605
606         if (buggy_latency) {
607                 pr_notice("FW issue: working around C-state latencies out of order\n");
608                 sort(&pr->power.states[1], max_cstate,
609                      sizeof(struct acpi_processor_cx),
610                      acpi_cst_latency_cmp,
611                      acpi_cst_latency_swap);
612         }
613
614         lapic_timer_propagate_broadcast(pr);
615
616         return (working);
617 }
618
619 static int acpi_processor_get_cstate_info(struct acpi_processor *pr)
620 {
621         unsigned int i;
622         int result;
623
624
625         /* NOTE: the idle thread may not be running while calling
626          * this function */
627
628         /* Zero initialize all the C-states info. */
629         memset(pr->power.states, 0, sizeof(pr->power.states));
630
631         result = acpi_processor_get_power_info_cst(pr);
632         if (result == -ENODEV)
633                 result = acpi_processor_get_power_info_fadt(pr);
634
635         if (result)
636                 return result;
637
638         acpi_processor_get_power_info_default(pr);
639
640         pr->power.count = acpi_processor_power_verify(pr);
641
642         /*
643          * if one state of type C2 or C3 is available, mark this
644          * CPU as being "idle manageable"
645          */
646         for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
647                 if (pr->power.states[i].valid) {
648                         pr->power.count = i;
649                         if (pr->power.states[i].type >= ACPI_STATE_C2)
650                                 pr->flags.power = 1;
651                 }
652         }
653
654         return 0;
655 }
656
657 /**
658  * acpi_idle_bm_check - checks if bus master activity was detected
659  */
660 static int acpi_idle_bm_check(void)
661 {
662         u32 bm_status = 0;
663
664         if (bm_check_disable)
665                 return 0;
666
667         acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
668         if (bm_status)
669                 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
670         /*
671          * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
672          * the true state of bus mastering activity; forcing us to
673          * manually check the BMIDEA bit of each IDE channel.
674          */
675         else if (errata.piix4.bmisx) {
676                 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
677                     || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
678                         bm_status = 1;
679         }
680         return bm_status;
681 }
682
683 /**
684  * acpi_idle_do_entry - enter idle state using the appropriate method
685  * @cx: cstate data
686  *
687  * Caller disables interrupt before call and enables interrupt after return.
688  */
689 static void __cpuidle acpi_idle_do_entry(struct acpi_processor_cx *cx)
690 {
691         if (cx->entry_method == ACPI_CSTATE_FFH) {
692                 /* Call into architectural FFH based C-state */
693                 acpi_processor_ffh_cstate_enter(cx);
694         } else if (cx->entry_method == ACPI_CSTATE_HALT) {
695                 acpi_safe_halt();
696         } else {
697                 /* IO port based C-state */
698                 inb(cx->address);
699                 /* Dummy wait op - must do something useless after P_LVL2 read
700                    because chipsets cannot guarantee that STPCLK# signal
701                    gets asserted in time to freeze execution properly. */
702                 inl(acpi_gbl_FADT.xpm_timer_block.address);
703         }
704 }
705
706 /**
707  * acpi_idle_play_dead - enters an ACPI state for long-term idle (i.e. off-lining)
708  * @dev: the target CPU
709  * @index: the index of suggested state
710  */
711 static int acpi_idle_play_dead(struct cpuidle_device *dev, int index)
712 {
713         struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
714
715         ACPI_FLUSH_CPU_CACHE();
716
717         while (1) {
718
719                 if (cx->entry_method == ACPI_CSTATE_HALT)
720                         safe_halt();
721                 else if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) {
722                         inb(cx->address);
723                         /* See comment in acpi_idle_do_entry() */
724                         inl(acpi_gbl_FADT.xpm_timer_block.address);
725                 } else
726                         return -ENODEV;
727         }
728
729         /* Never reached */
730         return 0;
731 }
732
733 static bool acpi_idle_fallback_to_c1(struct acpi_processor *pr)
734 {
735         return IS_ENABLED(CONFIG_HOTPLUG_CPU) && !pr->flags.has_cst &&
736                 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED);
737 }
738
739 static int c3_cpu_count;
740 static DEFINE_RAW_SPINLOCK(c3_lock);
741
742 /**
743  * acpi_idle_enter_bm - enters C3 with proper BM handling
744  * @pr: Target processor
745  * @cx: Target state context
746  * @timer_bc: Whether or not to change timer mode to broadcast
747  */
748 static void acpi_idle_enter_bm(struct acpi_processor *pr,
749                                struct acpi_processor_cx *cx, bool timer_bc)
750 {
751         acpi_unlazy_tlb(smp_processor_id());
752
753         /*
754          * Must be done before busmaster disable as we might need to
755          * access HPET !
756          */
757         if (timer_bc)
758                 lapic_timer_state_broadcast(pr, cx, 1);
759
760         /*
761          * disable bus master
762          * bm_check implies we need ARB_DIS
763          * bm_control implies whether we can do ARB_DIS
764          *
765          * That leaves a case where bm_check is set and bm_control is
766          * not set. In that case we cannot do much, we enter C3
767          * without doing anything.
768          */
769         if (pr->flags.bm_control) {
770                 raw_spin_lock(&c3_lock);
771                 c3_cpu_count++;
772                 /* Disable bus master arbitration when all CPUs are in C3 */
773                 if (c3_cpu_count == num_online_cpus())
774                         acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1);
775                 raw_spin_unlock(&c3_lock);
776         }
777
778         acpi_idle_do_entry(cx);
779
780         /* Re-enable bus master arbitration */
781         if (pr->flags.bm_control) {
782                 raw_spin_lock(&c3_lock);
783                 acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0);
784                 c3_cpu_count--;
785                 raw_spin_unlock(&c3_lock);
786         }
787
788         if (timer_bc)
789                 lapic_timer_state_broadcast(pr, cx, 0);
790 }
791
792 static int acpi_idle_enter(struct cpuidle_device *dev,
793                            struct cpuidle_driver *drv, int index)
794 {
795         struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
796         struct acpi_processor *pr;
797
798         pr = __this_cpu_read(processors);
799         if (unlikely(!pr))
800                 return -EINVAL;
801
802         if (cx->type != ACPI_STATE_C1) {
803                 if (acpi_idle_fallback_to_c1(pr) && num_online_cpus() > 1) {
804                         index = CPUIDLE_DRIVER_STATE_START;
805                         cx = per_cpu(acpi_cstate[index], dev->cpu);
806                 } else if (cx->type == ACPI_STATE_C3 && pr->flags.bm_check) {
807                         if (cx->bm_sts_skip || !acpi_idle_bm_check()) {
808                                 acpi_idle_enter_bm(pr, cx, true);
809                                 return index;
810                         } else if (drv->safe_state_index >= 0) {
811                                 index = drv->safe_state_index;
812                                 cx = per_cpu(acpi_cstate[index], dev->cpu);
813                         } else {
814                                 acpi_safe_halt();
815                                 return -EBUSY;
816                         }
817                 }
818         }
819
820         lapic_timer_state_broadcast(pr, cx, 1);
821
822         if (cx->type == ACPI_STATE_C3)
823                 ACPI_FLUSH_CPU_CACHE();
824
825         acpi_idle_do_entry(cx);
826
827         lapic_timer_state_broadcast(pr, cx, 0);
828
829         return index;
830 }
831
832 static void acpi_idle_enter_freeze(struct cpuidle_device *dev,
833                                    struct cpuidle_driver *drv, int index)
834 {
835         struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
836
837         if (cx->type == ACPI_STATE_C3) {
838                 struct acpi_processor *pr = __this_cpu_read(processors);
839
840                 if (unlikely(!pr))
841                         return;
842
843                 if (pr->flags.bm_check) {
844                         acpi_idle_enter_bm(pr, cx, false);
845                         return;
846                 } else {
847                         ACPI_FLUSH_CPU_CACHE();
848                 }
849         }
850         acpi_idle_do_entry(cx);
851 }
852
853 static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
854                                            struct cpuidle_device *dev)
855 {
856         int i, count = CPUIDLE_DRIVER_STATE_START;
857         struct acpi_processor_cx *cx;
858
859         if (max_cstate == 0)
860                 max_cstate = 1;
861
862         for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
863                 cx = &pr->power.states[i];
864
865                 if (!cx->valid)
866                         continue;
867
868                 per_cpu(acpi_cstate[count], dev->cpu) = cx;
869
870                 count++;
871                 if (count == CPUIDLE_STATE_MAX)
872                         break;
873         }
874
875         if (!count)
876                 return -EINVAL;
877
878         return 0;
879 }
880
881 static int acpi_processor_setup_cstates(struct acpi_processor *pr)
882 {
883         int i, count = CPUIDLE_DRIVER_STATE_START;
884         struct acpi_processor_cx *cx;
885         struct cpuidle_state *state;
886         struct cpuidle_driver *drv = &acpi_idle_driver;
887
888         if (max_cstate == 0)
889                 max_cstate = 1;
890
891         for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
892                 cx = &pr->power.states[i];
893
894                 if (!cx->valid)
895                         continue;
896
897                 state = &drv->states[count];
898                 snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
899                 strlcpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
900                 state->exit_latency = cx->latency;
901                 state->target_residency = cx->latency * latency_factor;
902                 state->enter = acpi_idle_enter;
903
904                 state->flags = 0;
905                 if (cx->type == ACPI_STATE_C1 || cx->type == ACPI_STATE_C2) {
906                         state->enter_dead = acpi_idle_play_dead;
907                         drv->safe_state_index = count;
908                 }
909                 /*
910                  * Halt-induced C1 is not good for ->enter_freeze, because it
911                  * re-enables interrupts on exit.  Moreover, C1 is generally not
912                  * particularly interesting from the suspend-to-idle angle, so
913                  * avoid C1 and the situations in which we may need to fall back
914                  * to it altogether.
915                  */
916                 if (cx->type != ACPI_STATE_C1 && !acpi_idle_fallback_to_c1(pr))
917                         state->enter_freeze = acpi_idle_enter_freeze;
918
919                 count++;
920                 if (count == CPUIDLE_STATE_MAX)
921                         break;
922         }
923
924         drv->state_count = count;
925
926         if (!count)
927                 return -EINVAL;
928
929         return 0;
930 }
931
932 static inline void acpi_processor_cstate_first_run_checks(void)
933 {
934         acpi_status status;
935         static int first_run;
936
937         if (first_run)
938                 return;
939         dmi_check_system(processor_power_dmi_table);
940         max_cstate = acpi_processor_cstate_check(max_cstate);
941         if (max_cstate < ACPI_C_STATES_MAX)
942                 pr_notice("ACPI: processor limited to max C-state %d\n",
943                           max_cstate);
944         first_run++;
945
946         if (acpi_gbl_FADT.cst_control && !nocst) {
947                 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
948                                             acpi_gbl_FADT.cst_control, 8);
949                 if (ACPI_FAILURE(status))
950                         ACPI_EXCEPTION((AE_INFO, status,
951                                         "Notifying BIOS of _CST ability failed"));
952         }
953 }
954 #else
955
956 static inline int disabled_by_idle_boot_param(void) { return 0; }
957 static inline void acpi_processor_cstate_first_run_checks(void) { }
958 static int acpi_processor_get_cstate_info(struct acpi_processor *pr)
959 {
960         return -ENODEV;
961 }
962
963 static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
964                                            struct cpuidle_device *dev)
965 {
966         return -EINVAL;
967 }
968
969 static int acpi_processor_setup_cstates(struct acpi_processor *pr)
970 {
971         return -EINVAL;
972 }
973
974 #endif /* CONFIG_ACPI_PROCESSOR_CSTATE */
975
976 struct acpi_lpi_states_array {
977         unsigned int size;
978         unsigned int composite_states_size;
979         struct acpi_lpi_state *entries;
980         struct acpi_lpi_state *composite_states[ACPI_PROCESSOR_MAX_POWER];
981 };
982
983 static int obj_get_integer(union acpi_object *obj, u32 *value)
984 {
985         if (obj->type != ACPI_TYPE_INTEGER)
986                 return -EINVAL;
987
988         *value = obj->integer.value;
989         return 0;
990 }
991
992 static int acpi_processor_evaluate_lpi(acpi_handle handle,
993                                        struct acpi_lpi_states_array *info)
994 {
995         acpi_status status;
996         int ret = 0;
997         int pkg_count, state_idx = 1, loop;
998         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
999         union acpi_object *lpi_data;
1000         struct acpi_lpi_state *lpi_state;
1001
1002         status = acpi_evaluate_object(handle, "_LPI", NULL, &buffer);
1003         if (ACPI_FAILURE(status)) {
1004                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _LPI, giving up\n"));
1005                 return -ENODEV;
1006         }
1007
1008         lpi_data = buffer.pointer;
1009
1010         /* There must be at least 4 elements = 3 elements + 1 package */
1011         if (!lpi_data || lpi_data->type != ACPI_TYPE_PACKAGE ||
1012             lpi_data->package.count < 4) {
1013                 pr_debug("not enough elements in _LPI\n");
1014                 ret = -ENODATA;
1015                 goto end;
1016         }
1017
1018         pkg_count = lpi_data->package.elements[2].integer.value;
1019
1020         /* Validate number of power states. */
1021         if (pkg_count < 1 || pkg_count != lpi_data->package.count - 3) {
1022                 pr_debug("count given by _LPI is not valid\n");
1023                 ret = -ENODATA;
1024                 goto end;
1025         }
1026
1027         lpi_state = kcalloc(pkg_count, sizeof(*lpi_state), GFP_KERNEL);
1028         if (!lpi_state) {
1029                 ret = -ENOMEM;
1030                 goto end;
1031         }
1032
1033         info->size = pkg_count;
1034         info->entries = lpi_state;
1035
1036         /* LPI States start at index 3 */
1037         for (loop = 3; state_idx <= pkg_count; loop++, state_idx++, lpi_state++) {
1038                 union acpi_object *element, *pkg_elem, *obj;
1039
1040                 element = &lpi_data->package.elements[loop];
1041                 if (element->type != ACPI_TYPE_PACKAGE || element->package.count < 7)
1042                         continue;
1043
1044                 pkg_elem = element->package.elements;
1045
1046                 obj = pkg_elem + 6;
1047                 if (obj->type == ACPI_TYPE_BUFFER) {
1048                         struct acpi_power_register *reg;
1049
1050                         reg = (struct acpi_power_register *)obj->buffer.pointer;
1051                         if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
1052                             reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)
1053                                 continue;
1054
1055                         lpi_state->address = reg->address;
1056                         lpi_state->entry_method =
1057                                 reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE ?
1058                                 ACPI_CSTATE_FFH : ACPI_CSTATE_SYSTEMIO;
1059                 } else if (obj->type == ACPI_TYPE_INTEGER) {
1060                         lpi_state->entry_method = ACPI_CSTATE_INTEGER;
1061                         lpi_state->address = obj->integer.value;
1062                 } else {
1063                         continue;
1064                 }
1065
1066                 /* elements[7,8] skipped for now i.e. Residency/Usage counter*/
1067
1068                 obj = pkg_elem + 9;
1069                 if (obj->type == ACPI_TYPE_STRING)
1070                         strlcpy(lpi_state->desc, obj->string.pointer,
1071                                 ACPI_CX_DESC_LEN);
1072
1073                 lpi_state->index = state_idx;
1074                 if (obj_get_integer(pkg_elem + 0, &lpi_state->min_residency)) {
1075                         pr_debug("No min. residency found, assuming 10 us\n");
1076                         lpi_state->min_residency = 10;
1077                 }
1078
1079                 if (obj_get_integer(pkg_elem + 1, &lpi_state->wake_latency)) {
1080                         pr_debug("No wakeup residency found, assuming 10 us\n");
1081                         lpi_state->wake_latency = 10;
1082                 }
1083
1084                 if (obj_get_integer(pkg_elem + 2, &lpi_state->flags))
1085                         lpi_state->flags = 0;
1086
1087                 if (obj_get_integer(pkg_elem + 3, &lpi_state->arch_flags))
1088                         lpi_state->arch_flags = 0;
1089
1090                 if (obj_get_integer(pkg_elem + 4, &lpi_state->res_cnt_freq))
1091                         lpi_state->res_cnt_freq = 1;
1092
1093                 if (obj_get_integer(pkg_elem + 5, &lpi_state->enable_parent_state))
1094                         lpi_state->enable_parent_state = 0;
1095         }
1096
1097         acpi_handle_debug(handle, "Found %d power states\n", state_idx);
1098 end:
1099         kfree(buffer.pointer);
1100         return ret;
1101 }
1102
1103 /*
1104  * flat_state_cnt - the number of composite LPI states after the process of flattening
1105  */
1106 static int flat_state_cnt;
1107
1108 /**
1109  * combine_lpi_states - combine local and parent LPI states to form a composite LPI state
1110  *
1111  * @local: local LPI state
1112  * @parent: parent LPI state
1113  * @result: composite LPI state
1114  */
1115 static bool combine_lpi_states(struct acpi_lpi_state *local,
1116                                struct acpi_lpi_state *parent,
1117                                struct acpi_lpi_state *result)
1118 {
1119         if (parent->entry_method == ACPI_CSTATE_INTEGER) {
1120                 if (!parent->address) /* 0 means autopromotable */
1121                         return false;
1122                 result->address = local->address + parent->address;
1123         } else {
1124                 result->address = parent->address;
1125         }
1126
1127         result->min_residency = max(local->min_residency, parent->min_residency);
1128         result->wake_latency = local->wake_latency + parent->wake_latency;
1129         result->enable_parent_state = parent->enable_parent_state;
1130         result->entry_method = local->entry_method;
1131
1132         result->flags = parent->flags;
1133         result->arch_flags = parent->arch_flags;
1134         result->index = parent->index;
1135
1136         strlcpy(result->desc, local->desc, ACPI_CX_DESC_LEN);
1137         strlcat(result->desc, "+", ACPI_CX_DESC_LEN);
1138         strlcat(result->desc, parent->desc, ACPI_CX_DESC_LEN);
1139         return true;
1140 }
1141
1142 #define ACPI_LPI_STATE_FLAGS_ENABLED                    BIT(0)
1143
1144 static void stash_composite_state(struct acpi_lpi_states_array *curr_level,
1145                                   struct acpi_lpi_state *t)
1146 {
1147         curr_level->composite_states[curr_level->composite_states_size++] = t;
1148 }
1149
1150 static int flatten_lpi_states(struct acpi_processor *pr,
1151                               struct acpi_lpi_states_array *curr_level,
1152                               struct acpi_lpi_states_array *prev_level)
1153 {
1154         int i, j, state_count = curr_level->size;
1155         struct acpi_lpi_state *p, *t = curr_level->entries;
1156
1157         curr_level->composite_states_size = 0;
1158         for (j = 0; j < state_count; j++, t++) {
1159                 struct acpi_lpi_state *flpi;
1160
1161                 if (!(t->flags & ACPI_LPI_STATE_FLAGS_ENABLED))
1162                         continue;
1163
1164                 if (flat_state_cnt >= ACPI_PROCESSOR_MAX_POWER) {
1165                         pr_warn("Limiting number of LPI states to max (%d)\n",
1166                                 ACPI_PROCESSOR_MAX_POWER);
1167                         pr_warn("Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
1168                         break;
1169                 }
1170
1171                 flpi = &pr->power.lpi_states[flat_state_cnt];
1172
1173                 if (!prev_level) { /* leaf/processor node */
1174                         memcpy(flpi, t, sizeof(*t));
1175                         stash_composite_state(curr_level, flpi);
1176                         flat_state_cnt++;
1177                         continue;
1178                 }
1179
1180                 for (i = 0; i < prev_level->composite_states_size; i++) {
1181                         p = prev_level->composite_states[i];
1182                         if (t->index <= p->enable_parent_state &&
1183                             combine_lpi_states(p, t, flpi)) {
1184                                 stash_composite_state(curr_level, flpi);
1185                                 flat_state_cnt++;
1186                                 flpi++;
1187                         }
1188                 }
1189         }
1190
1191         kfree(curr_level->entries);
1192         return 0;
1193 }
1194
1195 static int acpi_processor_get_lpi_info(struct acpi_processor *pr)
1196 {
1197         int ret, i;
1198         acpi_status status;
1199         acpi_handle handle = pr->handle, pr_ahandle;
1200         struct acpi_device *d = NULL;
1201         struct acpi_lpi_states_array info[2], *tmp, *prev, *curr;
1202
1203         if (!osc_pc_lpi_support_confirmed)
1204                 return -EOPNOTSUPP;
1205
1206         if (!acpi_has_method(handle, "_LPI"))
1207                 return -EINVAL;
1208
1209         flat_state_cnt = 0;
1210         prev = &info[0];
1211         curr = &info[1];
1212         handle = pr->handle;
1213         ret = acpi_processor_evaluate_lpi(handle, prev);
1214         if (ret)
1215                 return ret;
1216         flatten_lpi_states(pr, prev, NULL);
1217
1218         status = acpi_get_parent(handle, &pr_ahandle);
1219         while (ACPI_SUCCESS(status)) {
1220                 acpi_bus_get_device(pr_ahandle, &d);
1221                 handle = pr_ahandle;
1222
1223                 if (strcmp(acpi_device_hid(d), ACPI_PROCESSOR_CONTAINER_HID))
1224                         break;
1225
1226                 /* can be optional ? */
1227                 if (!acpi_has_method(handle, "_LPI"))
1228                         break;
1229
1230                 ret = acpi_processor_evaluate_lpi(handle, curr);
1231                 if (ret)
1232                         break;
1233
1234                 /* flatten all the LPI states in this level of hierarchy */
1235                 flatten_lpi_states(pr, curr, prev);
1236
1237                 tmp = prev, prev = curr, curr = tmp;
1238
1239                 status = acpi_get_parent(handle, &pr_ahandle);
1240         }
1241
1242         pr->power.count = flat_state_cnt;
1243         /* reset the index after flattening */
1244         for (i = 0; i < pr->power.count; i++)
1245                 pr->power.lpi_states[i].index = i;
1246
1247         /* Tell driver that _LPI is supported. */
1248         pr->flags.has_lpi = 1;
1249         pr->flags.power = 1;
1250
1251         return 0;
1252 }
1253
1254 int __weak acpi_processor_ffh_lpi_probe(unsigned int cpu)
1255 {
1256         return -ENODEV;
1257 }
1258
1259 int __weak acpi_processor_ffh_lpi_enter(struct acpi_lpi_state *lpi)
1260 {
1261         return -ENODEV;
1262 }
1263
1264 /**
1265  * acpi_idle_lpi_enter - enters an ACPI any LPI state
1266  * @dev: the target CPU
1267  * @drv: cpuidle driver containing cpuidle state info
1268  * @index: index of target state
1269  *
1270  * Return: 0 for success or negative value for error
1271  */
1272 static int acpi_idle_lpi_enter(struct cpuidle_device *dev,
1273                                struct cpuidle_driver *drv, int index)
1274 {
1275         struct acpi_processor *pr;
1276         struct acpi_lpi_state *lpi;
1277
1278         pr = __this_cpu_read(processors);
1279
1280         if (unlikely(!pr))
1281                 return -EINVAL;
1282
1283         lpi = &pr->power.lpi_states[index];
1284         if (lpi->entry_method == ACPI_CSTATE_FFH)
1285                 return acpi_processor_ffh_lpi_enter(lpi);
1286
1287         return -EINVAL;
1288 }
1289
1290 static int acpi_processor_setup_lpi_states(struct acpi_processor *pr)
1291 {
1292         int i;
1293         struct acpi_lpi_state *lpi;
1294         struct cpuidle_state *state;
1295         struct cpuidle_driver *drv = &acpi_idle_driver;
1296
1297         if (!pr->flags.has_lpi)
1298                 return -EOPNOTSUPP;
1299
1300         for (i = 0; i < pr->power.count && i < CPUIDLE_STATE_MAX; i++) {
1301                 lpi = &pr->power.lpi_states[i];
1302
1303                 state = &drv->states[i];
1304                 snprintf(state->name, CPUIDLE_NAME_LEN, "LPI-%d", i);
1305                 strlcpy(state->desc, lpi->desc, CPUIDLE_DESC_LEN);
1306                 state->exit_latency = lpi->wake_latency;
1307                 state->target_residency = lpi->min_residency;
1308                 if (lpi->arch_flags)
1309                         state->flags |= CPUIDLE_FLAG_TIMER_STOP;
1310                 state->enter = acpi_idle_lpi_enter;
1311                 drv->safe_state_index = i;
1312         }
1313
1314         drv->state_count = i;
1315
1316         return 0;
1317 }
1318
1319 /**
1320  * acpi_processor_setup_cpuidle_states- prepares and configures cpuidle
1321  * global state data i.e. idle routines
1322  *
1323  * @pr: the ACPI processor
1324  */
1325 static int acpi_processor_setup_cpuidle_states(struct acpi_processor *pr)
1326 {
1327         int i;
1328         struct cpuidle_driver *drv = &acpi_idle_driver;
1329
1330         if (!pr->flags.power_setup_done || !pr->flags.power)
1331                 return -EINVAL;
1332
1333         drv->safe_state_index = -1;
1334         for (i = CPUIDLE_DRIVER_STATE_START; i < CPUIDLE_STATE_MAX; i++) {
1335                 drv->states[i].name[0] = '\0';
1336                 drv->states[i].desc[0] = '\0';
1337         }
1338
1339         if (pr->flags.has_lpi)
1340                 return acpi_processor_setup_lpi_states(pr);
1341
1342         return acpi_processor_setup_cstates(pr);
1343 }
1344
1345 /**
1346  * acpi_processor_setup_cpuidle_dev - prepares and configures CPUIDLE
1347  * device i.e. per-cpu data
1348  *
1349  * @pr: the ACPI processor
1350  * @dev : the cpuidle device
1351  */
1352 static int acpi_processor_setup_cpuidle_dev(struct acpi_processor *pr,
1353                                             struct cpuidle_device *dev)
1354 {
1355         if (!pr->flags.power_setup_done || !pr->flags.power || !dev)
1356                 return -EINVAL;
1357
1358         dev->cpu = pr->id;
1359         if (pr->flags.has_lpi)
1360                 return acpi_processor_ffh_lpi_probe(pr->id);
1361
1362         return acpi_processor_setup_cpuidle_cx(pr, dev);
1363 }
1364
1365 static int acpi_processor_get_power_info(struct acpi_processor *pr)
1366 {
1367         int ret;
1368
1369         ret = acpi_processor_get_lpi_info(pr);
1370         if (ret)
1371                 ret = acpi_processor_get_cstate_info(pr);
1372
1373         return ret;
1374 }
1375
1376 int acpi_processor_hotplug(struct acpi_processor *pr)
1377 {
1378         int ret = 0;
1379         struct cpuidle_device *dev;
1380
1381         if (disabled_by_idle_boot_param())
1382                 return 0;
1383
1384         if (!pr->flags.power_setup_done)
1385                 return -ENODEV;
1386
1387         dev = per_cpu(acpi_cpuidle_device, pr->id);
1388         cpuidle_pause_and_lock();
1389         cpuidle_disable_device(dev);
1390         ret = acpi_processor_get_power_info(pr);
1391         if (!ret && pr->flags.power) {
1392                 acpi_processor_setup_cpuidle_dev(pr, dev);
1393                 ret = cpuidle_enable_device(dev);
1394         }
1395         cpuidle_resume_and_unlock();
1396
1397         return ret;
1398 }
1399
1400 int acpi_processor_power_state_has_changed(struct acpi_processor *pr)
1401 {
1402         int cpu;
1403         struct acpi_processor *_pr;
1404         struct cpuidle_device *dev;
1405
1406         if (disabled_by_idle_boot_param())
1407                 return 0;
1408
1409         if (!pr->flags.power_setup_done)
1410                 return -ENODEV;
1411
1412         /*
1413          * FIXME:  Design the ACPI notification to make it once per
1414          * system instead of once per-cpu.  This condition is a hack
1415          * to make the code that updates C-States be called once.
1416          */
1417
1418         if (pr->id == 0 && cpuidle_get_driver() == &acpi_idle_driver) {
1419
1420                 /* Protect against cpu-hotplug */
1421                 get_online_cpus();
1422                 cpuidle_pause_and_lock();
1423
1424                 /* Disable all cpuidle devices */
1425                 for_each_online_cpu(cpu) {
1426                         _pr = per_cpu(processors, cpu);
1427                         if (!_pr || !_pr->flags.power_setup_done)
1428                                 continue;
1429                         dev = per_cpu(acpi_cpuidle_device, cpu);
1430                         cpuidle_disable_device(dev);
1431                 }
1432
1433                 /* Populate Updated C-state information */
1434                 acpi_processor_get_power_info(pr);
1435                 acpi_processor_setup_cpuidle_states(pr);
1436
1437                 /* Enable all cpuidle devices */
1438                 for_each_online_cpu(cpu) {
1439                         _pr = per_cpu(processors, cpu);
1440                         if (!_pr || !_pr->flags.power_setup_done)
1441                                 continue;
1442                         acpi_processor_get_power_info(_pr);
1443                         if (_pr->flags.power) {
1444                                 dev = per_cpu(acpi_cpuidle_device, cpu);
1445                                 acpi_processor_setup_cpuidle_dev(_pr, dev);
1446                                 cpuidle_enable_device(dev);
1447                         }
1448                 }
1449                 cpuidle_resume_and_unlock();
1450                 put_online_cpus();
1451         }
1452
1453         return 0;
1454 }
1455
1456 static int acpi_processor_registered;
1457
1458 int acpi_processor_power_init(struct acpi_processor *pr)
1459 {
1460         int retval;
1461         struct cpuidle_device *dev;
1462
1463         if (disabled_by_idle_boot_param())
1464                 return 0;
1465
1466         acpi_processor_cstate_first_run_checks();
1467
1468         if (!acpi_processor_get_power_info(pr))
1469                 pr->flags.power_setup_done = 1;
1470
1471         /*
1472          * Install the idle handler if processor power management is supported.
1473          * Note that we use previously set idle handler will be used on
1474          * platforms that only support C1.
1475          */
1476         if (pr->flags.power) {
1477                 /* Register acpi_idle_driver if not already registered */
1478                 if (!acpi_processor_registered) {
1479                         acpi_processor_setup_cpuidle_states(pr);
1480                         retval = cpuidle_register_driver(&acpi_idle_driver);
1481                         if (retval)
1482                                 return retval;
1483                         pr_debug("%s registered with cpuidle\n",
1484                                  acpi_idle_driver.name);
1485                 }
1486
1487                 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1488                 if (!dev)
1489                         return -ENOMEM;
1490                 per_cpu(acpi_cpuidle_device, pr->id) = dev;
1491
1492                 acpi_processor_setup_cpuidle_dev(pr, dev);
1493
1494                 /* Register per-cpu cpuidle_device. Cpuidle driver
1495                  * must already be registered before registering device
1496                  */
1497                 retval = cpuidle_register_device(dev);
1498                 if (retval) {
1499                         if (acpi_processor_registered == 0)
1500                                 cpuidle_unregister_driver(&acpi_idle_driver);
1501                         return retval;
1502                 }
1503                 acpi_processor_registered++;
1504         }
1505         return 0;
1506 }
1507
1508 int acpi_processor_power_exit(struct acpi_processor *pr)
1509 {
1510         struct cpuidle_device *dev = per_cpu(acpi_cpuidle_device, pr->id);
1511
1512         if (disabled_by_idle_boot_param())
1513                 return 0;
1514
1515         if (pr->flags.power) {
1516                 cpuidle_unregister_device(dev);
1517                 acpi_processor_registered--;
1518                 if (acpi_processor_registered == 0)
1519                         cpuidle_unregister_driver(&acpi_idle_driver);
1520         }
1521
1522         pr->flags.power_setup_done = 0;
1523         return 0;
1524 }