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