2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/globaldata.h>
40 #include <machine/md_var.h>
41 #include <machine/cpufunc.h>
42 #include <machine/cpufreq.h>
43 #include <machine/cputypes.h>
44 #include <machine/specialreg.h>
47 #include "acpi_cpu_pstate.h"
49 #define AMD_APMI_HWPSTATE 0x80
51 #define AMD_MSR_PSTATE_CSR_MASK 0x7ULL
52 #define AMD1X_MSR_PSTATE_CTL 0xc0010062
53 #define AMD1X_MSR_PSTATE_ST 0xc0010063
55 #define AMD_MSR_PSTATE_EN 0x8000000000000000ULL
57 #define AMD10_MSR_PSTATE_START 0xc0010064
58 #define AMD10_MSR_PSTATE_COUNT 5
59 #define AMD11_MSR_PSTATE_COUNT 8 /* starting from 11h */
61 #define AMD0F_PST_CTL_FID(cval) (((cval) >> 0) & 0x3f)
62 #define AMD0F_PST_CTL_VID(cval) (((cval) >> 6) & 0x1f)
63 #define AMD0F_PST_CTL_VST(cval) (((cval) >> 11) & 0x7f)
64 #define AMD0F_PST_CTL_MVS(cval) (((cval) >> 18) & 0x3)
65 #define AMD0F_PST_CTL_PLLTIME(cval) (((cval) >> 20) & 0x7f)
66 #define AMD0F_PST_CTL_RVO(cval) (((cval) >> 28) & 0x3)
67 #define AMD0F_PST_CTL_IRT(cval) (((cval) >> 30) & 0x3)
69 #define AMD0F_PST_ST_FID(sval) (((sval) >> 0) & 0x3f)
70 #define AMD0F_PST_ST_VID(sval) (((sval) >> 6) & 0x3f)
72 #define INTEL_MSR_MISC_ENABLE 0x1a0
73 #define INTEL_MSR_MISC_EST_EN 0x10000ULL
75 #define INTEL_MSR_PERF_STATUS 0x198
76 #define INTEL_MSR_PERF_CTL 0x199
77 #define INTEL_MSR_PERF_MASK 0xffffULL
79 static const struct acpi_pst_md *
80 acpi_pst_amd_probe(void);
81 static int acpi_pst_amd_check_csr(const struct acpi_pst_res *,
82 const struct acpi_pst_res *);
83 static int acpi_pst_amd1x_check_pstates(const struct acpi_pstate *, int,
85 static int acpi_pst_amd10_check_pstates(const struct acpi_pstate *, int);
86 static int acpi_pst_amd0f_check_pstates(const struct acpi_pstate *, int);
87 static int acpi_pst_amd_init(const struct acpi_pst_res *,
88 const struct acpi_pst_res *);
89 static int acpi_pst_amd1x_set_pstate(const struct acpi_pst_res *,
90 const struct acpi_pst_res *, const struct acpi_pstate *);
91 static int acpi_pst_amd0f_set_pstate(const struct acpi_pst_res *,
92 const struct acpi_pst_res *, const struct acpi_pstate *);
93 static const struct acpi_pstate *
94 acpi_pst_amd1x_get_pstate(const struct acpi_pst_res *,
95 const struct acpi_pstate *, int);
96 static const struct acpi_pstate *
97 acpi_pst_amd0f_get_pstate(const struct acpi_pst_res *,
98 const struct acpi_pstate *, int);
100 static const struct acpi_pst_md *
101 acpi_pst_intel_probe(void);
102 static int acpi_pst_intel_check_csr(const struct acpi_pst_res *,
103 const struct acpi_pst_res *);
104 static int acpi_pst_intel_check_pstates(const struct acpi_pstate *, int);
105 static int acpi_pst_intel_init(const struct acpi_pst_res *,
106 const struct acpi_pst_res *);
107 static int acpi_pst_intel_set_pstate(const struct acpi_pst_res *,
108 const struct acpi_pst_res *, const struct acpi_pstate *);
109 static const struct acpi_pstate *
110 acpi_pst_intel_get_pstate(const struct acpi_pst_res *,
111 const struct acpi_pstate *, int);
113 static int acpi_pst_md_gas_asz(const ACPI_GENERIC_ADDRESS *);
114 static int acpi_pst_md_gas_verify(const ACPI_GENERIC_ADDRESS *);
115 static uint32_t acpi_pst_md_res_read(const struct acpi_pst_res *);
116 static void acpi_pst_md_res_write(const struct acpi_pst_res *, uint32_t);
118 static const struct acpi_pst_md acpi_pst_amd10 = {
119 .pmd_check_csr = acpi_pst_amd_check_csr,
120 .pmd_check_pstates = acpi_pst_amd10_check_pstates,
121 .pmd_init = acpi_pst_amd_init,
122 .pmd_set_pstate = acpi_pst_amd1x_set_pstate,
123 .pmd_get_pstate = acpi_pst_amd1x_get_pstate
126 static const struct acpi_pst_md acpi_pst_amd0f = {
127 .pmd_check_csr = acpi_pst_amd_check_csr,
128 .pmd_check_pstates = acpi_pst_amd0f_check_pstates,
129 .pmd_init = acpi_pst_amd_init,
130 .pmd_set_pstate = acpi_pst_amd0f_set_pstate,
131 .pmd_get_pstate = acpi_pst_amd0f_get_pstate
134 static const struct acpi_pst_md acpi_pst_intel = {
135 .pmd_check_csr = acpi_pst_intel_check_csr,
136 .pmd_check_pstates = acpi_pst_intel_check_pstates,
137 .pmd_init = acpi_pst_intel_init,
138 .pmd_set_pstate = acpi_pst_intel_set_pstate,
139 .pmd_get_pstate = acpi_pst_intel_get_pstate
142 static int acpi_pst_stringent_check = 1;
143 TUNABLE_INT("hw.acpi.cpu.pstate.strigent_check", &acpi_pst_stringent_check);
145 static int acpi_pst_amd1x_msr_pstate_count = AMD10_MSR_PSTATE_COUNT;
147 const struct acpi_pst_md *
148 acpi_pst_md_probe(void)
150 if (cpu_vendor_id == CPU_VENDOR_AMD)
151 return acpi_pst_amd_probe();
152 else if (cpu_vendor_id == CPU_VENDOR_INTEL)
153 return acpi_pst_intel_probe();
157 static const struct acpi_pst_md *
158 acpi_pst_amd_probe(void)
162 /* Only Family >= 0fh has P-State support */
163 if (CPUID_TO_FAMILY(cpu_id) < 0xf)
166 /* Check whether APMI exists */
167 if (cpu_exthigh < 0x80000007)
171 do_cpuid(0x80000007, regs);
173 if (CPUID_TO_FAMILY(cpu_id) == 0xf) { /* Family 0fh */
174 if ((regs[3] & 0x06) == 0x06)
175 return &acpi_pst_amd0f;
176 } else if (CPUID_TO_FAMILY(cpu_id) >= 0x10) { /* Family >= 10h */
177 if (CPUID_TO_FAMILY(cpu_id) >= 0x11) {
178 acpi_pst_amd1x_msr_pstate_count =
179 AMD11_MSR_PSTATE_COUNT;
182 return &acpi_pst_amd10;
188 acpi_pst_amd_check_csr(const struct acpi_pst_res *ctrl,
189 const struct acpi_pst_res *status)
191 if (ctrl->pr_gas.SpaceId != ACPI_ADR_SPACE_FIXED_HARDWARE) {
192 kprintf("cpu%d: Invalid P-State control register\n", mycpuid);
195 if (status->pr_gas.SpaceId != ACPI_ADR_SPACE_FIXED_HARDWARE) {
196 kprintf("cpu%d: Invalid P-State status register\n", mycpuid);
203 acpi_pst_amd1x_check_pstates(const struct acpi_pstate *pstates, int npstates,
204 uint32_t msr_start, uint32_t msr_end)
209 * Make sure that related MSR P-State registers are enabled.
212 * We don't check status register value here;
213 * it will not be used.
215 for (i = 0; i < npstates; ++i) {
220 (pstates[i].st_cval & AMD_MSR_PSTATE_CSR_MASK);
221 if (msr >= msr_end) {
222 kprintf("cpu%d: MSR P-State register %#08x "
223 "does not exist\n", mycpuid, msr);
228 if ((pstate & AMD_MSR_PSTATE_EN) == 0) {
229 kprintf("cpu%d: MSR P-State register %#08x "
230 "is not enabled\n", mycpuid, msr);
238 acpi_pst_amd10_check_pstates(const struct acpi_pstate *pstates, int npstates)
240 if (npstates > acpi_pst_amd1x_msr_pstate_count) {
241 kprintf("cpu%d: only %d P-states are allowed\n", mycpuid,
242 acpi_pst_amd1x_msr_pstate_count);
246 return acpi_pst_amd1x_check_pstates(pstates, npstates,
247 AMD10_MSR_PSTATE_START,
248 AMD10_MSR_PSTATE_START + AMD10_MSR_PSTATE_COUNT);
252 acpi_pst_amd1x_set_pstate(const struct acpi_pst_res *ctrl __unused,
253 const struct acpi_pst_res *status __unused,
254 const struct acpi_pstate *pstate)
258 cval = pstate->st_cval & AMD_MSR_PSTATE_CSR_MASK;
259 wrmsr(AMD1X_MSR_PSTATE_CTL, cval);
262 * Don't check AMD1X_MSR_PSTATE_ST here, since it is
263 * affected by various P-State limits.
266 * AMD Family 10h Processor BKDG Rev 3.20 (#31116)
267 * 2.4.2.4 P-state Transition Behavior
273 static const struct acpi_pstate *
274 acpi_pst_amd1x_get_pstate(const struct acpi_pst_res *status __unused,
275 const struct acpi_pstate *pstates, int npstates)
280 sval = rdmsr(AMD1X_MSR_PSTATE_ST) & AMD_MSR_PSTATE_CSR_MASK;
281 for (i = 0; i < npstates; ++i) {
282 if ((pstates[i].st_sval & AMD_MSR_PSTATE_CSR_MASK) == sval)
289 acpi_pst_amd0f_check_pstates(const struct acpi_pstate *pstates, int npstates)
291 struct amd0f_fidvid fv_max, fv_min;
294 amd0f_fidvid_limit(&fv_min, &fv_max);
296 if (fv_min.fid == fv_max.fid && fv_min.vid == fv_max.vid) {
297 kprintf("cpu%d: only one P-State is supported\n", mycpuid);
298 if (acpi_pst_stringent_check)
302 for (i = 0; i < npstates; ++i) {
303 const struct acpi_pstate *p = &pstates[i];
304 uint32_t fid, vid, mvs, rvo;
307 fid = AMD0F_PST_CTL_FID(p->st_cval);
308 vid = AMD0F_PST_CTL_VID(p->st_cval);
311 if (vid != fv_max.vid) {
312 kprintf("cpu%d: max VID mismatch "
313 "real %u, lim %d\n", mycpuid,
316 if (fid != fv_max.fid) {
317 kprintf("cpu%d: max FID mismatch "
318 "real %u, lim %d\n", mycpuid,
321 } else if (i == npstates - 1) {
322 if (vid != fv_min.vid) {
323 kprintf("cpu%d: min VID mismatch "
324 "real %u, lim %d\n", mycpuid,
327 if (fid != fv_min.fid) {
328 kprintf("cpu%d: min FID mismatch "
329 "real %u, lim %d\n", mycpuid,
333 if (fid >= fv_max.fid || fid < (fv_min.fid + 0x8)) {
334 kprintf("cpu%d: Invalid FID %#x, "
335 "out [%#x, %#x]\n", mycpuid, fid,
336 fv_min.fid + 0x8, fv_max.fid);
337 if (acpi_pst_stringent_check)
340 if (vid < fv_max.vid || vid > fv_min.vid) {
341 kprintf("cpu%d: Invalid VID %#x, "
342 "in [%#x, %#x]\n", mycpuid, vid,
343 fv_max.vid, fv_min.vid);
344 if (acpi_pst_stringent_check)
349 mvs = AMD0F_PST_CTL_MVS(p->st_cval);
350 rvo = AMD0F_PST_CTL_RVO(p->st_cval);
352 /* Only 0 is allowed, i.e. 25mV stepping */
354 kprintf("cpu%d: Invalid MVS %#x\n", mycpuid, mvs);
359 mvs_mv = 25 * (1 << mvs);
361 if (rvo_mv % mvs_mv != 0) {
362 kprintf("cpu%d: Invalid MVS/RVO (%#x/%#x)\n",
371 acpi_pst_amd0f_set_pstate(const struct acpi_pst_res *ctrl __unused,
372 const struct acpi_pst_res *status __unused,
373 const struct acpi_pstate *pstate)
375 struct amd0f_fidvid fv;
376 struct amd0f_xsit xsit;
378 fv.fid = AMD0F_PST_CTL_FID(pstate->st_cval);
379 fv.vid = AMD0F_PST_CTL_VID(pstate->st_cval);
381 xsit.rvo = AMD0F_PST_CTL_RVO(pstate->st_cval);
382 xsit.mvs = AMD0F_PST_CTL_MVS(pstate->st_cval);
383 xsit.vst = AMD0F_PST_CTL_VST(pstate->st_cval);
384 xsit.pll_time = AMD0F_PST_CTL_PLLTIME(pstate->st_cval);
385 xsit.irt = AMD0F_PST_CTL_IRT(pstate->st_cval);
387 return amd0f_set_fidvid(&fv, &xsit);
390 static const struct acpi_pstate *
391 acpi_pst_amd0f_get_pstate(const struct acpi_pst_res *status __unused,
392 const struct acpi_pstate *pstates, int npstates)
394 struct amd0f_fidvid fv;
397 error = amd0f_get_fidvid(&fv);
401 for (i = 0; i < npstates; ++i) {
402 const struct acpi_pstate *p = &pstates[i];
404 if (fv.fid == AMD0F_PST_ST_FID(p->st_sval) &&
405 fv.vid == AMD0F_PST_ST_VID(p->st_sval))
412 acpi_pst_amd_init(const struct acpi_pst_res *ctrl __unused,
413 const struct acpi_pst_res *status __unused)
418 static const struct acpi_pst_md *
419 acpi_pst_intel_probe(void)
421 if ((cpu_feature2 & CPUID2_EST) == 0)
424 if (CPUID_TO_FAMILY(cpu_id) >= 0xf || CPUID_TO_FAMILY(cpu_id) == 0x6)
425 return &acpi_pst_intel;
431 acpi_pst_intel_check_csr(const struct acpi_pst_res *ctrl,
432 const struct acpi_pst_res *status)
436 if (ctrl->pr_gas.SpaceId != status->pr_gas.SpaceId) {
437 kprintf("cpu%d: P-State control(%d)/status(%d) registers have "
438 "different SpaceId", mycpuid,
439 ctrl->pr_gas.SpaceId, status->pr_gas.SpaceId);
443 switch (ctrl->pr_gas.SpaceId) {
444 case ACPI_ADR_SPACE_FIXED_HARDWARE:
445 if (ctrl->pr_res != NULL || status->pr_res != NULL) {
446 /* XXX should panic() */
447 kprintf("cpu%d: Allocated resource for fixed hardware "
448 "registers\n", mycpuid);
453 case ACPI_ADR_SPACE_SYSTEM_IO:
454 if (ctrl->pr_res == NULL) {
455 kprintf("cpu%d: ioport allocation failed for control "
456 "register\n", mycpuid);
459 error = acpi_pst_md_gas_verify(&ctrl->pr_gas);
461 kprintf("cpu%d: Invalid control register GAS\n",
466 if (status->pr_res == NULL) {
467 kprintf("cpu%d: ioport allocation failed for status "
468 "register\n", mycpuid);
471 error = acpi_pst_md_gas_verify(&status->pr_gas);
473 kprintf("cpu%d: Invalid status register GAS\n",
480 kprintf("cpu%d: Invalid P-State control/status register "
481 "SpaceId %d\n", mycpuid, ctrl->pr_gas.SpaceId);
488 acpi_pst_intel_check_pstates(const struct acpi_pstate *pstates __unused,
489 int npstates __unused)
495 acpi_pst_intel_init(const struct acpi_pst_res *ctrl __unused,
496 const struct acpi_pst_res *status __unused)
498 uint64_t misc_enable;
500 if (CPUID_TO_FAMILY(cpu_id) == 0xf ||
501 (CPUID_TO_FAMILY(cpu_id) == 0x6 && CPUID_TO_MODEL(cpu_id) < 0xd)) {
502 /* EST enable bit is reserved in INTEL_MSR_MISC_ENABLE */
506 misc_enable = rdmsr(INTEL_MSR_MISC_ENABLE);
507 if ((misc_enable & INTEL_MSR_MISC_EST_EN) == 0) {
508 misc_enable |= INTEL_MSR_MISC_EST_EN;
509 wrmsr(INTEL_MSR_MISC_ENABLE, misc_enable);
511 misc_enable = rdmsr(INTEL_MSR_MISC_ENABLE);
512 if ((misc_enable & INTEL_MSR_MISC_EST_EN) == 0) {
513 kprintf("cpu%d: Can't enable EST\n", mycpuid);
521 acpi_pst_intel_set_pstate(const struct acpi_pst_res *ctrl,
522 const struct acpi_pst_res *status __unused,
523 const struct acpi_pstate *pstate)
525 if (ctrl->pr_res != NULL) {
526 acpi_pst_md_res_write(ctrl, pstate->st_cval);
530 ctl = rdmsr(INTEL_MSR_PERF_CTL);
531 ctl &= ~INTEL_MSR_PERF_MASK;
532 ctl |= (pstate->st_cval & INTEL_MSR_PERF_MASK);
533 wrmsr(INTEL_MSR_PERF_CTL, ctl);
538 static const struct acpi_pstate *
539 acpi_pst_intel_get_pstate(const struct acpi_pst_res *status,
540 const struct acpi_pstate *pstates, int npstates)
544 if (status->pr_res != NULL) {
547 st = acpi_pst_md_res_read(status);
548 for (i = 0; i < npstates; ++i) {
549 if (pstates[i].st_sval == st)
555 sval = rdmsr(INTEL_MSR_PERF_STATUS) & INTEL_MSR_PERF_MASK;
556 for (i = 0; i < npstates; ++i) {
557 if ((pstates[i].st_sval & INTEL_MSR_PERF_MASK) == sval)
565 acpi_pst_md_gas_asz(const ACPI_GENERIC_ADDRESS *gas)
569 if (gas->AccessWidth != 0)
570 asz = gas->AccessWidth;
572 asz = gas->BitWidth / NBBY;
586 acpi_pst_md_gas_verify(const ACPI_GENERIC_ADDRESS *gas)
590 if (gas->BitOffset % NBBY != 0)
593 end = gas->BitWidth / NBBY;
594 reg = gas->BitOffset / NBBY;
599 asz = acpi_pst_md_gas_asz(gas);
609 acpi_pst_md_res_read(const struct acpi_pst_res *res)
613 KKASSERT(res->pr_res != NULL);
614 asz = acpi_pst_md_gas_asz(&res->pr_gas);
615 reg = res->pr_gas.BitOffset / NBBY;
619 return bus_space_read_1(res->pr_bt, res->pr_bh, reg);
621 return bus_space_read_2(res->pr_bt, res->pr_bh, reg);
623 return bus_space_read_4(res->pr_bt, res->pr_bh, reg);
625 panic("unsupported access width %d", asz);
632 acpi_pst_md_res_write(const struct acpi_pst_res *res, uint32_t val)
636 KKASSERT(res->pr_res != NULL);
637 asz = acpi_pst_md_gas_asz(&res->pr_gas);
638 reg = res->pr_gas.BitOffset / NBBY;
642 bus_space_write_1(res->pr_bt, res->pr_bh, reg, val);
645 bus_space_write_2(res->pr_bt, res->pr_bh, reg, val);
648 bus_space_write_4(res->pr_bt, res->pr_bh, reg, val);
651 panic("unsupported access width %d", asz);