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