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