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
28
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/cpufreq.h>
33#include <linux/slab.h>
34
35#ifdef CONFIG_X86
36#include <asm/cpufeature.h>
37#endif
38
39#include <acpi/acpi_bus.h>
40#include <acpi/acpi_drivers.h>
41#include <acpi/processor.h>
42
43#define PREFIX "ACPI: "
44
45#define ACPI_PROCESSOR_CLASS "processor"
46#define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
47#define _COMPONENT ACPI_PROCESSOR_COMPONENT
48ACPI_MODULE_NAME("processor_perflib");
49
50static DEFINE_MUTEX(performance_mutex);
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68static int ignore_ppc = -1;
69module_param(ignore_ppc, int, 0644);
70MODULE_PARM_DESC(ignore_ppc, "If the frequency of your machine gets wrongly" \
71 "limited by BIOS, this should help");
72
73#define PPC_REGISTERED 1
74#define PPC_IN_USE 2
75
76static int acpi_processor_ppc_status;
77
78static int acpi_processor_ppc_notifier(struct notifier_block *nb,
79 unsigned long event, void *data)
80{
81 struct cpufreq_policy *policy = data;
82 struct acpi_processor *pr;
83 unsigned int ppc = 0;
84
85 if (event == CPUFREQ_START && ignore_ppc <= 0) {
86 ignore_ppc = 0;
87 return 0;
88 }
89
90 if (ignore_ppc)
91 return 0;
92
93 if (event != CPUFREQ_INCOMPATIBLE)
94 return 0;
95
96 mutex_lock(&performance_mutex);
97
98 pr = per_cpu(processors, policy->cpu);
99 if (!pr || !pr->performance)
100 goto out;
101
102 ppc = (unsigned int)pr->performance_platform_limit;
103
104 if (ppc >= pr->performance->state_count)
105 goto out;
106
107 cpufreq_verify_within_limits(policy, 0,
108 pr->performance->states[ppc].
109 core_frequency * 1000);
110
111 out:
112 mutex_unlock(&performance_mutex);
113
114 return 0;
115}
116
117static struct notifier_block acpi_ppc_notifier_block = {
118 .notifier_call = acpi_processor_ppc_notifier,
119};
120
121static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
122{
123 acpi_status status = 0;
124 unsigned long long ppc = 0;
125
126
127 if (!pr)
128 return -EINVAL;
129
130
131
132
133
134 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
135
136 if (status != AE_NOT_FOUND)
137 acpi_processor_ppc_status |= PPC_IN_USE;
138
139 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
140 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
141 return -ENODEV;
142 }
143
144 pr_debug("CPU %d: _PPC is %d - frequency %s limited\n", pr->id,
145 (int)ppc, ppc ? "" : "not");
146
147 pr->performance_platform_limit = (int)ppc;
148
149 return 0;
150}
151
152#define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
153
154
155
156
157
158
159
160static void acpi_processor_ppc_ost(acpi_handle handle, int status)
161{
162 if (acpi_has_method(handle, "_OST"))
163 acpi_evaluate_ost(handle, ACPI_PROCESSOR_NOTIFY_PERFORMANCE,
164 status, NULL);
165}
166
167int acpi_processor_ppc_has_changed(struct acpi_processor *pr, int event_flag)
168{
169 int ret;
170
171 if (ignore_ppc) {
172
173
174
175
176 if (event_flag)
177 acpi_processor_ppc_ost(pr->handle, 1);
178 return 0;
179 }
180
181 ret = acpi_processor_get_platform_limit(pr);
182
183
184
185
186 if (event_flag) {
187 if (ret < 0)
188 acpi_processor_ppc_ost(pr->handle, 1);
189 else
190 acpi_processor_ppc_ost(pr->handle, 0);
191 }
192 if (ret < 0)
193 return (ret);
194 else
195 return cpufreq_update_policy(pr->id);
196}
197
198int acpi_processor_get_bios_limit(int cpu, unsigned int *limit)
199{
200 struct acpi_processor *pr;
201
202 pr = per_cpu(processors, cpu);
203 if (!pr || !pr->performance || !pr->performance->state_count)
204 return -ENODEV;
205 *limit = pr->performance->states[pr->performance_platform_limit].
206 core_frequency * 1000;
207 return 0;
208}
209EXPORT_SYMBOL(acpi_processor_get_bios_limit);
210
211void acpi_processor_ppc_init(void)
212{
213 if (!cpufreq_register_notifier
214 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
215 acpi_processor_ppc_status |= PPC_REGISTERED;
216 else
217 printk(KERN_DEBUG
218 "Warning: Processor Platform Limit not supported.\n");
219}
220
221void acpi_processor_ppc_exit(void)
222{
223 if (acpi_processor_ppc_status & PPC_REGISTERED)
224 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
225 CPUFREQ_POLICY_NOTIFIER);
226
227 acpi_processor_ppc_status &= ~PPC_REGISTERED;
228}
229
230
231
232
233
234
235void acpi_processor_load_module(struct acpi_processor *pr)
236{
237 static int requested;
238 acpi_status status = 0;
239 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
240
241 if (!arch_has_acpi_pdc() || requested)
242 return;
243 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
244 if (!ACPI_FAILURE(status)) {
245 printk(KERN_INFO PREFIX "Requesting acpi_cpufreq\n");
246 request_module_nowait("acpi_cpufreq");
247 requested = 1;
248 }
249 kfree(buffer.pointer);
250}
251
252static int acpi_processor_get_performance_control(struct acpi_processor *pr)
253{
254 int result = 0;
255 acpi_status status = 0;
256 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
257 union acpi_object *pct = NULL;
258 union acpi_object obj = { 0 };
259
260
261 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
262 if (ACPI_FAILURE(status)) {
263 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
264 return -ENODEV;
265 }
266
267 pct = (union acpi_object *)buffer.pointer;
268 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
269 || (pct->package.count != 2)) {
270 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
271 result = -EFAULT;
272 goto end;
273 }
274
275
276
277
278
279 obj = pct->package.elements[0];
280
281 if ((obj.type != ACPI_TYPE_BUFFER)
282 || (obj.buffer.length < sizeof(struct acpi_pct_register))
283 || (obj.buffer.pointer == NULL)) {
284 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
285 result = -EFAULT;
286 goto end;
287 }
288 memcpy(&pr->performance->control_register, obj.buffer.pointer,
289 sizeof(struct acpi_pct_register));
290
291
292
293
294
295 obj = pct->package.elements[1];
296
297 if ((obj.type != ACPI_TYPE_BUFFER)
298 || (obj.buffer.length < sizeof(struct acpi_pct_register))
299 || (obj.buffer.pointer == NULL)) {
300 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
301 result = -EFAULT;
302 goto end;
303 }
304
305 memcpy(&pr->performance->status_register, obj.buffer.pointer,
306 sizeof(struct acpi_pct_register));
307
308 end:
309 kfree(buffer.pointer);
310
311 return result;
312}
313
314#ifdef CONFIG_X86
315
316
317
318
319static void amd_fixup_frequency(struct acpi_processor_px *px, int i)
320{
321 u32 hi, lo, fid, did;
322 int index = px->control & 0x00000007;
323
324 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
325 return;
326
327 if ((boot_cpu_data.x86 == 0x10 && boot_cpu_data.x86_model < 10)
328 || boot_cpu_data.x86 == 0x11) {
329 rdmsr(MSR_AMD_PSTATE_DEF_BASE + index, lo, hi);
330
331
332
333
334 if (!(hi & BIT(31)))
335 return;
336
337 fid = lo & 0x3f;
338 did = (lo >> 6) & 7;
339 if (boot_cpu_data.x86 == 0x10)
340 px->core_frequency = (100 * (fid + 0x10)) >> did;
341 else
342 px->core_frequency = (100 * (fid + 8)) >> did;
343 }
344}
345#else
346static void amd_fixup_frequency(struct acpi_processor_px *px, int i) {};
347#endif
348
349static int acpi_processor_get_performance_states(struct acpi_processor *pr)
350{
351 int result = 0;
352 acpi_status status = AE_OK;
353 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
354 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
355 struct acpi_buffer state = { 0, NULL };
356 union acpi_object *pss = NULL;
357 int i;
358 int last_invalid = -1;
359
360
361 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
362 if (ACPI_FAILURE(status)) {
363 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
364 return -ENODEV;
365 }
366
367 pss = buffer.pointer;
368 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
369 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
370 result = -EFAULT;
371 goto end;
372 }
373
374 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
375 pss->package.count));
376
377 pr->performance->state_count = pss->package.count;
378 pr->performance->states =
379 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
380 GFP_KERNEL);
381 if (!pr->performance->states) {
382 result = -ENOMEM;
383 goto end;
384 }
385
386 for (i = 0; i < pr->performance->state_count; i++) {
387
388 struct acpi_processor_px *px = &(pr->performance->states[i]);
389
390 state.length = sizeof(struct acpi_processor_px);
391 state.pointer = px;
392
393 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
394
395 status = acpi_extract_package(&(pss->package.elements[i]),
396 &format, &state);
397 if (ACPI_FAILURE(status)) {
398 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
399 result = -EFAULT;
400 kfree(pr->performance->states);
401 goto end;
402 }
403
404 amd_fixup_frequency(px, i);
405
406 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
407 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
408 i,
409 (u32) px->core_frequency,
410 (u32) px->power,
411 (u32) px->transition_latency,
412 (u32) px->bus_master_latency,
413 (u32) px->control, (u32) px->status));
414
415
416
417
418 if (!px->core_frequency ||
419 ((u32)(px->core_frequency * 1000) !=
420 (px->core_frequency * 1000))) {
421 printk(KERN_ERR FW_BUG PREFIX
422 "Invalid BIOS _PSS frequency found for processor %d: 0x%llx MHz\n",
423 pr->id, px->core_frequency);
424 if (last_invalid == -1)
425 last_invalid = i;
426 } else {
427 if (last_invalid != -1) {
428
429
430
431 memcpy(&(pr->performance->states[last_invalid]),
432 px, sizeof(struct acpi_processor_px));
433 ++last_invalid;
434 }
435 }
436 }
437
438 if (last_invalid == 0) {
439 printk(KERN_ERR FW_BUG PREFIX
440 "No valid BIOS _PSS frequency found for processor %d\n", pr->id);
441 result = -EFAULT;
442 kfree(pr->performance->states);
443 pr->performance->states = NULL;
444 }
445
446 if (last_invalid > 0)
447 pr->performance->state_count = last_invalid;
448
449 end:
450 kfree(buffer.pointer);
451
452 return result;
453}
454
455int acpi_processor_get_performance_info(struct acpi_processor *pr)
456{
457 int result = 0;
458
459 if (!pr || !pr->performance || !pr->handle)
460 return -EINVAL;
461
462 if (!acpi_has_method(pr->handle, "_PCT")) {
463 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
464 "ACPI-based processor performance control unavailable\n"));
465 return -ENODEV;
466 }
467
468 result = acpi_processor_get_performance_control(pr);
469 if (result)
470 goto update_bios;
471
472 result = acpi_processor_get_performance_states(pr);
473 if (result)
474 goto update_bios;
475
476
477 if (ignore_ppc != 1)
478 result = acpi_processor_get_platform_limit(pr);
479
480 return result;
481
482
483
484
485
486 update_bios:
487#ifdef CONFIG_X86
488 if (acpi_has_method(pr->handle, "_PPC")) {
489 if(boot_cpu_has(X86_FEATURE_EST))
490 printk(KERN_WARNING FW_BUG "BIOS needs update for CPU "
491 "frequency support\n");
492 }
493#endif
494 return result;
495}
496EXPORT_SYMBOL_GPL(acpi_processor_get_performance_info);
497
498int acpi_processor_pstate_control(void)
499{
500 acpi_status status;
501
502 if (!acpi_gbl_FADT.smi_command || !acpi_gbl_FADT.pstate_control)
503 return 0;
504
505 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
506 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
507 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
508
509 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
510 (u32)acpi_gbl_FADT.pstate_control, 8);
511 if (ACPI_SUCCESS(status))
512 return 1;
513
514 ACPI_EXCEPTION((AE_INFO, status,
515 "Failed to write pstate_control [0x%x] to smi_command [0x%x]",
516 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
517 return -EIO;
518}
519
520int acpi_processor_notify_smm(struct module *calling_module)
521{
522 static int is_done = 0;
523 int result;
524
525 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
526 return -EBUSY;
527
528 if (!try_module_get(calling_module))
529 return -EINVAL;
530
531
532
533
534
535
536 if (is_done > 0) {
537 module_put(calling_module);
538 return 0;
539 } else if (is_done < 0) {
540 module_put(calling_module);
541 return is_done;
542 }
543
544 is_done = -EIO;
545
546 result = acpi_processor_pstate_control();
547 if (!result) {
548 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
549 module_put(calling_module);
550 return 0;
551 }
552 if (result < 0) {
553 module_put(calling_module);
554 return result;
555 }
556
557
558
559 is_done = 1;
560
561 if (!(acpi_processor_ppc_status & PPC_IN_USE))
562 module_put(calling_module);
563
564 return 0;
565}
566
567EXPORT_SYMBOL(acpi_processor_notify_smm);
568
569static int acpi_processor_get_psd(struct acpi_processor *pr)
570{
571 int result = 0;
572 acpi_status status = AE_OK;
573 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
574 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
575 struct acpi_buffer state = {0, NULL};
576 union acpi_object *psd = NULL;
577 struct acpi_psd_package *pdomain;
578
579 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
580 if (ACPI_FAILURE(status)) {
581 return -ENODEV;
582 }
583
584 psd = buffer.pointer;
585 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
586 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
587 result = -EFAULT;
588 goto end;
589 }
590
591 if (psd->package.count != 1) {
592 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
593 result = -EFAULT;
594 goto end;
595 }
596
597 pdomain = &(pr->performance->domain_info);
598
599 state.length = sizeof(struct acpi_psd_package);
600 state.pointer = pdomain;
601
602 status = acpi_extract_package(&(psd->package.elements[0]),
603 &format, &state);
604 if (ACPI_FAILURE(status)) {
605 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
606 result = -EFAULT;
607 goto end;
608 }
609
610 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
611 printk(KERN_ERR PREFIX "Unknown _PSD:num_entries\n");
612 result = -EFAULT;
613 goto end;
614 }
615
616 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
617 printk(KERN_ERR PREFIX "Unknown _PSD:revision\n");
618 result = -EFAULT;
619 goto end;
620 }
621
622 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
623 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
624 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
625 printk(KERN_ERR PREFIX "Invalid _PSD:coord_type\n");
626 result = -EFAULT;
627 goto end;
628 }
629end:
630 kfree(buffer.pointer);
631 return result;
632}
633
634int acpi_processor_preregister_performance(
635 struct acpi_processor_performance __percpu *performance)
636{
637 int count_target;
638 int retval = 0;
639 unsigned int i, j;
640 cpumask_var_t covered_cpus;
641 struct acpi_processor *pr;
642 struct acpi_psd_package *pdomain;
643 struct acpi_processor *match_pr;
644 struct acpi_psd_package *match_pdomain;
645
646 if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
647 return -ENOMEM;
648
649 mutex_lock(&performance_mutex);
650
651
652
653
654
655 for_each_possible_cpu(i) {
656 pr = per_cpu(processors, i);
657 if (!pr) {
658
659 continue;
660 }
661
662 if (pr->performance) {
663 retval = -EBUSY;
664 goto err_out;
665 }
666
667 if (!performance || !per_cpu_ptr(performance, i)) {
668 retval = -EINVAL;
669 goto err_out;
670 }
671 }
672
673
674 for_each_possible_cpu(i) {
675 pr = per_cpu(processors, i);
676 if (!pr)
677 continue;
678
679 pr->performance = per_cpu_ptr(performance, i);
680 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
681 if (acpi_processor_get_psd(pr)) {
682 retval = -EINVAL;
683 continue;
684 }
685 }
686 if (retval)
687 goto err_ret;
688
689
690
691
692
693 for_each_possible_cpu(i) {
694 pr = per_cpu(processors, i);
695 if (!pr)
696 continue;
697
698 if (cpumask_test_cpu(i, covered_cpus))
699 continue;
700
701 pdomain = &(pr->performance->domain_info);
702 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
703 cpumask_set_cpu(i, covered_cpus);
704 if (pdomain->num_processors <= 1)
705 continue;
706
707
708 count_target = pdomain->num_processors;
709 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
710 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
711 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
712 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
713 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
714 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
715
716 for_each_possible_cpu(j) {
717 if (i == j)
718 continue;
719
720 match_pr = per_cpu(processors, j);
721 if (!match_pr)
722 continue;
723
724 match_pdomain = &(match_pr->performance->domain_info);
725 if (match_pdomain->domain != pdomain->domain)
726 continue;
727
728
729
730 if (match_pdomain->num_processors != count_target) {
731 retval = -EINVAL;
732 goto err_ret;
733 }
734
735 if (pdomain->coord_type != match_pdomain->coord_type) {
736 retval = -EINVAL;
737 goto err_ret;
738 }
739
740 cpumask_set_cpu(j, covered_cpus);
741 cpumask_set_cpu(j, pr->performance->shared_cpu_map);
742 }
743
744 for_each_possible_cpu(j) {
745 if (i == j)
746 continue;
747
748 match_pr = per_cpu(processors, j);
749 if (!match_pr)
750 continue;
751
752 match_pdomain = &(match_pr->performance->domain_info);
753 if (match_pdomain->domain != pdomain->domain)
754 continue;
755
756 match_pr->performance->shared_type =
757 pr->performance->shared_type;
758 cpumask_copy(match_pr->performance->shared_cpu_map,
759 pr->performance->shared_cpu_map);
760 }
761 }
762
763err_ret:
764 for_each_possible_cpu(i) {
765 pr = per_cpu(processors, i);
766 if (!pr || !pr->performance)
767 continue;
768
769
770 if (retval) {
771 cpumask_clear(pr->performance->shared_cpu_map);
772 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
773 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
774 }
775 pr->performance = NULL;
776 }
777
778err_out:
779 mutex_unlock(&performance_mutex);
780 free_cpumask_var(covered_cpus);
781 return retval;
782}
783EXPORT_SYMBOL(acpi_processor_preregister_performance);
784
785int
786acpi_processor_register_performance(struct acpi_processor_performance
787 *performance, unsigned int cpu)
788{
789 struct acpi_processor *pr;
790
791 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
792 return -EINVAL;
793
794 mutex_lock(&performance_mutex);
795
796 pr = per_cpu(processors, cpu);
797 if (!pr) {
798 mutex_unlock(&performance_mutex);
799 return -ENODEV;
800 }
801
802 if (pr->performance) {
803 mutex_unlock(&performance_mutex);
804 return -EBUSY;
805 }
806
807 WARN_ON(!performance);
808
809 pr->performance = performance;
810
811 if (acpi_processor_get_performance_info(pr)) {
812 pr->performance = NULL;
813 mutex_unlock(&performance_mutex);
814 return -EIO;
815 }
816
817 mutex_unlock(&performance_mutex);
818 return 0;
819}
820
821EXPORT_SYMBOL(acpi_processor_register_performance);
822
823void
824acpi_processor_unregister_performance(struct acpi_processor_performance
825 *performance, unsigned int cpu)
826{
827 struct acpi_processor *pr;
828
829 mutex_lock(&performance_mutex);
830
831 pr = per_cpu(processors, cpu);
832 if (!pr) {
833 mutex_unlock(&performance_mutex);
834 return;
835 }
836
837 if (pr->performance)
838 kfree(pr->performance->states);
839 pr->performance = NULL;
840
841 mutex_unlock(&performance_mutex);
842
843 return;
844}
845
846EXPORT_SYMBOL(acpi_processor_unregister_performance);
847