1 /*
2  * Copyright 2012 by Oracle Inc
3  * Author: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
4  *
5  * This code borrows ideas from https://lkml.org/lkml/2011/11/30/249
6  * so many thanks go to Kevin Tian <kevin.tian@intel.com>
7  * and Yu Ke <ke.yu@intel.com>.
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms and conditions of the GNU General Public License,
11  * version 2, as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
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 <acpi/acpi_bus.h>
29 #include <acpi/acpi_drivers.h>
30 #include <acpi/processor.h>
31 
32 #include <xen/xen.h>
33 #include <xen/interface/platform.h>
34 #include <asm/xen/hypercall.h>
35 
36 #define DRV_NAME "xen-acpi-processor: "
37 
38 static int no_hypercall;
39 MODULE_PARM_DESC(off, "Inhibit the hypercall.");
40 module_param_named(off, no_hypercall, int, 0400);
41 
42 /*
43  * Note: Do not convert the acpi_id* below to cpumask_var_t or use cpumask_bit
44  * - as those shrink to nr_cpu_bits (which is dependent on possible_cpu), which
45  * can be less than what we want to put in. Instead use the 'nr_acpi_bits'
46  * which is dynamically computed based on the MADT or x2APIC table.
47  */
48 static unsigned int nr_acpi_bits;
49 /* Mutex to protect the acpi_ids_done - for CPU hotplug use. */
50 static DEFINE_MUTEX(acpi_ids_mutex);
51 /* Which ACPI ID we have processed from 'struct acpi_processor'. */
52 static unsigned long *acpi_ids_done;
53 /* Which ACPI ID exist in the SSDT/DSDT processor definitions. */
54 static unsigned long __initdata *acpi_id_present;
55 /* And if there is an _CST definition (or a PBLK) for the ACPI IDs */
56 static unsigned long __initdata *acpi_id_cst_present;
57 
58 static int push_cxx_to_hypervisor(struct acpi_processor *_pr)
59 {
60 	struct xen_platform_op op = {
61 		.cmd			= XENPF_set_processor_pminfo,
62 		.interface_version	= XENPF_INTERFACE_VERSION,
63 		.u.set_pminfo.id	= _pr->acpi_id,
64 		.u.set_pminfo.type	= XEN_PM_CX,
65 	};
66 	struct xen_processor_cx *dst_cx, *dst_cx_states = NULL;
67 	struct acpi_processor_cx *cx;
68 	unsigned int i, ok;
69 	int ret = 0;
70 
71 	dst_cx_states = kcalloc(_pr->power.count,
72 				sizeof(struct xen_processor_cx), GFP_KERNEL);
73 	if (!dst_cx_states)
74 		return -ENOMEM;
75 
76 	for (ok = 0, i = 1; i <= _pr->power.count; i++) {
77 		cx = &_pr->power.states[i];
78 		if (!cx->valid)
79 			continue;
80 
81 		dst_cx = &(dst_cx_states[ok++]);
82 
83 		dst_cx->reg.space_id = ACPI_ADR_SPACE_SYSTEM_IO;
84 		if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) {
85 			dst_cx->reg.bit_width = 8;
86 			dst_cx->reg.bit_offset = 0;
87 			dst_cx->reg.access_size = 1;
88 		} else {
89 			dst_cx->reg.space_id = ACPI_ADR_SPACE_FIXED_HARDWARE;
90 			if (cx->entry_method == ACPI_CSTATE_FFH) {
91 				/* NATIVE_CSTATE_BEYOND_HALT */
92 				dst_cx->reg.bit_offset = 2;
93 				dst_cx->reg.bit_width = 1; /* VENDOR_INTEL */
94 			}
95 			dst_cx->reg.access_size = 0;
96 		}
97 		dst_cx->reg.address = cx->address;
98 
99 		dst_cx->type = cx->type;
100 		dst_cx->latency = cx->latency;
101 
102 		dst_cx->dpcnt = 0;
103 		set_xen_guest_handle(dst_cx->dp, NULL);
104 	}
105 	if (!ok) {
106 		pr_debug(DRV_NAME "No _Cx for ACPI CPU %u\n", _pr->acpi_id);
107 		kfree(dst_cx_states);
108 		return -EINVAL;
109 	}
110 	op.u.set_pminfo.power.count = ok;
111 	op.u.set_pminfo.power.flags.bm_control = _pr->flags.bm_control;
112 	op.u.set_pminfo.power.flags.bm_check = _pr->flags.bm_check;
113 	op.u.set_pminfo.power.flags.has_cst = _pr->flags.has_cst;
114 	op.u.set_pminfo.power.flags.power_setup_done =
115 		_pr->flags.power_setup_done;
116 
117 	set_xen_guest_handle(op.u.set_pminfo.power.states, dst_cx_states);
118 
119 	if (!no_hypercall)
120 		ret = HYPERVISOR_dom0_op(&op);
121 
122 	if (!ret) {
123 		pr_debug("ACPI CPU%u - C-states uploaded.\n", _pr->acpi_id);
124 		for (i = 1; i <= _pr->power.count; i++) {
125 			cx = &_pr->power.states[i];
126 			if (!cx->valid)
127 				continue;
128 			pr_debug("     C%d: %s %d uS\n",
129 				 cx->type, cx->desc, (u32)cx->latency);
130 		}
131 	} else if (ret != -EINVAL)
132 		/* EINVAL means the ACPI ID is incorrect - meaning the ACPI
133 		 * table is referencing a non-existing CPU - which can happen
134 		 * with broken ACPI tables. */
135 		pr_err(DRV_NAME "(CX): Hypervisor error (%d) for ACPI CPU%u\n",
136 		       ret, _pr->acpi_id);
137 
138 	kfree(dst_cx_states);
139 
140 	return ret;
141 }
142 static struct xen_processor_px *
143 xen_copy_pss_data(struct acpi_processor *_pr,
144 		  struct xen_processor_performance *dst_perf)
145 {
146 	struct xen_processor_px *dst_states = NULL;
147 	unsigned int i;
148 
149 	BUILD_BUG_ON(sizeof(struct xen_processor_px) !=
150 		     sizeof(struct acpi_processor_px));
151 
152 	dst_states = kcalloc(_pr->performance->state_count,
153 			     sizeof(struct xen_processor_px), GFP_KERNEL);
154 	if (!dst_states)
155 		return ERR_PTR(-ENOMEM);
156 
157 	dst_perf->state_count = _pr->performance->state_count;
158 	for (i = 0; i < _pr->performance->state_count; i++) {
159 		/* Fortunatly for us, they are both the same size */
160 		memcpy(&(dst_states[i]), &(_pr->performance->states[i]),
161 		       sizeof(struct acpi_processor_px));
162 	}
163 	return dst_states;
164 }
165 static int xen_copy_psd_data(struct acpi_processor *_pr,
166 			     struct xen_processor_performance *dst)
167 {
168 	struct acpi_psd_package *pdomain;
169 
170 	BUILD_BUG_ON(sizeof(struct xen_psd_package) !=
171 		     sizeof(struct acpi_psd_package));
172 
173 	/* This information is enumerated only if acpi_processor_preregister_performance
174 	 * has been called.
175 	 */
176 	dst->shared_type = _pr->performance->shared_type;
177 
178 	pdomain = &(_pr->performance->domain_info);
179 
180 	/* 'acpi_processor_preregister_performance' does not parse if the
181 	 * num_processors <= 1, but Xen still requires it. Do it manually here.
182 	 */
183 	if (pdomain->num_processors <= 1) {
184 		if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
185 			dst->shared_type = CPUFREQ_SHARED_TYPE_ALL;
186 		else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
187 			dst->shared_type = CPUFREQ_SHARED_TYPE_HW;
188 		else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
189 			dst->shared_type = CPUFREQ_SHARED_TYPE_ANY;
190 
191 	}
192 	memcpy(&(dst->domain_info), pdomain, sizeof(struct acpi_psd_package));
193 	return 0;
194 }
195 static int xen_copy_pct_data(struct acpi_pct_register *pct,
196 			     struct xen_pct_register *dst_pct)
197 {
198 	/* It would be nice if you could just do 'memcpy(pct, dst_pct') but
199 	 * sadly the Xen structure did not have the proper padding so the
200 	 * descriptor field takes two (dst_pct) bytes instead of one (pct).
201 	 */
202 	dst_pct->descriptor = pct->descriptor;
203 	dst_pct->length = pct->length;
204 	dst_pct->space_id = pct->space_id;
205 	dst_pct->bit_width = pct->bit_width;
206 	dst_pct->bit_offset = pct->bit_offset;
207 	dst_pct->reserved = pct->reserved;
208 	dst_pct->address = pct->address;
209 	return 0;
210 }
211 static int push_pxx_to_hypervisor(struct acpi_processor *_pr)
212 {
213 	int ret = 0;
214 	struct xen_platform_op op = {
215 		.cmd			= XENPF_set_processor_pminfo,
216 		.interface_version	= XENPF_INTERFACE_VERSION,
217 		.u.set_pminfo.id	= _pr->acpi_id,
218 		.u.set_pminfo.type	= XEN_PM_PX,
219 	};
220 	struct xen_processor_performance *dst_perf;
221 	struct xen_processor_px *dst_states = NULL;
222 
223 	dst_perf = &op.u.set_pminfo.perf;
224 
225 	dst_perf->platform_limit = _pr->performance_platform_limit;
226 	dst_perf->flags |= XEN_PX_PPC;
227 	xen_copy_pct_data(&(_pr->performance->control_register),
228 			  &dst_perf->control_register);
229 	xen_copy_pct_data(&(_pr->performance->status_register),
230 			  &dst_perf->status_register);
231 	dst_perf->flags |= XEN_PX_PCT;
232 	dst_states = xen_copy_pss_data(_pr, dst_perf);
233 	if (!IS_ERR_OR_NULL(dst_states)) {
234 		set_xen_guest_handle(dst_perf->states, dst_states);
235 		dst_perf->flags |= XEN_PX_PSS;
236 	}
237 	if (!xen_copy_psd_data(_pr, dst_perf))
238 		dst_perf->flags |= XEN_PX_PSD;
239 
240 	if (dst_perf->flags != (XEN_PX_PSD | XEN_PX_PSS | XEN_PX_PCT | XEN_PX_PPC)) {
241 		pr_warn(DRV_NAME "ACPI CPU%u missing some P-state data (%x), skipping.\n",
242 			_pr->acpi_id, dst_perf->flags);
243 		ret = -ENODEV;
244 		goto err_free;
245 	}
246 
247 	if (!no_hypercall)
248 		ret = HYPERVISOR_dom0_op(&op);
249 
250 	if (!ret) {
251 		struct acpi_processor_performance *perf;
252 		unsigned int i;
253 
254 		perf = _pr->performance;
255 		pr_debug("ACPI CPU%u - P-states uploaded.\n", _pr->acpi_id);
256 		for (i = 0; i < perf->state_count; i++) {
257 			pr_debug("     %cP%d: %d MHz, %d mW, %d uS\n",
258 			(i == perf->state ? '*' : ' '), i,
259 			(u32) perf->states[i].core_frequency,
260 			(u32) perf->states[i].power,
261 			(u32) perf->states[i].transition_latency);
262 		}
263 	} else if (ret != -EINVAL)
264 		/* EINVAL means the ACPI ID is incorrect - meaning the ACPI
265 		 * table is referencing a non-existing CPU - which can happen
266 		 * with broken ACPI tables. */
267 		pr_warn(DRV_NAME "(_PXX): Hypervisor error (%d) for ACPI CPU%u\n",
268 		       ret, _pr->acpi_id);
269 err_free:
270 	if (!IS_ERR_OR_NULL(dst_states))
271 		kfree(dst_states);
272 
273 	return ret;
274 }
275 static int upload_pm_data(struct acpi_processor *_pr)
276 {
277 	int err = 0;
278 
279 	mutex_lock(&acpi_ids_mutex);
280 	if (__test_and_set_bit(_pr->acpi_id, acpi_ids_done)) {
281 		mutex_unlock(&acpi_ids_mutex);
282 		return -EBUSY;
283 	}
284 	if (_pr->flags.power)
285 		err = push_cxx_to_hypervisor(_pr);
286 
287 	if (_pr->performance && _pr->performance->states)
288 		err |= push_pxx_to_hypervisor(_pr);
289 
290 	mutex_unlock(&acpi_ids_mutex);
291 	return err;
292 }
293 static unsigned int __init get_max_acpi_id(void)
294 {
295 	struct xenpf_pcpuinfo *info;
296 	struct xen_platform_op op = {
297 		.cmd = XENPF_get_cpuinfo,
298 		.interface_version = XENPF_INTERFACE_VERSION,
299 	};
300 	int ret = 0;
301 	unsigned int i, last_cpu, max_acpi_id = 0;
302 
303 	info = &op.u.pcpu_info;
304 	info->xen_cpuid = 0;
305 
306 	ret = HYPERVISOR_dom0_op(&op);
307 	if (ret)
308 		return NR_CPUS;
309 
310 	/* The max_present is the same irregardless of the xen_cpuid */
311 	last_cpu = op.u.pcpu_info.max_present;
312 	for (i = 0; i <= last_cpu; i++) {
313 		info->xen_cpuid = i;
314 		ret = HYPERVISOR_dom0_op(&op);
315 		if (ret)
316 			continue;
317 		max_acpi_id = max(info->acpi_id, max_acpi_id);
318 	}
319 	max_acpi_id *= 2; /* Slack for CPU hotplug support. */
320 	pr_debug(DRV_NAME "Max ACPI ID: %u\n", max_acpi_id);
321 	return max_acpi_id;
322 }
323 /*
324  * The read_acpi_id and check_acpi_ids are there to support the Xen
325  * oddity of virtual CPUs != physical CPUs in the initial domain.
326  * The user can supply 'xen_max_vcpus=X' on the Xen hypervisor line
327  * which will band the amount of CPUs the initial domain can see.
328  * In general that is OK, except it plays havoc with any of the
329  * for_each_[present|online]_cpu macros which are banded to the virtual
330  * CPU amount.
331  */
332 static acpi_status __init
333 read_acpi_id(acpi_handle handle, u32 lvl, void *context, void **rv)
334 {
335 	u32 acpi_id;
336 	acpi_status status;
337 	acpi_object_type acpi_type;
338 	unsigned long long tmp;
339 	union acpi_object object = { 0 };
340 	struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
341 	acpi_io_address pblk = 0;
342 
343 	status = acpi_get_type(handle, &acpi_type);
344 	if (ACPI_FAILURE(status))
345 		return AE_OK;
346 
347 	switch (acpi_type) {
348 	case ACPI_TYPE_PROCESSOR:
349 		status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
350 		if (ACPI_FAILURE(status))
351 			return AE_OK;
352 		acpi_id = object.processor.proc_id;
353 		pblk = object.processor.pblk_address;
354 		break;
355 	case ACPI_TYPE_DEVICE:
356 		status = acpi_evaluate_integer(handle, "_UID", NULL, &tmp);
357 		if (ACPI_FAILURE(status))
358 			return AE_OK;
359 		acpi_id = tmp;
360 		break;
361 	default:
362 		return AE_OK;
363 	}
364 	/* There are more ACPI Processor objects than in x2APIC or MADT.
365 	 * This can happen with incorrect ACPI SSDT declerations. */
366 	if (acpi_id > nr_acpi_bits) {
367 		pr_debug(DRV_NAME "We only have %u, trying to set %u\n",
368 			 nr_acpi_bits, acpi_id);
369 		return AE_OK;
370 	}
371 	/* OK, There is a ACPI Processor object */
372 	__set_bit(acpi_id, acpi_id_present);
373 
374 	pr_debug(DRV_NAME "ACPI CPU%u w/ PBLK:0x%lx\n", acpi_id,
375 		 (unsigned long)pblk);
376 
377 	status = acpi_evaluate_object(handle, "_CST", NULL, &buffer);
378 	if (ACPI_FAILURE(status)) {
379 		if (!pblk)
380 			return AE_OK;
381 	}
382 	/* .. and it has a C-state */
383 	__set_bit(acpi_id, acpi_id_cst_present);
384 
385 	return AE_OK;
386 }
387 static int __init check_acpi_ids(struct acpi_processor *pr_backup)
388 {
389 
390 	if (!pr_backup)
391 		return -ENODEV;
392 
393 	/* All online CPUs have been processed at this stage. Now verify
394 	 * whether in fact "online CPUs" == physical CPUs.
395 	 */
396 	acpi_id_present = kcalloc(BITS_TO_LONGS(nr_acpi_bits), sizeof(unsigned long), GFP_KERNEL);
397 	if (!acpi_id_present)
398 		return -ENOMEM;
399 
400 	acpi_id_cst_present = kcalloc(BITS_TO_LONGS(nr_acpi_bits), sizeof(unsigned long), GFP_KERNEL);
401 	if (!acpi_id_cst_present) {
402 		kfree(acpi_id_present);
403 		return -ENOMEM;
404 	}
405 
406 	acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
407 			    ACPI_UINT32_MAX,
408 			    read_acpi_id, NULL, NULL, NULL);
409 	acpi_get_devices("ACPI0007", read_acpi_id, NULL, NULL);
410 
411 	if (!bitmap_equal(acpi_id_present, acpi_ids_done, nr_acpi_bits)) {
412 		unsigned int i;
413 		for_each_set_bit(i, acpi_id_present, nr_acpi_bits) {
414 			pr_backup->acpi_id = i;
415 			/* Mask out C-states if there are no _CST or PBLK */
416 			pr_backup->flags.power = test_bit(i, acpi_id_cst_present);
417 			(void)upload_pm_data(pr_backup);
418 		}
419 	}
420 	kfree(acpi_id_present);
421 	acpi_id_present = NULL;
422 	kfree(acpi_id_cst_present);
423 	acpi_id_cst_present = NULL;
424 	return 0;
425 }
426 static int __init check_prereq(void)
427 {
428 	struct cpuinfo_x86 *c = &cpu_data(0);
429 
430 	if (!xen_initial_domain())
431 		return -ENODEV;
432 
433 	if (!acpi_gbl_FADT.smi_command)
434 		return -ENODEV;
435 
436 	if (c->x86_vendor == X86_VENDOR_INTEL) {
437 		if (!cpu_has(c, X86_FEATURE_EST))
438 			return -ENODEV;
439 
440 		return 0;
441 	}
442 	if (c->x86_vendor == X86_VENDOR_AMD) {
443 		/* Copied from powernow-k8.h, can't include ../cpufreq/powernow
444 		 * as we get compile warnings for the static functions.
445 		 */
446 #define CPUID_FREQ_VOLT_CAPABILITIES    0x80000007
447 #define USE_HW_PSTATE                   0x00000080
448 		u32 eax, ebx, ecx, edx;
449 		cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
450 		if ((edx & USE_HW_PSTATE) != USE_HW_PSTATE)
451 			return -ENODEV;
452 		return 0;
453 	}
454 	return -ENODEV;
455 }
456 /* acpi_perf_data is a pointer to percpu data. */
457 static struct acpi_processor_performance __percpu *acpi_perf_data;
458 
459 static void free_acpi_perf_data(void)
460 {
461 	unsigned int i;
462 
463 	/* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
464 	for_each_possible_cpu(i)
465 		free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
466 				 ->shared_cpu_map);
467 	free_percpu(acpi_perf_data);
468 }
469 
470 static int __init xen_acpi_processor_init(void)
471 {
472 	struct acpi_processor *pr_backup = NULL;
473 	unsigned int i;
474 	int rc = check_prereq();
475 
476 	if (rc)
477 		return rc;
478 
479 	nr_acpi_bits = get_max_acpi_id() + 1;
480 	acpi_ids_done = kcalloc(BITS_TO_LONGS(nr_acpi_bits), sizeof(unsigned long), GFP_KERNEL);
481 	if (!acpi_ids_done)
482 		return -ENOMEM;
483 
484 	acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
485 	if (!acpi_perf_data) {
486 		pr_debug(DRV_NAME "Memory allocation error for acpi_perf_data.\n");
487 		kfree(acpi_ids_done);
488 		return -ENOMEM;
489 	}
490 	for_each_possible_cpu(i) {
491 		if (!zalloc_cpumask_var_node(
492 			&per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
493 			GFP_KERNEL, cpu_to_node(i))) {
494 			rc = -ENOMEM;
495 			goto err_out;
496 		}
497 	}
498 
499 	/* Do initialization in ACPI core. It is OK to fail here. */
500 	(void)acpi_processor_preregister_performance(acpi_perf_data);
501 
502 	for_each_possible_cpu(i) {
503 		struct acpi_processor_performance *perf;
504 
505 		perf = per_cpu_ptr(acpi_perf_data, i);
506 		rc = acpi_processor_register_performance(perf, i);
507 		if (rc)
508 			goto err_out;
509 	}
510 	rc = acpi_processor_notify_smm(THIS_MODULE);
511 	if (rc)
512 		goto err_unregister;
513 
514 	for_each_possible_cpu(i) {
515 		struct acpi_processor *_pr;
516 		_pr = per_cpu(processors, i /* APIC ID */);
517 		if (!_pr)
518 			continue;
519 
520 		if (!pr_backup) {
521 			pr_backup = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
522 			if (pr_backup)
523 				memcpy(pr_backup, _pr, sizeof(struct acpi_processor));
524 		}
525 		(void)upload_pm_data(_pr);
526 	}
527 	rc = check_acpi_ids(pr_backup);
528 
529 	kfree(pr_backup);
530 	pr_backup = NULL;
531 
532 	if (rc)
533 		goto err_unregister;
534 
535 	return 0;
536 err_unregister:
537 	for_each_possible_cpu(i) {
538 		struct acpi_processor_performance *perf;
539 		perf = per_cpu_ptr(acpi_perf_data, i);
540 		acpi_processor_unregister_performance(perf, i);
541 	}
542 err_out:
543 	/* Freeing a NULL pointer is OK: alloc_percpu zeroes. */
544 	free_acpi_perf_data();
545 	kfree(acpi_ids_done);
546 	return rc;
547 }
548 static void __exit xen_acpi_processor_exit(void)
549 {
550 	int i;
551 
552 	kfree(acpi_ids_done);
553 	for_each_possible_cpu(i) {
554 		struct acpi_processor_performance *perf;
555 		perf = per_cpu_ptr(acpi_perf_data, i);
556 		acpi_processor_unregister_performance(perf, i);
557 	}
558 	free_acpi_perf_data();
559 }
560 
561 MODULE_AUTHOR("Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>");
562 MODULE_DESCRIPTION("Xen ACPI Processor P-states (and Cx) driver which uploads PM data to Xen hypervisor");
563 MODULE_LICENSE("GPL");
564 
565 /* We want to be loaded before the CPU freq scaling drivers are loaded.
566  * They are loaded in late_initcall. */
567 device_initcall(xen_acpi_processor_init);
568 module_exit(xen_acpi_processor_exit);
569