Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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