Changeset View
Changeset View
Standalone View
Standalone View
sys/x86/cpufreq/hwpstate_amd.c
| Show First 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | |||||
| #include <machine/_inttypes.h> | #include <machine/_inttypes.h> | ||||
| #include <machine/cputypes.h> | #include <machine/cputypes.h> | ||||
| #include <machine/md_var.h> | #include <machine/md_var.h> | ||||
| #include <machine/specialreg.h> | #include <machine/specialreg.h> | ||||
| #include <contrib/dev/acpica/include/acpi.h> | #include <contrib/dev/acpica/include/acpi.h> | ||||
| #include <dev/acpica/acpivar.h> | #include <dev/acpica/acpivar.h> | ||||
| #include <dev/acpica/acpi_cppc_lib.h> | |||||
| #include <x86/cpufreq/hwpstate_common.h> | #include <x86/cpufreq/hwpstate_common.h> | ||||
| #include "acpi_if.h" | #include "acpi_if.h" | ||||
| #include "cpufreq_if.h" | #include "cpufreq_if.h" | ||||
| #define MSR_AMD_10H_11H_LIMIT 0xc0010061 | #define MSR_AMD_10H_11H_LIMIT 0xc0010061 | ||||
| #define MSR_AMD_10H_11H_CONTROL 0xc0010062 | #define MSR_AMD_10H_11H_CONTROL 0xc0010062 | ||||
| #define MSR_AMD_10H_11H_STATUS 0xc0010063 | #define MSR_AMD_10H_11H_STATUS 0xc0010063 | ||||
| #define MSR_AMD_10H_11H_CONFIG 0xc0010064 | #define MSR_AMD_10H_11H_CONFIG 0xc0010064 | ||||
| #define MSR_AMD_CPPC_CAPS_1 0xc00102b0 | #define MSR_AMD_CPPC_CAPS_1 0xc00102b0 | ||||
| #define MSR_AMD_CPPC_ENABLE 0xc00102b1 | #define MSR_AMD_CPPC_ENABLE 0xc00102b1 | ||||
| #define MSR_AMD_CPPC_CAPS_2 0xc00102b2 | #define MSR_AMD_CPPC_CAPS_2 0xc00102b2 | ||||
| ▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | struct hwpstate_setting { | ||||
| int power; /* Power consumed in mW. */ | int power; /* Power consumed in mW. */ | ||||
| int lat; /* Transition latency in us. */ | int lat; /* Transition latency in us. */ | ||||
| int pstate_id; /* P-State id */ | int pstate_id; /* P-State id */ | ||||
| }; | }; | ||||
| #define HWPFL_USE_CPPC (1 << 0) | #define HWPFL_USE_CPPC (1 << 0) | ||||
| #define HWPFL_CPPC_REQUEST_NOT_READ (1 << 1) | #define HWPFL_CPPC_REQUEST_NOT_READ (1 << 1) | ||||
| /* Bits for the hwpstate_amd_cppc_verify tunable. */ | |||||
| #define HWPSTATE_AMD_CPPC_VERIFY_CPUID (1 << 0) | |||||
| #define HWPSTATE_AMD_CPPC_VERIFY_ACPI (1 << 1) | |||||
| struct hwpstate_cpufreq_methods { | struct hwpstate_cpufreq_methods { | ||||
| int (*get)(device_t dev, struct cf_setting *cf); | int (*get)(device_t dev, struct cf_setting *cf); | ||||
| int (*set)(device_t dev, const struct cf_setting *cf); | int (*set)(device_t dev, const struct cf_setting *cf); | ||||
| int (*settings)(device_t dev, struct cf_setting *sets, int *count); | int (*settings)(device_t dev, struct cf_setting *sets, int *count); | ||||
| int (*type)(device_t dev, int *type); | int (*type)(device_t dev, int *type); | ||||
| }; | }; | ||||
| /* | /* | ||||
| Show All 9 Lines | struct hwpstate_softc { | ||||
| union { | union { | ||||
| struct { | struct { | ||||
| struct hwpstate_setting | struct hwpstate_setting | ||||
| hwpstate_settings[AMD_10H_11H_MAX_STATES]; | hwpstate_settings[AMD_10H_11H_MAX_STATES]; | ||||
| int cfnum; | int cfnum; | ||||
| }; | }; | ||||
| struct { | struct { | ||||
| uint64_t request; | uint64_t request; | ||||
| struct acpi_cppc_ctx *ctx; | |||||
| } cppc; | } cppc; | ||||
| }; | }; | ||||
| u_int cpuid; | u_int cpuid; | ||||
| }; | }; | ||||
| static void hwpstate_identify(driver_t *driver, device_t parent); | static void hwpstate_identify(driver_t *driver, device_t parent); | ||||
| static int hwpstate_probe(device_t dev); | static int hwpstate_probe(device_t dev); | ||||
| static int hwpstate_attach(device_t dev); | static int hwpstate_attach(device_t dev); | ||||
| Show All 17 Lines | SYSCTL_BOOL(_debug, OID_AUTO, hwpstate_pstate_limit, CTLFLAG_RWTUN, | ||||
| &hwpstate_pstate_limit, 0, | &hwpstate_pstate_limit, 0, | ||||
| "If enabled (1), limit administrative control of P-states to the value in " | "If enabled (1), limit administrative control of P-states to the value in " | ||||
| "CurPstateLimit"); | "CurPstateLimit"); | ||||
| static bool hwpstate_amd_cppc_enable = true; | static bool hwpstate_amd_cppc_enable = true; | ||||
| SYSCTL_BOOL(_machdep, OID_AUTO, hwpstate_amd_cppc_enable, CTLFLAG_RDTUN, | SYSCTL_BOOL(_machdep, OID_AUTO, hwpstate_amd_cppc_enable, CTLFLAG_RDTUN, | ||||
| &hwpstate_amd_cppc_enable, 0, | &hwpstate_amd_cppc_enable, 0, | ||||
| "Set 1 (default) to enable AMD CPPC, 0 to disable"); | "Set 1 (default) to enable AMD CPPC, 0 to disable"); | ||||
| static int hwpstate_amd_cppc_verify = HWPSTATE_AMD_CPPC_VERIFY_CPUID | | |||||
| HWPSTATE_AMD_CPPC_VERIFY_ACPI; | |||||
| SYSCTL_INT(_machdep, OID_AUTO, hwpstate_amd_cppc_verify, CTLFLAG_RDTUN, | |||||
| &hwpstate_amd_cppc_verify, 0, | |||||
| "Bitmask gating CPPC use (default 0x3): bit 0 (0x1) requires the CPUID " | |||||
| "CPPC bit; bit 1 (0x2) requires an ACPI _CPC object"); | |||||
| static device_method_t hwpstate_methods[] = { | static device_method_t hwpstate_methods[] = { | ||||
| /* Device interface */ | /* Device interface */ | ||||
| DEVMETHOD(device_identify, hwpstate_identify), | DEVMETHOD(device_identify, hwpstate_identify), | ||||
| DEVMETHOD(device_probe, hwpstate_probe), | DEVMETHOD(device_probe, hwpstate_probe), | ||||
| DEVMETHOD(device_attach, hwpstate_attach), | DEVMETHOD(device_attach, hwpstate_attach), | ||||
| DEVMETHOD(device_detach, hwpstate_detach), | DEVMETHOD(device_detach, hwpstate_detach), | ||||
| DEVMETHOD(device_shutdown, hwpstate_shutdown), | DEVMETHOD(device_shutdown, hwpstate_shutdown), | ||||
| ▲ Show 20 Lines • Show All 647 Lines • ▼ Show 20 Lines | enable_cppc_cb(void *args) | ||||
| SET_BITS_VALUE(data->request, AMD_CPPC_REQUEST_MAX_PERF_BITS, | SET_BITS_VALUE(data->request, AMD_CPPC_REQUEST_MAX_PERF_BITS, | ||||
| highest_perf); | highest_perf); | ||||
| /* | /* | ||||
| * Set controls to maximum performance to avoid regressions now that | * Set controls to maximum performance to avoid regressions now that | ||||
| * CPPC is activated by default and to match what the P-state support | * CPPC is activated by default and to match what the P-state support | ||||
| * does. | * does. | ||||
| */ | */ | ||||
| SET_BITS_VALUE(data->request, AMD_CPPC_REQUEST_EPP_BITS, 0); | SET_BITS_VALUE(data->request, AMD_CPPC_REQUEST_EPP_BITS, 0); | ||||
| /* | |||||
| * Some CPUs advertise CPPC in CPUID but lack a _CPC object in the | |||||
| * DSDT, and their autonomous mode is broken. When the _CPC object is | |||||
| * present we use autonomous mode; otherwise we pin to the highest | |||||
| * performance level to avoid performance regressions. | |||||
| */ | |||||
| if (sc->cppc.ctx != NULL) | |||||
| /* 0 in "Desired Performance" is autonomous mode. */ | /* 0 in "Desired Performance" is autonomous mode. */ | ||||
| MPASS(highest_perf != 0); | |||||
| SET_BITS_VALUE(data->request, AMD_CPPC_REQUEST_DES_PERF_BITS, | SET_BITS_VALUE(data->request, AMD_CPPC_REQUEST_DES_PERF_BITS, | ||||
| 0); | |||||
| else | |||||
| SET_BITS_VALUE(data->request, AMD_CPPC_REQUEST_DES_PERF_BITS, | |||||
| highest_perf); | highest_perf); | ||||
| error = wrmsr_safe(MSR_AMD_CPPC_REQUEST, data->request); | error = wrmsr_safe(MSR_AMD_CPPC_REQUEST, data->request); | ||||
| if (error != 0) | if (error != 0) | ||||
| return; | return; | ||||
| data->res &= ~HWP_ERROR_CPPC_REQUEST_WRITE; | data->res &= ~HWP_ERROR_CPPC_REQUEST_WRITE; | ||||
| sc->cppc.request = data->request; | sc->cppc.request = data->request; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 135 Lines • ▼ Show 20 Lines | static const struct hwpstate_cpufreq_methods pstate_methods = { | ||||
| .type = hwpstate_type_pstate }; | .type = hwpstate_type_pstate }; | ||||
| static int | static int | ||||
| hwpstate_probe(device_t dev) | hwpstate_probe(device_t dev) | ||||
| { | { | ||||
| struct hwpstate_softc *sc; | struct hwpstate_softc *sc; | ||||
| sc = device_get_softc(dev); | sc = device_get_softc(dev); | ||||
| if (hwpstate_amd_cppc_enable && | if (hwpstate_amd_cppc_enable) | ||||
| (amd_extended_feature_extensions & AMDFEID_CPPC)) { | |||||
| sc->flags |= HWPFL_USE_CPPC; | sc->flags |= HWPFL_USE_CPPC; | ||||
| device_set_desc(dev, | |||||
| "AMD Collaborative Processor Performance Control (CPPC)"); | if ((hwpstate_amd_cppc_verify & HWPSTATE_AMD_CPPC_VERIFY_CPUID) != 0 && | ||||
| } else | (amd_extended_feature_extensions & AMDFEID_CPPC) == 0) | ||||
| device_set_desc(dev, "Cool`n'Quiet 2.0"); | sc->flags &= ~HWPFL_USE_CPPC; | ||||
| sc->dev = dev; | sc->dev = dev; | ||||
| sc->cpuid = cpu_get_pcpu(dev)->pc_cpuid; | sc->cpuid = cpu_get_pcpu(dev)->pc_cpuid; | ||||
| if ((sc->flags & HWPFL_USE_CPPC) != 0) { | if ((sc->flags & HWPFL_USE_CPPC) != 0) { | ||||
olce: I'd wrap the block around the ACPI check in a `if ((sc->flags & HWPFL_USE_CPPC) != 0)` to avoid… | |||||
Done Inline ActionsIt is required as we use this context to check if we need to enter autonomous mode. aokblast: It is required as we use this context to check if we need to enter autonomous mode. | |||||
| sc->cpufreq_methods = &cppc_methods; | sc->cpufreq_methods = &cppc_methods; | ||||
| device_set_desc(dev, | |||||
| "AMD Collaborative Processor Performance Control (CPPC)"); | |||||
| return (0); | return (0); | ||||
| } | } | ||||
| device_set_desc(dev, "Cool`n'Quiet 2.0"); | |||||
| sc->cpufreq_methods = &pstate_methods; | sc->cpufreq_methods = &pstate_methods; | ||||
| return (hwpstate_probe_pstate(dev)); | return (hwpstate_probe_pstate(dev)); | ||||
| } | } | ||||
| static int | static int | ||||
| hwpstate_attach(device_t dev) | hwpstate_attach(device_t dev) | ||||
| { | { | ||||
| struct hwpstate_softc *sc; | struct hwpstate_softc *sc; | ||||
| int res; | int res; | ||||
| sc = device_get_softc(dev); | sc = device_get_softc(dev); | ||||
| if ((sc->flags & HWPFL_USE_CPPC) != 0) { | if ((sc->flags & HWPFL_USE_CPPC) != 0) { | ||||
| /* | |||||
| * Some CPUs advertise CPPC in CPUID but lack a _CPC object in | |||||
| * the DSDT, and their autonomous mode is broken. By default, | |||||
| * require _CPC and fall back to P-state control when it is | |||||
| * absent. A NULL ctx with the ACPI check disabled keeps CPPC | |||||
| * enabled but pinned to the highest performance level (see | |||||
| * enable_cppc_cb()). | |||||
| */ | |||||
| sc->cppc.ctx = acpi_cppc_ctx_init(dev); | |||||
| if (sc->cppc.ctx == NULL && | |||||
| (hwpstate_amd_cppc_verify & HWPSTATE_AMD_CPPC_VERIFY_ACPI) != | |||||
| 0) { | |||||
| sc->flags &= ~HWPFL_USE_CPPC; | |||||
| sc->cpufreq_methods = &pstate_methods; | |||||
| device_set_desc(dev, "Cool`n'Quiet 2.0"); | |||||
| if ((res = hwpstate_probe_pstate(dev)) != 0) | |||||
| return (res); | |||||
| } | |||||
| } | |||||
| if ((sc->flags & HWPFL_USE_CPPC) != 0) { | |||||
| if ((res = enable_cppc(sc)) != 0) | if ((res = enable_cppc(sc)) != 0) | ||||
| return (res); | return (res); | ||||
| SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), | SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), | ||||
| SYSCTL_STATIC_CHILDREN(_debug), OID_AUTO, | SYSCTL_STATIC_CHILDREN(_debug), OID_AUTO, | ||||
| device_get_nameunit(dev), | device_get_nameunit(dev), | ||||
| CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, | CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, | ||||
| sc, 0, sysctl_cppc_dump_handler, "A", ""); | sc, 0, sysctl_cppc_dump_handler, "A", ""); | ||||
| ▲ Show 20 Lines • Show All 213 Lines • ▼ Show 20 Lines | |||||
| static int | static int | ||||
| hwpstate_detach(device_t dev) | hwpstate_detach(device_t dev) | ||||
| { | { | ||||
| struct hwpstate_softc *sc; | struct hwpstate_softc *sc; | ||||
| sc = device_get_softc(dev); | sc = device_get_softc(dev); | ||||
| if ((sc->flags & HWPFL_USE_CPPC) == 0) | if ((sc->flags & HWPFL_USE_CPPC) == 0) | ||||
| hwpstate_goto_pstate(dev, 0); | hwpstate_goto_pstate(dev, 0); | ||||
| else if (sc->cppc.ctx != NULL) | |||||
| acpi_cppc_ctx_free(dev, sc->cppc.ctx); | |||||
| return (cpufreq_unregister(dev)); | return (cpufreq_unregister(dev)); | ||||
| } | } | ||||
| static int | static int | ||||
| hwpstate_shutdown(device_t dev) | hwpstate_shutdown(device_t dev) | ||||
| { | { | ||||
| /* hwpstate_goto_pstate(dev, 0); */ | /* hwpstate_goto_pstate(dev, 0); */ | ||||
| Show All 11 Lines | |||||
I'd wrap the block around the ACPI check in a if ((sc->flags & HWPFL_USE_CPPC) != 0) to avoid calling acpi_cppc_ctx_init() at all if CPUID already indicates the feature is not supported. Doing so also delineates a bit more the assignment to sc->cppc.ctx, which wasn't that visible at first.