0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0013
0014 #include <linux/cpumask.h>
0015 #include <linux/cpufreq.h>
0016 #include <linux/freezer.h>
0017 #include <linux/kernel.h>
0018 #include <linux/kthread.h>
0019 #include <linux/init.h>
0020 #include <linux/module.h>
0021 #include <linux/types.h>
0022 #include <linux/syscore_ops.h>
0023 #include <linux/acpi.h>
0024 #include <acpi/processor.h>
0025 #include <xen/xen.h>
0026 #include <xen/interface/platform.h>
0027 #include <asm/xen/hypercall.h>
0028
0029 static int no_hypercall;
0030 MODULE_PARM_DESC(off, "Inhibit the hypercall.");
0031 module_param_named(off, no_hypercall, int, 0400);
0032
0033
0034
0035
0036
0037
0038
0039 static unsigned int nr_acpi_bits;
0040
0041 static DEFINE_MUTEX(acpi_ids_mutex);
0042
0043 static unsigned long *acpi_ids_done;
0044
0045 static unsigned long *acpi_id_present;
0046
0047 static unsigned long *acpi_id_cst_present;
0048
0049 static struct acpi_psd_package *acpi_psd;
0050
0051 static int push_cxx_to_hypervisor(struct acpi_processor *_pr)
0052 {
0053 struct xen_platform_op op = {
0054 .cmd = XENPF_set_processor_pminfo,
0055 .interface_version = XENPF_INTERFACE_VERSION,
0056 .u.set_pminfo.id = _pr->acpi_id,
0057 .u.set_pminfo.type = XEN_PM_CX,
0058 };
0059 struct xen_processor_cx *dst_cx, *dst_cx_states = NULL;
0060 struct acpi_processor_cx *cx;
0061 unsigned int i, ok;
0062 int ret = 0;
0063
0064 dst_cx_states = kcalloc(_pr->power.count,
0065 sizeof(struct xen_processor_cx), GFP_KERNEL);
0066 if (!dst_cx_states)
0067 return -ENOMEM;
0068
0069 for (ok = 0, i = 1; i <= _pr->power.count; i++) {
0070 cx = &_pr->power.states[i];
0071 if (!cx->valid)
0072 continue;
0073
0074 dst_cx = &(dst_cx_states[ok++]);
0075
0076 dst_cx->reg.space_id = ACPI_ADR_SPACE_SYSTEM_IO;
0077 if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) {
0078 dst_cx->reg.bit_width = 8;
0079 dst_cx->reg.bit_offset = 0;
0080 dst_cx->reg.access_size = 1;
0081 } else {
0082 dst_cx->reg.space_id = ACPI_ADR_SPACE_FIXED_HARDWARE;
0083 if (cx->entry_method == ACPI_CSTATE_FFH) {
0084
0085 dst_cx->reg.bit_offset = 2;
0086 dst_cx->reg.bit_width = 1;
0087 }
0088 dst_cx->reg.access_size = 0;
0089 }
0090 dst_cx->reg.address = cx->address;
0091
0092 dst_cx->type = cx->type;
0093 dst_cx->latency = cx->latency;
0094
0095 dst_cx->dpcnt = 0;
0096 set_xen_guest_handle(dst_cx->dp, NULL);
0097 }
0098 if (!ok) {
0099 pr_debug("No _Cx for ACPI CPU %u\n", _pr->acpi_id);
0100 kfree(dst_cx_states);
0101 return -EINVAL;
0102 }
0103 op.u.set_pminfo.power.count = ok;
0104 op.u.set_pminfo.power.flags.bm_control = _pr->flags.bm_control;
0105 op.u.set_pminfo.power.flags.bm_check = _pr->flags.bm_check;
0106 op.u.set_pminfo.power.flags.has_cst = _pr->flags.has_cst;
0107 op.u.set_pminfo.power.flags.power_setup_done =
0108 _pr->flags.power_setup_done;
0109
0110 set_xen_guest_handle(op.u.set_pminfo.power.states, dst_cx_states);
0111
0112 if (!no_hypercall)
0113 ret = HYPERVISOR_platform_op(&op);
0114
0115 if (!ret) {
0116 pr_debug("ACPI CPU%u - C-states uploaded.\n", _pr->acpi_id);
0117 for (i = 1; i <= _pr->power.count; i++) {
0118 cx = &_pr->power.states[i];
0119 if (!cx->valid)
0120 continue;
0121 pr_debug(" C%d: %s %d uS\n",
0122 cx->type, cx->desc, (u32)cx->latency);
0123 }
0124 } else if ((ret != -EINVAL) && (ret != -ENOSYS))
0125
0126
0127
0128 pr_err("(CX): Hypervisor error (%d) for ACPI CPU%u\n",
0129 ret, _pr->acpi_id);
0130
0131 kfree(dst_cx_states);
0132
0133 return ret;
0134 }
0135 static struct xen_processor_px *
0136 xen_copy_pss_data(struct acpi_processor *_pr,
0137 struct xen_processor_performance *dst_perf)
0138 {
0139 struct xen_processor_px *dst_states = NULL;
0140 unsigned int i;
0141
0142 BUILD_BUG_ON(sizeof(struct xen_processor_px) !=
0143 sizeof(struct acpi_processor_px));
0144
0145 dst_states = kcalloc(_pr->performance->state_count,
0146 sizeof(struct xen_processor_px), GFP_KERNEL);
0147 if (!dst_states)
0148 return ERR_PTR(-ENOMEM);
0149
0150 dst_perf->state_count = _pr->performance->state_count;
0151 for (i = 0; i < _pr->performance->state_count; i++) {
0152
0153 memcpy(&(dst_states[i]), &(_pr->performance->states[i]),
0154 sizeof(struct acpi_processor_px));
0155 }
0156 return dst_states;
0157 }
0158 static int xen_copy_psd_data(struct acpi_processor *_pr,
0159 struct xen_processor_performance *dst)
0160 {
0161 struct acpi_psd_package *pdomain;
0162
0163 BUILD_BUG_ON(sizeof(struct xen_psd_package) !=
0164 sizeof(struct acpi_psd_package));
0165
0166
0167
0168
0169 dst->shared_type = _pr->performance->shared_type;
0170
0171 pdomain = &(_pr->performance->domain_info);
0172
0173
0174
0175
0176 if (pdomain->num_processors <= 1) {
0177 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
0178 dst->shared_type = CPUFREQ_SHARED_TYPE_ALL;
0179 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
0180 dst->shared_type = CPUFREQ_SHARED_TYPE_HW;
0181 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
0182 dst->shared_type = CPUFREQ_SHARED_TYPE_ANY;
0183
0184 }
0185 memcpy(&(dst->domain_info), pdomain, sizeof(struct acpi_psd_package));
0186 return 0;
0187 }
0188 static int xen_copy_pct_data(struct acpi_pct_register *pct,
0189 struct xen_pct_register *dst_pct)
0190 {
0191
0192
0193
0194
0195 dst_pct->descriptor = pct->descriptor;
0196 dst_pct->length = pct->length;
0197 dst_pct->space_id = pct->space_id;
0198 dst_pct->bit_width = pct->bit_width;
0199 dst_pct->bit_offset = pct->bit_offset;
0200 dst_pct->reserved = pct->reserved;
0201 dst_pct->address = pct->address;
0202 return 0;
0203 }
0204 static int push_pxx_to_hypervisor(struct acpi_processor *_pr)
0205 {
0206 int ret = 0;
0207 struct xen_platform_op op = {
0208 .cmd = XENPF_set_processor_pminfo,
0209 .interface_version = XENPF_INTERFACE_VERSION,
0210 .u.set_pminfo.id = _pr->acpi_id,
0211 .u.set_pminfo.type = XEN_PM_PX,
0212 };
0213 struct xen_processor_performance *dst_perf;
0214 struct xen_processor_px *dst_states = NULL;
0215
0216 dst_perf = &op.u.set_pminfo.perf;
0217
0218 dst_perf->platform_limit = _pr->performance_platform_limit;
0219 dst_perf->flags |= XEN_PX_PPC;
0220 xen_copy_pct_data(&(_pr->performance->control_register),
0221 &dst_perf->control_register);
0222 xen_copy_pct_data(&(_pr->performance->status_register),
0223 &dst_perf->status_register);
0224 dst_perf->flags |= XEN_PX_PCT;
0225 dst_states = xen_copy_pss_data(_pr, dst_perf);
0226 if (!IS_ERR_OR_NULL(dst_states)) {
0227 set_xen_guest_handle(dst_perf->states, dst_states);
0228 dst_perf->flags |= XEN_PX_PSS;
0229 }
0230 if (!xen_copy_psd_data(_pr, dst_perf))
0231 dst_perf->flags |= XEN_PX_PSD;
0232
0233 if (dst_perf->flags != (XEN_PX_PSD | XEN_PX_PSS | XEN_PX_PCT | XEN_PX_PPC)) {
0234 pr_warn("ACPI CPU%u missing some P-state data (%x), skipping\n",
0235 _pr->acpi_id, dst_perf->flags);
0236 ret = -ENODEV;
0237 goto err_free;
0238 }
0239
0240 if (!no_hypercall)
0241 ret = HYPERVISOR_platform_op(&op);
0242
0243 if (!ret) {
0244 struct acpi_processor_performance *perf;
0245 unsigned int i;
0246
0247 perf = _pr->performance;
0248 pr_debug("ACPI CPU%u - P-states uploaded.\n", _pr->acpi_id);
0249 for (i = 0; i < perf->state_count; i++) {
0250 pr_debug(" %cP%d: %d MHz, %d mW, %d uS\n",
0251 (i == perf->state ? '*' : ' '), i,
0252 (u32) perf->states[i].core_frequency,
0253 (u32) perf->states[i].power,
0254 (u32) perf->states[i].transition_latency);
0255 }
0256 } else if ((ret != -EINVAL) && (ret != -ENOSYS))
0257
0258
0259
0260 pr_warn("(_PXX): Hypervisor error (%d) for ACPI CPU%u\n",
0261 ret, _pr->acpi_id);
0262 err_free:
0263 if (!IS_ERR_OR_NULL(dst_states))
0264 kfree(dst_states);
0265
0266 return ret;
0267 }
0268 static int upload_pm_data(struct acpi_processor *_pr)
0269 {
0270 int err = 0;
0271
0272 mutex_lock(&acpi_ids_mutex);
0273 if (__test_and_set_bit(_pr->acpi_id, acpi_ids_done)) {
0274 mutex_unlock(&acpi_ids_mutex);
0275 return -EBUSY;
0276 }
0277 if (_pr->flags.power)
0278 err = push_cxx_to_hypervisor(_pr);
0279
0280 if (_pr->performance && _pr->performance->states)
0281 err |= push_pxx_to_hypervisor(_pr);
0282
0283 mutex_unlock(&acpi_ids_mutex);
0284 return err;
0285 }
0286 static unsigned int __init get_max_acpi_id(void)
0287 {
0288 struct xenpf_pcpuinfo *info;
0289 struct xen_platform_op op = {
0290 .cmd = XENPF_get_cpuinfo,
0291 .interface_version = XENPF_INTERFACE_VERSION,
0292 };
0293 int ret = 0;
0294 unsigned int i, last_cpu, max_acpi_id = 0;
0295
0296 info = &op.u.pcpu_info;
0297 info->xen_cpuid = 0;
0298
0299 ret = HYPERVISOR_platform_op(&op);
0300 if (ret)
0301 return NR_CPUS;
0302
0303
0304 last_cpu = op.u.pcpu_info.max_present;
0305 for (i = 0; i <= last_cpu; i++) {
0306 info->xen_cpuid = i;
0307 ret = HYPERVISOR_platform_op(&op);
0308 if (ret)
0309 continue;
0310 max_acpi_id = max(info->acpi_id, max_acpi_id);
0311 }
0312 max_acpi_id *= 2;
0313 pr_debug("Max ACPI ID: %u\n", max_acpi_id);
0314 return max_acpi_id;
0315 }
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325 static acpi_status
0326 read_acpi_id(acpi_handle handle, u32 lvl, void *context, void **rv)
0327 {
0328 u32 acpi_id;
0329 acpi_status status;
0330 acpi_object_type acpi_type;
0331 unsigned long long tmp;
0332 union acpi_object object = { 0 };
0333 struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
0334 acpi_io_address pblk = 0;
0335
0336 status = acpi_get_type(handle, &acpi_type);
0337 if (ACPI_FAILURE(status))
0338 return AE_OK;
0339
0340 switch (acpi_type) {
0341 case ACPI_TYPE_PROCESSOR:
0342 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
0343 if (ACPI_FAILURE(status))
0344 return AE_OK;
0345 acpi_id = object.processor.proc_id;
0346 pblk = object.processor.pblk_address;
0347 break;
0348 case ACPI_TYPE_DEVICE:
0349 status = acpi_evaluate_integer(handle, "_UID", NULL, &tmp);
0350 if (ACPI_FAILURE(status))
0351 return AE_OK;
0352 acpi_id = tmp;
0353 break;
0354 default:
0355 return AE_OK;
0356 }
0357 if (invalid_phys_cpuid(acpi_get_phys_id(handle,
0358 acpi_type == ACPI_TYPE_DEVICE,
0359 acpi_id))) {
0360 pr_debug("CPU with ACPI ID %u is unavailable\n", acpi_id);
0361 return AE_OK;
0362 }
0363
0364
0365 if (acpi_id >= nr_acpi_bits) {
0366 pr_debug("max acpi id %u, trying to set %u\n",
0367 nr_acpi_bits - 1, acpi_id);
0368 return AE_OK;
0369 }
0370
0371 __set_bit(acpi_id, acpi_id_present);
0372
0373 pr_debug("ACPI CPU%u w/ PBLK:0x%lx\n", acpi_id, (unsigned long)pblk);
0374
0375
0376 if (!acpi_processor_get_psd(handle, &acpi_psd[acpi_id])) {
0377 pr_debug("ACPI CPU%u w/ PST:coord_type = %llu domain = %llu\n",
0378 acpi_id, acpi_psd[acpi_id].coord_type,
0379 acpi_psd[acpi_id].domain);
0380 }
0381
0382 status = acpi_evaluate_object(handle, "_CST", NULL, &buffer);
0383 if (ACPI_FAILURE(status)) {
0384 if (!pblk)
0385 return AE_OK;
0386 }
0387
0388 __set_bit(acpi_id, acpi_id_cst_present);
0389
0390 return AE_OK;
0391 }
0392 static int check_acpi_ids(struct acpi_processor *pr_backup)
0393 {
0394
0395 if (!pr_backup)
0396 return -ENODEV;
0397
0398 if (acpi_id_present && acpi_id_cst_present)
0399
0400 goto upload;
0401
0402
0403
0404
0405 acpi_id_present = bitmap_zalloc(nr_acpi_bits, GFP_KERNEL);
0406 if (!acpi_id_present)
0407 return -ENOMEM;
0408
0409 acpi_id_cst_present = bitmap_zalloc(nr_acpi_bits, GFP_KERNEL);
0410 if (!acpi_id_cst_present) {
0411 bitmap_free(acpi_id_present);
0412 return -ENOMEM;
0413 }
0414
0415 acpi_psd = kcalloc(nr_acpi_bits, sizeof(struct acpi_psd_package),
0416 GFP_KERNEL);
0417 if (!acpi_psd) {
0418 bitmap_free(acpi_id_present);
0419 bitmap_free(acpi_id_cst_present);
0420 return -ENOMEM;
0421 }
0422
0423 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
0424 ACPI_UINT32_MAX,
0425 read_acpi_id, NULL, NULL, NULL);
0426 acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, read_acpi_id, NULL, NULL);
0427
0428 upload:
0429 if (!bitmap_equal(acpi_id_present, acpi_ids_done, nr_acpi_bits)) {
0430 unsigned int i;
0431 for_each_set_bit(i, acpi_id_present, nr_acpi_bits) {
0432 pr_backup->acpi_id = i;
0433
0434 pr_backup->flags.power = test_bit(i, acpi_id_cst_present);
0435
0436 if (acpi_psd[i].num_entries) {
0437 memcpy(&pr_backup->performance->domain_info,
0438 &acpi_psd[i],
0439 sizeof(struct acpi_psd_package));
0440 }
0441 (void)upload_pm_data(pr_backup);
0442 }
0443 }
0444
0445 return 0;
0446 }
0447
0448
0449 static struct acpi_processor_performance __percpu *acpi_perf_data;
0450
0451 static void free_acpi_perf_data(void)
0452 {
0453 int i;
0454
0455
0456 for_each_possible_cpu(i)
0457 free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
0458 ->shared_cpu_map);
0459 free_percpu(acpi_perf_data);
0460 }
0461
0462 static int xen_upload_processor_pm_data(void)
0463 {
0464 struct acpi_processor *pr_backup = NULL;
0465 int i;
0466 int rc = 0;
0467
0468 pr_info("Uploading Xen processor PM info\n");
0469
0470 for_each_possible_cpu(i) {
0471 struct acpi_processor *_pr;
0472 _pr = per_cpu(processors, i );
0473 if (!_pr)
0474 continue;
0475
0476 if (!pr_backup) {
0477 pr_backup = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
0478 if (pr_backup)
0479 memcpy(pr_backup, _pr, sizeof(struct acpi_processor));
0480 }
0481 (void)upload_pm_data(_pr);
0482 }
0483
0484 rc = check_acpi_ids(pr_backup);
0485 kfree(pr_backup);
0486
0487 return rc;
0488 }
0489
0490 static void xen_acpi_processor_resume_worker(struct work_struct *dummy)
0491 {
0492 int rc;
0493
0494 bitmap_zero(acpi_ids_done, nr_acpi_bits);
0495
0496 rc = xen_upload_processor_pm_data();
0497 if (rc != 0)
0498 pr_info("ACPI data upload failed, error = %d\n", rc);
0499 }
0500
0501 static void xen_acpi_processor_resume(void)
0502 {
0503 static DECLARE_WORK(wq, xen_acpi_processor_resume_worker);
0504
0505
0506
0507
0508
0509
0510
0511
0512 schedule_work(&wq);
0513 }
0514
0515 static struct syscore_ops xap_syscore_ops = {
0516 .resume = xen_acpi_processor_resume,
0517 };
0518
0519 static int __init xen_acpi_processor_init(void)
0520 {
0521 int i;
0522 int rc;
0523
0524 if (!xen_initial_domain())
0525 return -ENODEV;
0526
0527 nr_acpi_bits = get_max_acpi_id() + 1;
0528 acpi_ids_done = bitmap_zalloc(nr_acpi_bits, GFP_KERNEL);
0529 if (!acpi_ids_done)
0530 return -ENOMEM;
0531
0532 acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
0533 if (!acpi_perf_data) {
0534 pr_debug("Memory allocation error for acpi_perf_data\n");
0535 bitmap_free(acpi_ids_done);
0536 return -ENOMEM;
0537 }
0538 for_each_possible_cpu(i) {
0539 if (!zalloc_cpumask_var_node(
0540 &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
0541 GFP_KERNEL, cpu_to_node(i))) {
0542 rc = -ENOMEM;
0543 goto err_out;
0544 }
0545 }
0546
0547
0548 (void)acpi_processor_preregister_performance(acpi_perf_data);
0549
0550 for_each_possible_cpu(i) {
0551 struct acpi_processor *pr;
0552 struct acpi_processor_performance *perf;
0553
0554 pr = per_cpu(processors, i);
0555 perf = per_cpu_ptr(acpi_perf_data, i);
0556 if (!pr)
0557 continue;
0558
0559 pr->performance = perf;
0560 rc = acpi_processor_get_performance_info(pr);
0561 if (rc)
0562 goto err_out;
0563 }
0564
0565 rc = xen_upload_processor_pm_data();
0566 if (rc)
0567 goto err_unregister;
0568
0569 register_syscore_ops(&xap_syscore_ops);
0570
0571 return 0;
0572 err_unregister:
0573 for_each_possible_cpu(i)
0574 acpi_processor_unregister_performance(i);
0575
0576 err_out:
0577
0578 free_acpi_perf_data();
0579 bitmap_free(acpi_ids_done);
0580 return rc;
0581 }
0582 static void __exit xen_acpi_processor_exit(void)
0583 {
0584 int i;
0585
0586 unregister_syscore_ops(&xap_syscore_ops);
0587 bitmap_free(acpi_ids_done);
0588 bitmap_free(acpi_id_present);
0589 bitmap_free(acpi_id_cst_present);
0590 kfree(acpi_psd);
0591 for_each_possible_cpu(i)
0592 acpi_processor_unregister_performance(i);
0593
0594 free_acpi_perf_data();
0595 }
0596
0597 MODULE_AUTHOR("Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>");
0598 MODULE_DESCRIPTION("Xen ACPI Processor P-states (and Cx) driver which uploads PM data to Xen hypervisor");
0599 MODULE_LICENSE("GPL");
0600
0601
0602
0603 device_initcall(xen_acpi_processor_init);
0604 module_exit(xen_acpi_processor_exit);