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