1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/cpumask.h>
21#include <linux/cpufreq.h>
22#include <linux/freezer.h>
23#include <linux/kernel.h>
24#include <linux/kthread.h>
25#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/types.h>
28#include <linux/syscore_ops.h>
29#include <acpi/acpi_bus.h>
30#include <acpi/acpi_drivers.h>
31#include <acpi/processor.h>
32
33#include <xen/xen.h>
34#include <xen/interface/platform.h>
35#include <asm/xen/hypercall.h>
36
37#define DRV_NAME "xen-acpi-processor: "
38
39static int no_hypercall;
40MODULE_PARM_DESC(off, "Inhibit the hypercall.");
41module_param_named(off, no_hypercall, int, 0400);
42
43
44
45
46
47
48
49static unsigned int nr_acpi_bits;
50
51static DEFINE_MUTEX(acpi_ids_mutex);
52
53static unsigned long *acpi_ids_done;
54
55static unsigned long *acpi_id_present;
56
57static unsigned long *acpi_id_cst_present;
58
59static int push_cxx_to_hypervisor(struct acpi_processor *_pr)
60{
61 struct xen_platform_op op = {
62 .cmd = XENPF_set_processor_pminfo,
63 .interface_version = XENPF_INTERFACE_VERSION,
64 .u.set_pminfo.id = _pr->acpi_id,
65 .u.set_pminfo.type = XEN_PM_CX,
66 };
67 struct xen_processor_cx *dst_cx, *dst_cx_states = NULL;
68 struct acpi_processor_cx *cx;
69 unsigned int i, ok;
70 int ret = 0;
71
72 dst_cx_states = kcalloc(_pr->power.count,
73 sizeof(struct xen_processor_cx), GFP_KERNEL);
74 if (!dst_cx_states)
75 return -ENOMEM;
76
77 for (ok = 0, i = 1; i <= _pr->power.count; i++) {
78 cx = &_pr->power.states[i];
79 if (!cx->valid)
80 continue;
81
82 dst_cx = &(dst_cx_states[ok++]);
83
84 dst_cx->reg.space_id = ACPI_ADR_SPACE_SYSTEM_IO;
85 if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) {
86 dst_cx->reg.bit_width = 8;
87 dst_cx->reg.bit_offset = 0;
88 dst_cx->reg.access_size = 1;
89 } else {
90 dst_cx->reg.space_id = ACPI_ADR_SPACE_FIXED_HARDWARE;
91 if (cx->entry_method == ACPI_CSTATE_FFH) {
92
93 dst_cx->reg.bit_offset = 2;
94 dst_cx->reg.bit_width = 1;
95 }
96 dst_cx->reg.access_size = 0;
97 }
98 dst_cx->reg.address = cx->address;
99
100 dst_cx->type = cx->type;
101 dst_cx->latency = cx->latency;
102
103 dst_cx->dpcnt = 0;
104 set_xen_guest_handle(dst_cx->dp, NULL);
105 }
106 if (!ok) {
107 pr_debug(DRV_NAME "No _Cx for ACPI CPU %u\n", _pr->acpi_id);
108 kfree(dst_cx_states);
109 return -EINVAL;
110 }
111 op.u.set_pminfo.power.count = ok;
112 op.u.set_pminfo.power.flags.bm_control = _pr->flags.bm_control;
113 op.u.set_pminfo.power.flags.bm_check = _pr->flags.bm_check;
114 op.u.set_pminfo.power.flags.has_cst = _pr->flags.has_cst;
115 op.u.set_pminfo.power.flags.power_setup_done =
116 _pr->flags.power_setup_done;
117
118 set_xen_guest_handle(op.u.set_pminfo.power.states, dst_cx_states);
119
120 if (!no_hypercall)
121 ret = HYPERVISOR_dom0_op(&op);
122
123 if (!ret) {
124 pr_debug("ACPI CPU%u - C-states uploaded.\n", _pr->acpi_id);
125 for (i = 1; i <= _pr->power.count; i++) {
126 cx = &_pr->power.states[i];
127 if (!cx->valid)
128 continue;
129 pr_debug(" C%d: %s %d uS\n",
130 cx->type, cx->desc, (u32)cx->latency);
131 }
132 } else if (ret != -EINVAL)
133
134
135
136 pr_err(DRV_NAME "(CX): Hypervisor error (%d) for ACPI CPU%u\n",
137 ret, _pr->acpi_id);
138
139 kfree(dst_cx_states);
140
141 return ret;
142}
143static struct xen_processor_px *
144xen_copy_pss_data(struct acpi_processor *_pr,
145 struct xen_processor_performance *dst_perf)
146{
147 struct xen_processor_px *dst_states = NULL;
148 unsigned int i;
149
150 BUILD_BUG_ON(sizeof(struct xen_processor_px) !=
151 sizeof(struct acpi_processor_px));
152
153 dst_states = kcalloc(_pr->performance->state_count,
154 sizeof(struct xen_processor_px), GFP_KERNEL);
155 if (!dst_states)
156 return ERR_PTR(-ENOMEM);
157
158 dst_perf->state_count = _pr->performance->state_count;
159 for (i = 0; i < _pr->performance->state_count; i++) {
160
161 memcpy(&(dst_states[i]), &(_pr->performance->states[i]),
162 sizeof(struct acpi_processor_px));
163 }
164 return dst_states;
165}
166static int xen_copy_psd_data(struct acpi_processor *_pr,
167 struct xen_processor_performance *dst)
168{
169 struct acpi_psd_package *pdomain;
170
171 BUILD_BUG_ON(sizeof(struct xen_psd_package) !=
172 sizeof(struct acpi_psd_package));
173
174
175
176
177 dst->shared_type = _pr->performance->shared_type;
178
179 pdomain = &(_pr->performance->domain_info);
180
181
182
183
184 if (pdomain->num_processors <= 1) {
185 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
186 dst->shared_type = CPUFREQ_SHARED_TYPE_ALL;
187 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
188 dst->shared_type = CPUFREQ_SHARED_TYPE_HW;
189 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
190 dst->shared_type = CPUFREQ_SHARED_TYPE_ANY;
191
192 }
193 memcpy(&(dst->domain_info), pdomain, sizeof(struct acpi_psd_package));
194 return 0;
195}
196static int xen_copy_pct_data(struct acpi_pct_register *pct,
197 struct xen_pct_register *dst_pct)
198{
199
200
201
202
203 dst_pct->descriptor = pct->descriptor;
204 dst_pct->length = pct->length;
205 dst_pct->space_id = pct->space_id;
206 dst_pct->bit_width = pct->bit_width;
207 dst_pct->bit_offset = pct->bit_offset;
208 dst_pct->reserved = pct->reserved;
209 dst_pct->address = pct->address;
210 return 0;
211}
212static int push_pxx_to_hypervisor(struct acpi_processor *_pr)
213{
214 int ret = 0;
215 struct xen_platform_op op = {
216 .cmd = XENPF_set_processor_pminfo,
217 .interface_version = XENPF_INTERFACE_VERSION,
218 .u.set_pminfo.id = _pr->acpi_id,
219 .u.set_pminfo.type = XEN_PM_PX,
220 };
221 struct xen_processor_performance *dst_perf;
222 struct xen_processor_px *dst_states = NULL;
223
224 dst_perf = &op.u.set_pminfo.perf;
225
226 dst_perf->platform_limit = _pr->performance_platform_limit;
227 dst_perf->flags |= XEN_PX_PPC;
228 xen_copy_pct_data(&(_pr->performance->control_register),
229 &dst_perf->control_register);
230 xen_copy_pct_data(&(_pr->performance->status_register),
231 &dst_perf->status_register);
232 dst_perf->flags |= XEN_PX_PCT;
233 dst_states = xen_copy_pss_data(_pr, dst_perf);
234 if (!IS_ERR_OR_NULL(dst_states)) {
235 set_xen_guest_handle(dst_perf->states, dst_states);
236 dst_perf->flags |= XEN_PX_PSS;
237 }
238 if (!xen_copy_psd_data(_pr, dst_perf))
239 dst_perf->flags |= XEN_PX_PSD;
240
241 if (dst_perf->flags != (XEN_PX_PSD | XEN_PX_PSS | XEN_PX_PCT | XEN_PX_PPC)) {
242 pr_warn(DRV_NAME "ACPI CPU%u missing some P-state data (%x), skipping.\n",
243 _pr->acpi_id, dst_perf->flags);
244 ret = -ENODEV;
245 goto err_free;
246 }
247
248 if (!no_hypercall)
249 ret = HYPERVISOR_dom0_op(&op);
250
251 if (!ret) {
252 struct acpi_processor_performance *perf;
253 unsigned int i;
254
255 perf = _pr->performance;
256 pr_debug("ACPI CPU%u - P-states uploaded.\n", _pr->acpi_id);
257 for (i = 0; i < perf->state_count; i++) {
258 pr_debug(" %cP%d: %d MHz, %d mW, %d uS\n",
259 (i == perf->state ? '*' : ' '), i,
260 (u32) perf->states[i].core_frequency,
261 (u32) perf->states[i].power,
262 (u32) perf->states[i].transition_latency);
263 }
264 } else if (ret != -EINVAL)
265
266
267
268 pr_warn(DRV_NAME "(_PXX): Hypervisor error (%d) for ACPI CPU%u\n",
269 ret, _pr->acpi_id);
270err_free:
271 if (!IS_ERR_OR_NULL(dst_states))
272 kfree(dst_states);
273
274 return ret;
275}
276static int upload_pm_data(struct acpi_processor *_pr)
277{
278 int err = 0;
279
280 mutex_lock(&acpi_ids_mutex);
281 if (__test_and_set_bit(_pr->acpi_id, acpi_ids_done)) {
282 mutex_unlock(&acpi_ids_mutex);
283 return -EBUSY;
284 }
285 if (_pr->flags.power)
286 err = push_cxx_to_hypervisor(_pr);
287
288 if (_pr->performance && _pr->performance->states)
289 err |= push_pxx_to_hypervisor(_pr);
290
291 mutex_unlock(&acpi_ids_mutex);
292 return err;
293}
294static unsigned int __init get_max_acpi_id(void)
295{
296 struct xenpf_pcpuinfo *info;
297 struct xen_platform_op op = {
298 .cmd = XENPF_get_cpuinfo,
299 .interface_version = XENPF_INTERFACE_VERSION,
300 };
301 int ret = 0;
302 unsigned int i, last_cpu, max_acpi_id = 0;
303
304 info = &op.u.pcpu_info;
305 info->xen_cpuid = 0;
306
307 ret = HYPERVISOR_dom0_op(&op);
308 if (ret)
309 return NR_CPUS;
310
311
312 last_cpu = op.u.pcpu_info.max_present;
313 for (i = 0; i <= last_cpu; i++) {
314 info->xen_cpuid = i;
315 ret = HYPERVISOR_dom0_op(&op);
316 if (ret)
317 continue;
318 max_acpi_id = max(info->acpi_id, max_acpi_id);
319 }
320 max_acpi_id *= 2;
321 pr_debug(DRV_NAME "Max ACPI ID: %u\n", max_acpi_id);
322 return max_acpi_id;
323}
324
325
326
327
328
329
330
331
332
333static acpi_status
334read_acpi_id(acpi_handle handle, u32 lvl, void *context, void **rv)
335{
336 u32 acpi_id;
337 acpi_status status;
338 acpi_object_type acpi_type;
339 unsigned long long tmp;
340 union acpi_object object = { 0 };
341 struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
342 acpi_io_address pblk = 0;
343
344 status = acpi_get_type(handle, &acpi_type);
345 if (ACPI_FAILURE(status))
346 return AE_OK;
347
348 switch (acpi_type) {
349 case ACPI_TYPE_PROCESSOR:
350 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
351 if (ACPI_FAILURE(status))
352 return AE_OK;
353 acpi_id = object.processor.proc_id;
354 pblk = object.processor.pblk_address;
355 break;
356 case ACPI_TYPE_DEVICE:
357 status = acpi_evaluate_integer(handle, "_UID", NULL, &tmp);
358 if (ACPI_FAILURE(status))
359 return AE_OK;
360 acpi_id = tmp;
361 break;
362 default:
363 return AE_OK;
364 }
365
366
367 if (acpi_id > nr_acpi_bits) {
368 pr_debug(DRV_NAME "We only have %u, trying to set %u\n",
369 nr_acpi_bits, acpi_id);
370 return AE_OK;
371 }
372
373 __set_bit(acpi_id, acpi_id_present);
374
375 pr_debug(DRV_NAME "ACPI CPU%u w/ PBLK:0x%lx\n", acpi_id,
376 (unsigned long)pblk);
377
378 status = acpi_evaluate_object(handle, "_CST", NULL, &buffer);
379 if (ACPI_FAILURE(status)) {
380 if (!pblk)
381 return AE_OK;
382 }
383
384 __set_bit(acpi_id, acpi_id_cst_present);
385
386 return AE_OK;
387}
388static int check_acpi_ids(struct acpi_processor *pr_backup)
389{
390
391 if (!pr_backup)
392 return -ENODEV;
393
394 if (acpi_id_present && acpi_id_cst_present)
395
396 goto upload;
397
398
399
400
401 acpi_id_present = kcalloc(BITS_TO_LONGS(nr_acpi_bits), sizeof(unsigned long), GFP_KERNEL);
402 if (!acpi_id_present)
403 return -ENOMEM;
404
405 acpi_id_cst_present = kcalloc(BITS_TO_LONGS(nr_acpi_bits), sizeof(unsigned long), GFP_KERNEL);
406 if (!acpi_id_cst_present) {
407 kfree(acpi_id_present);
408 return -ENOMEM;
409 }
410
411 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
412 ACPI_UINT32_MAX,
413 read_acpi_id, NULL, NULL, NULL);
414 acpi_get_devices("ACPI0007", read_acpi_id, NULL, NULL);
415
416upload:
417 if (!bitmap_equal(acpi_id_present, acpi_ids_done, nr_acpi_bits)) {
418 unsigned int i;
419 for_each_set_bit(i, acpi_id_present, nr_acpi_bits) {
420 pr_backup->acpi_id = i;
421
422 pr_backup->flags.power = test_bit(i, acpi_id_cst_present);
423 (void)upload_pm_data(pr_backup);
424 }
425 }
426
427 return 0;
428}
429static int __init check_prereq(void)
430{
431 struct cpuinfo_x86 *c = &cpu_data(0);
432
433 if (!xen_initial_domain())
434 return -ENODEV;
435
436 if (!acpi_gbl_FADT.smi_command)
437 return -ENODEV;
438
439 if (c->x86_vendor == X86_VENDOR_INTEL) {
440 if (!cpu_has(c, X86_FEATURE_EST))
441 return -ENODEV;
442
443 return 0;
444 }
445 if (c->x86_vendor == X86_VENDOR_AMD) {
446
447
448
449#define CPUID_FREQ_VOLT_CAPABILITIES 0x80000007
450#define USE_HW_PSTATE 0x00000080
451 u32 eax, ebx, ecx, edx;
452 cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
453 if ((edx & USE_HW_PSTATE) != USE_HW_PSTATE)
454 return -ENODEV;
455 return 0;
456 }
457 return -ENODEV;
458}
459
460static struct acpi_processor_performance __percpu *acpi_perf_data;
461
462static void free_acpi_perf_data(void)
463{
464 unsigned int i;
465
466
467 for_each_possible_cpu(i)
468 free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
469 ->shared_cpu_map);
470 free_percpu(acpi_perf_data);
471}
472
473static int xen_upload_processor_pm_data(void)
474{
475 struct acpi_processor *pr_backup = NULL;
476 unsigned int i;
477 int rc = 0;
478
479 pr_info(DRV_NAME "Uploading Xen processor PM info\n");
480
481 for_each_possible_cpu(i) {
482 struct acpi_processor *_pr;
483 _pr = per_cpu(processors, i );
484 if (!_pr)
485 continue;
486
487 if (!pr_backup) {
488 pr_backup = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
489 if (pr_backup)
490 memcpy(pr_backup, _pr, sizeof(struct acpi_processor));
491 }
492 (void)upload_pm_data(_pr);
493 }
494
495 rc = check_acpi_ids(pr_backup);
496 kfree(pr_backup);
497
498 return rc;
499}
500
501static void xen_acpi_processor_resume(void)
502{
503 bitmap_zero(acpi_ids_done, nr_acpi_bits);
504 xen_upload_processor_pm_data();
505}
506
507static struct syscore_ops xap_syscore_ops = {
508 .resume = xen_acpi_processor_resume,
509};
510
511static int __init xen_acpi_processor_init(void)
512{
513 unsigned int i;
514 int rc = check_prereq();
515
516 if (rc)
517 return rc;
518
519 nr_acpi_bits = get_max_acpi_id() + 1;
520 acpi_ids_done = kcalloc(BITS_TO_LONGS(nr_acpi_bits), sizeof(unsigned long), GFP_KERNEL);
521 if (!acpi_ids_done)
522 return -ENOMEM;
523
524 acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
525 if (!acpi_perf_data) {
526 pr_debug(DRV_NAME "Memory allocation error for acpi_perf_data.\n");
527 kfree(acpi_ids_done);
528 return -ENOMEM;
529 }
530 for_each_possible_cpu(i) {
531 if (!zalloc_cpumask_var_node(
532 &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
533 GFP_KERNEL, cpu_to_node(i))) {
534 rc = -ENOMEM;
535 goto err_out;
536 }
537 }
538
539
540 (void)acpi_processor_preregister_performance(acpi_perf_data);
541
542 for_each_possible_cpu(i) {
543 struct acpi_processor *pr;
544 struct acpi_processor_performance *perf;
545
546 pr = per_cpu(processors, i);
547 perf = per_cpu_ptr(acpi_perf_data, i);
548 if (!pr)
549 continue;
550
551 pr->performance = perf;
552 rc = acpi_processor_get_performance_info(pr);
553 if (rc)
554 goto err_out;
555 }
556
557 rc = xen_upload_processor_pm_data();
558 if (rc)
559 goto err_unregister;
560
561 register_syscore_ops(&xap_syscore_ops);
562
563 return 0;
564err_unregister:
565 for_each_possible_cpu(i) {
566 struct acpi_processor_performance *perf;
567 perf = per_cpu_ptr(acpi_perf_data, i);
568 acpi_processor_unregister_performance(perf, i);
569 }
570err_out:
571
572 free_acpi_perf_data();
573 kfree(acpi_ids_done);
574 return rc;
575}
576static void __exit xen_acpi_processor_exit(void)
577{
578 int i;
579
580 unregister_syscore_ops(&xap_syscore_ops);
581 kfree(acpi_ids_done);
582 kfree(acpi_id_present);
583 kfree(acpi_id_cst_present);
584 for_each_possible_cpu(i) {
585 struct acpi_processor_performance *perf;
586 perf = per_cpu_ptr(acpi_perf_data, i);
587 acpi_processor_unregister_performance(perf, i);
588 }
589 free_acpi_perf_data();
590}
591
592MODULE_AUTHOR("Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>");
593MODULE_DESCRIPTION("Xen ACPI Processor P-states (and Cx) driver which uploads PM data to Xen hypervisor");
594MODULE_LICENSE("GPL");
595
596
597
598device_initcall(xen_acpi_processor_init);
599module_exit(xen_acpi_processor_exit);
600