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