1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/module.h>
28#include <linux/acpi.h>
29#include <linux/dmi.h>
30#include <linux/sched.h>
31#include <linux/tick.h>
32#include <linux/cpuidle.h>
33#include <linux/syscore_ops.h>
34#include <acpi/processor.h>
35
36
37
38
39
40
41
42#ifdef CONFIG_X86
43#include <asm/apic.h>
44#endif
45
46#define PREFIX "ACPI: "
47
48#define ACPI_PROCESSOR_CLASS "processor"
49#define _COMPONENT ACPI_PROCESSOR_COMPONENT
50ACPI_MODULE_NAME("processor_idle");
51
52static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
53module_param(max_cstate, uint, 0000);
54static unsigned int nocst __read_mostly;
55module_param(nocst, uint, 0000);
56static int bm_check_disable __read_mostly;
57module_param(bm_check_disable, uint, 0000);
58
59static unsigned int latency_factor __read_mostly = 2;
60module_param(latency_factor, uint, 0644);
61
62static DEFINE_PER_CPU(struct cpuidle_device *, acpi_cpuidle_device);
63
64static DEFINE_PER_CPU(struct acpi_processor_cx * [CPUIDLE_STATE_MAX],
65 acpi_cstate);
66
67static int disabled_by_idle_boot_param(void)
68{
69 return boot_option_idle_override == IDLE_POLL ||
70 boot_option_idle_override == IDLE_HALT;
71}
72
73
74
75
76
77
78
79static int set_max_cstate(const struct dmi_system_id *id)
80{
81 if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
82 return 0;
83
84 printk(KERN_NOTICE PREFIX "%s detected - limiting to C%ld max_cstate."
85 " Override with \"processor.max_cstate=%d\"\n", id->ident,
86 (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
87
88 max_cstate = (long)id->driver_data;
89
90 return 0;
91}
92
93static const struct dmi_system_id processor_power_dmi_table[] = {
94 { set_max_cstate, "Clevo 5600D", {
95 DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
96 DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
97 (void *)2},
98 { set_max_cstate, "Pavilion zv5000", {
99 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
100 DMI_MATCH(DMI_PRODUCT_NAME,"Pavilion zv5000 (DS502A#ABA)")},
101 (void *)1},
102 { set_max_cstate, "Asus L8400B", {
103 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
104 DMI_MATCH(DMI_PRODUCT_NAME,"L8400B series Notebook PC")},
105 (void *)1},
106 {},
107};
108
109
110
111
112
113
114static void acpi_safe_halt(void)
115{
116 if (!tif_need_resched()) {
117 safe_halt();
118 local_irq_disable();
119 }
120}
121
122#ifdef ARCH_APICTIMER_STOPS_ON_C3
123
124
125
126
127
128
129
130static void lapic_timer_check_state(int state, struct acpi_processor *pr,
131 struct acpi_processor_cx *cx)
132{
133 struct acpi_processor_power *pwr = &pr->power;
134 u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
135
136 if (cpu_has(&cpu_data(pr->id), X86_FEATURE_ARAT))
137 return;
138
139 if (amd_e400_c1e_detected)
140 type = ACPI_STATE_C1;
141
142
143
144
145
146 if (pwr->timer_broadcast_on_state < state)
147 return;
148
149 if (cx->type >= type)
150 pr->power.timer_broadcast_on_state = state;
151}
152
153static void __lapic_timer_propagate_broadcast(void *arg)
154{
155 struct acpi_processor *pr = (struct acpi_processor *) arg;
156
157 if (pr->power.timer_broadcast_on_state < INT_MAX)
158 tick_broadcast_enable();
159 else
160 tick_broadcast_disable();
161}
162
163static void lapic_timer_propagate_broadcast(struct acpi_processor *pr)
164{
165 smp_call_function_single(pr->id, __lapic_timer_propagate_broadcast,
166 (void *)pr, 1);
167}
168
169
170static void lapic_timer_state_broadcast(struct acpi_processor *pr,
171 struct acpi_processor_cx *cx,
172 int broadcast)
173{
174 int state = cx - pr->power.states;
175
176 if (state >= pr->power.timer_broadcast_on_state) {
177 if (broadcast)
178 tick_broadcast_enter();
179 else
180 tick_broadcast_exit();
181 }
182}
183
184#else
185
186static void lapic_timer_check_state(int state, struct acpi_processor *pr,
187 struct acpi_processor_cx *cstate) { }
188static void lapic_timer_propagate_broadcast(struct acpi_processor *pr) { }
189static void lapic_timer_state_broadcast(struct acpi_processor *pr,
190 struct acpi_processor_cx *cx,
191 int broadcast)
192{
193}
194
195#endif
196
197#ifdef CONFIG_PM_SLEEP
198static u32 saved_bm_rld;
199
200static int acpi_processor_suspend(void)
201{
202 acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &saved_bm_rld);
203 return 0;
204}
205
206static void acpi_processor_resume(void)
207{
208 u32 resumed_bm_rld = 0;
209
210 acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &resumed_bm_rld);
211 if (resumed_bm_rld == saved_bm_rld)
212 return;
213
214 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, saved_bm_rld);
215}
216
217static struct syscore_ops acpi_processor_syscore_ops = {
218 .suspend = acpi_processor_suspend,
219 .resume = acpi_processor_resume,
220};
221
222void acpi_processor_syscore_init(void)
223{
224 register_syscore_ops(&acpi_processor_syscore_ops);
225}
226
227void acpi_processor_syscore_exit(void)
228{
229 unregister_syscore_ops(&acpi_processor_syscore_ops);
230}
231#endif
232
233#if defined(CONFIG_X86)
234static void tsc_check_state(int state)
235{
236 switch (boot_cpu_data.x86_vendor) {
237 case X86_VENDOR_AMD:
238 case X86_VENDOR_INTEL:
239
240
241
242
243 if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
244 return;
245
246
247 default:
248
249 if (state > ACPI_STATE_C1)
250 mark_tsc_unstable("TSC halts in idle");
251 }
252}
253#else
254static void tsc_check_state(int state) { return; }
255#endif
256
257static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
258{
259
260 if (!pr->pblk)
261 return -ENODEV;
262
263
264 pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
265 pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
266
267#ifndef CONFIG_HOTPLUG_CPU
268
269
270
271
272 if ((num_online_cpus() > 1) &&
273 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
274 return -ENODEV;
275#endif
276
277
278 pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
279 pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
280
281
282 pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.c2_latency;
283 pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.c3_latency;
284
285
286
287
288
289 if (acpi_gbl_FADT.c2_latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
290 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
291 "C2 latency too large [%d]\n", acpi_gbl_FADT.c2_latency));
292
293 pr->power.states[ACPI_STATE_C2].address = 0;
294 }
295
296
297
298
299
300 if (acpi_gbl_FADT.c3_latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
301 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
302 "C3 latency too large [%d]\n", acpi_gbl_FADT.c3_latency));
303
304 pr->power.states[ACPI_STATE_C3].address = 0;
305 }
306
307 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
308 "lvl2[0x%08x] lvl3[0x%08x]\n",
309 pr->power.states[ACPI_STATE_C2].address,
310 pr->power.states[ACPI_STATE_C3].address));
311
312 return 0;
313}
314
315static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
316{
317 if (!pr->power.states[ACPI_STATE_C1].valid) {
318
319
320 pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
321 pr->power.states[ACPI_STATE_C1].valid = 1;
322 pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
323 }
324
325 pr->power.states[ACPI_STATE_C0].valid = 1;
326 return 0;
327}
328
329static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
330{
331 acpi_status status;
332 u64 count;
333 int current_count;
334 int i, ret = 0;
335 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
336 union acpi_object *cst;
337
338
339 if (nocst)
340 return -ENODEV;
341
342 current_count = 0;
343
344 status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
345 if (ACPI_FAILURE(status)) {
346 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
347 return -ENODEV;
348 }
349
350 cst = buffer.pointer;
351
352
353 if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
354 printk(KERN_ERR PREFIX "not enough elements in _CST\n");
355 ret = -EFAULT;
356 goto end;
357 }
358
359 count = cst->package.elements[0].integer.value;
360
361
362 if (count < 1 || count != cst->package.count - 1) {
363 printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
364 ret = -EFAULT;
365 goto end;
366 }
367
368
369 pr->flags.has_cst = 1;
370
371 for (i = 1; i <= count; i++) {
372 union acpi_object *element;
373 union acpi_object *obj;
374 struct acpi_power_register *reg;
375 struct acpi_processor_cx cx;
376
377 memset(&cx, 0, sizeof(cx));
378
379 element = &(cst->package.elements[i]);
380 if (element->type != ACPI_TYPE_PACKAGE)
381 continue;
382
383 if (element->package.count != 4)
384 continue;
385
386 obj = &(element->package.elements[0]);
387
388 if (obj->type != ACPI_TYPE_BUFFER)
389 continue;
390
391 reg = (struct acpi_power_register *)obj->buffer.pointer;
392
393 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
394 (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
395 continue;
396
397
398 obj = &(element->package.elements[1]);
399 if (obj->type != ACPI_TYPE_INTEGER)
400 continue;
401
402 cx.type = obj->integer.value;
403
404
405
406
407 if (i == 1 && cx.type != ACPI_STATE_C1)
408 current_count++;
409
410 cx.address = reg->address;
411 cx.index = current_count + 1;
412
413 cx.entry_method = ACPI_CSTATE_SYSTEMIO;
414 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
415 if (acpi_processor_ffh_cstate_probe
416 (pr->id, &cx, reg) == 0) {
417 cx.entry_method = ACPI_CSTATE_FFH;
418 } else if (cx.type == ACPI_STATE_C1) {
419
420
421
422
423
424
425 cx.entry_method = ACPI_CSTATE_HALT;
426 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
427 } else {
428 continue;
429 }
430 if (cx.type == ACPI_STATE_C1 &&
431 (boot_option_idle_override == IDLE_NOMWAIT)) {
432
433
434
435
436
437
438
439
440
441
442 cx.entry_method = ACPI_CSTATE_HALT;
443 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
444 }
445 } else {
446 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
447 cx.address);
448 }
449
450 if (cx.type == ACPI_STATE_C1) {
451 cx.valid = 1;
452 }
453
454 obj = &(element->package.elements[2]);
455 if (obj->type != ACPI_TYPE_INTEGER)
456 continue;
457
458 cx.latency = obj->integer.value;
459
460 obj = &(element->package.elements[3]);
461 if (obj->type != ACPI_TYPE_INTEGER)
462 continue;
463
464 current_count++;
465 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
466
467
468
469
470
471 if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
472 printk(KERN_WARNING
473 "Limiting number of power states to max (%d)\n",
474 ACPI_PROCESSOR_MAX_POWER);
475 printk(KERN_WARNING
476 "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
477 break;
478 }
479 }
480
481 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
482 current_count));
483
484
485 if (current_count < 2)
486 ret = -EFAULT;
487
488 end:
489 kfree(buffer.pointer);
490
491 return ret;
492}
493
494static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
495 struct acpi_processor_cx *cx)
496{
497 static int bm_check_flag = -1;
498 static int bm_control_flag = -1;
499
500
501 if (!cx->address)
502 return;
503
504
505
506
507
508
509
510
511 else if (errata.piix4.fdma) {
512 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
513 "C3 not supported on PIIX4 with Type-F DMA\n"));
514 return;
515 }
516
517
518 if (bm_check_flag == -1) {
519
520 acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
521 bm_check_flag = pr->flags.bm_check;
522 bm_control_flag = pr->flags.bm_control;
523 } else {
524 pr->flags.bm_check = bm_check_flag;
525 pr->flags.bm_control = bm_control_flag;
526 }
527
528 if (pr->flags.bm_check) {
529 if (!pr->flags.bm_control) {
530 if (pr->flags.has_cst != 1) {
531
532 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
533 "C3 support requires BM control\n"));
534 return;
535 } else {
536
537 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
538 "C3 support without BM control\n"));
539 }
540 }
541 } else {
542
543
544
545
546 if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
547 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
548 "Cache invalidation should work properly"
549 " for C3 to be enabled on SMP systems\n"));
550 return;
551 }
552 }
553
554
555
556
557
558
559
560 cx->valid = 1;
561
562
563
564
565
566
567
568
569
570 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
571
572 return;
573}
574
575static int acpi_processor_power_verify(struct acpi_processor *pr)
576{
577 unsigned int i;
578 unsigned int working = 0;
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
603 lapic_timer_check_state(i, pr, cx);
604 tsc_check_state(cx->type);
605 working++;
606 }
607
608 lapic_timer_propagate_broadcast(pr);
609
610 return (working);
611}
612
613static int acpi_processor_get_power_info(struct acpi_processor *pr)
614{
615 unsigned int i;
616 int result;
617
618
619
620
621
622
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
638
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
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
666
667
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
679
680
681
682
683static void acpi_idle_do_entry(struct acpi_processor_cx *cx)
684{
685 if (cx->entry_method == ACPI_CSTATE_FFH) {
686
687 acpi_processor_ffh_cstate_enter(cx);
688 } else if (cx->entry_method == ACPI_CSTATE_HALT) {
689 acpi_safe_halt();
690 } else {
691
692 inb(cx->address);
693
694
695
696 inl(acpi_gbl_FADT.xpm_timer_block.address);
697 }
698}
699
700
701
702
703
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
718 inl(acpi_gbl_FADT.xpm_timer_block.address);
719 } else
720 return -ENODEV;
721 }
722
723
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
738
739
740
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
749
750
751 if (timer_bc)
752 lapic_timer_state_broadcast(pr, cx, 1);
753
754
755
756
757
758
759
760
761
762
763 if (pr->flags.bm_control) {
764 raw_spin_lock(&c3_lock);
765 c3_cpu_count++;
766
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
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 = CPUIDLE_DRIVER_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 void acpi_idle_enter_freeze(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;
836
837 if (pr->flags.bm_check) {
838 acpi_idle_enter_bm(pr, cx, false);
839 return;
840 } else {
841 ACPI_FLUSH_CPU_CACHE();
842 }
843 }
844 acpi_idle_do_entry(cx);
845}
846
847struct cpuidle_driver acpi_idle_driver = {
848 .name = "acpi_idle",
849 .owner = THIS_MODULE,
850};
851
852
853
854
855
856
857
858
859static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
860 struct cpuidle_device *dev)
861{
862 int i, count = CPUIDLE_DRIVER_STATE_START;
863 struct acpi_processor_cx *cx;
864
865 if (!pr->flags.power_setup_done)
866 return -EINVAL;
867
868 if (pr->flags.power == 0) {
869 return -EINVAL;
870 }
871
872 if (!dev)
873 return -EINVAL;
874
875 dev->cpu = pr->id;
876
877 if (max_cstate == 0)
878 max_cstate = 1;
879
880 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
881 cx = &pr->power.states[i];
882
883 if (!cx->valid)
884 continue;
885
886 per_cpu(acpi_cstate[count], dev->cpu) = cx;
887
888 count++;
889 if (count == CPUIDLE_STATE_MAX)
890 break;
891 }
892
893 if (!count)
894 return -EINVAL;
895
896 return 0;
897}
898
899
900
901
902
903
904
905static int acpi_processor_setup_cpuidle_states(struct acpi_processor *pr)
906{
907 int i, count = CPUIDLE_DRIVER_STATE_START;
908 struct acpi_processor_cx *cx;
909 struct cpuidle_state *state;
910 struct cpuidle_driver *drv = &acpi_idle_driver;
911
912 if (!pr->flags.power_setup_done)
913 return -EINVAL;
914
915 if (pr->flags.power == 0)
916 return -EINVAL;
917
918 drv->safe_state_index = -1;
919 for (i = CPUIDLE_DRIVER_STATE_START; i < CPUIDLE_STATE_MAX; i++) {
920 drv->states[i].name[0] = '\0';
921 drv->states[i].desc[0] = '\0';
922 }
923
924 if (max_cstate == 0)
925 max_cstate = 1;
926
927 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
928 cx = &pr->power.states[i];
929
930 if (!cx->valid)
931 continue;
932
933 state = &drv->states[count];
934 snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
935 strncpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
936 state->exit_latency = cx->latency;
937 state->target_residency = cx->latency * latency_factor;
938 state->enter = acpi_idle_enter;
939
940 state->flags = 0;
941 if (cx->type == ACPI_STATE_C1 || cx->type == ACPI_STATE_C2) {
942 state->enter_dead = acpi_idle_play_dead;
943 drv->safe_state_index = count;
944 }
945
946
947
948
949
950
951
952 if (cx->type != ACPI_STATE_C1 && !acpi_idle_fallback_to_c1(pr))
953 state->enter_freeze = acpi_idle_enter_freeze;
954
955 count++;
956 if (count == CPUIDLE_STATE_MAX)
957 break;
958 }
959
960 drv->state_count = count;
961
962 if (!count)
963 return -EINVAL;
964
965 return 0;
966}
967
968int acpi_processor_hotplug(struct acpi_processor *pr)
969{
970 int ret = 0;
971 struct cpuidle_device *dev;
972
973 if (disabled_by_idle_boot_param())
974 return 0;
975
976 if (nocst)
977 return -ENODEV;
978
979 if (!pr->flags.power_setup_done)
980 return -ENODEV;
981
982 dev = per_cpu(acpi_cpuidle_device, pr->id);
983 cpuidle_pause_and_lock();
984 cpuidle_disable_device(dev);
985 acpi_processor_get_power_info(pr);
986 if (pr->flags.power) {
987 acpi_processor_setup_cpuidle_cx(pr, dev);
988 ret = cpuidle_enable_device(dev);
989 }
990 cpuidle_resume_and_unlock();
991
992 return ret;
993}
994
995int acpi_processor_cst_has_changed(struct acpi_processor *pr)
996{
997 int cpu;
998 struct acpi_processor *_pr;
999 struct cpuidle_device *dev;
1000
1001 if (disabled_by_idle_boot_param())
1002 return 0;
1003
1004 if (nocst)
1005 return -ENODEV;
1006
1007 if (!pr->flags.power_setup_done)
1008 return -ENODEV;
1009
1010
1011
1012
1013
1014
1015
1016 if (pr->id == 0 && cpuidle_get_driver() == &acpi_idle_driver) {
1017
1018
1019 get_online_cpus();
1020 cpuidle_pause_and_lock();
1021
1022
1023 for_each_online_cpu(cpu) {
1024 _pr = per_cpu(processors, cpu);
1025 if (!_pr || !_pr->flags.power_setup_done)
1026 continue;
1027 dev = per_cpu(acpi_cpuidle_device, cpu);
1028 cpuidle_disable_device(dev);
1029 }
1030
1031
1032 acpi_processor_get_power_info(pr);
1033 acpi_processor_setup_cpuidle_states(pr);
1034
1035
1036 for_each_online_cpu(cpu) {
1037 _pr = per_cpu(processors, cpu);
1038 if (!_pr || !_pr->flags.power_setup_done)
1039 continue;
1040 acpi_processor_get_power_info(_pr);
1041 if (_pr->flags.power) {
1042 dev = per_cpu(acpi_cpuidle_device, cpu);
1043 acpi_processor_setup_cpuidle_cx(_pr, dev);
1044 cpuidle_enable_device(dev);
1045 }
1046 }
1047 cpuidle_resume_and_unlock();
1048 put_online_cpus();
1049 }
1050
1051 return 0;
1052}
1053
1054static int acpi_processor_registered;
1055
1056int acpi_processor_power_init(struct acpi_processor *pr)
1057{
1058 acpi_status status;
1059 int retval;
1060 struct cpuidle_device *dev;
1061 static int first_run;
1062
1063 if (disabled_by_idle_boot_param())
1064 return 0;
1065
1066 if (!first_run) {
1067 dmi_check_system(processor_power_dmi_table);
1068 max_cstate = acpi_processor_cstate_check(max_cstate);
1069 if (max_cstate < ACPI_C_STATES_MAX)
1070 printk(KERN_NOTICE
1071 "ACPI: processor limited to max C-state %d\n",
1072 max_cstate);
1073 first_run++;
1074 }
1075
1076 if (acpi_gbl_FADT.cst_control && !nocst) {
1077 status =
1078 acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
1079 if (ACPI_FAILURE(status)) {
1080 ACPI_EXCEPTION((AE_INFO, status,
1081 "Notifying BIOS of _CST ability failed"));
1082 }
1083 }
1084
1085 acpi_processor_get_power_info(pr);
1086 pr->flags.power_setup_done = 1;
1087
1088
1089
1090
1091
1092
1093 if (pr->flags.power) {
1094
1095 if (!acpi_processor_registered) {
1096 acpi_processor_setup_cpuidle_states(pr);
1097 retval = cpuidle_register_driver(&acpi_idle_driver);
1098 if (retval)
1099 return retval;
1100 printk(KERN_DEBUG "ACPI: %s registered with cpuidle\n",
1101 acpi_idle_driver.name);
1102 }
1103
1104 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1105 if (!dev)
1106 return -ENOMEM;
1107 per_cpu(acpi_cpuidle_device, pr->id) = dev;
1108
1109 acpi_processor_setup_cpuidle_cx(pr, dev);
1110
1111
1112
1113
1114 retval = cpuidle_register_device(dev);
1115 if (retval) {
1116 if (acpi_processor_registered == 0)
1117 cpuidle_unregister_driver(&acpi_idle_driver);
1118 return retval;
1119 }
1120 acpi_processor_registered++;
1121 }
1122 return 0;
1123}
1124
1125int acpi_processor_power_exit(struct acpi_processor *pr)
1126{
1127 struct cpuidle_device *dev = per_cpu(acpi_cpuidle_device, pr->id);
1128
1129 if (disabled_by_idle_boot_param())
1130 return 0;
1131
1132 if (pr->flags.power) {
1133 cpuidle_unregister_device(dev);
1134 acpi_processor_registered--;
1135 if (acpi_processor_registered == 0)
1136 cpuidle_unregister_driver(&acpi_idle_driver);
1137 }
1138
1139 pr->flags.power_setup_done = 0;
1140 return 0;
1141}
1142