acpi_cpu_md.c revision 1.66 1 /* $NetBSD: acpi_cpu_md.c,v 1.66 2011/09/24 11:17:25 jruoho Exp $ */
2
3 /*-
4 * Copyright (c) 2010, 2011 Jukka Ruohonen <jruohonen (at) iki.fi>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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 the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: acpi_cpu_md.c,v 1.66 2011/09/24 11:17:25 jruoho Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/kcore.h>
36 #include <sys/sysctl.h>
37 #include <sys/xcall.h>
38
39 #include <x86/cpu.h>
40 #include <x86/cpufunc.h>
41 #include <x86/cputypes.h>
42 #include <x86/cpuvar.h>
43 #include <x86/cpu_msr.h>
44 #include <x86/machdep.h>
45
46 #include <dev/acpi/acpica.h>
47 #include <dev/acpi/acpi_cpu.h>
48
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pcidevs.h>
51
52 #include <machine/acpi_machdep.h>
53
54 /*
55 * Intel IA32_MISC_ENABLE.
56 */
57 #define MSR_MISC_ENABLE_EST __BIT(16)
58 #define MSR_MISC_ENABLE_TURBO __BIT(38)
59
60 /*
61 * AMD C1E.
62 */
63 #define MSR_CMPHALT 0xc0010055
64
65 #define MSR_CMPHALT_SMI __BIT(27)
66 #define MSR_CMPHALT_C1E __BIT(28)
67 #define MSR_CMPHALT_BMSTS __BIT(29)
68
69 /*
70 * AMD families 10h, 11h, and 14h
71 */
72 #define MSR_10H_LIMIT 0xc0010061
73 #define MSR_10H_CONTROL 0xc0010062
74 #define MSR_10H_STATUS 0xc0010063
75 #define MSR_10H_CONFIG 0xc0010064
76
77 /*
78 * AMD family 0Fh.
79 */
80 #define MSR_0FH_CONTROL 0xc0010041
81 #define MSR_0FH_STATUS 0xc0010042
82
83 #define MSR_0FH_STATUS_CFID __BITS( 0, 5)
84 #define MSR_0FH_STATUS_CVID __BITS(32, 36)
85 #define MSR_0FH_STATUS_PENDING __BITS(31, 31)
86
87 #define MSR_0FH_CONTROL_FID __BITS( 0, 5)
88 #define MSR_0FH_CONTROL_VID __BITS( 8, 12)
89 #define MSR_0FH_CONTROL_CHG __BITS(16, 16)
90 #define MSR_0FH_CONTROL_CNT __BITS(32, 51)
91
92 #define ACPI_0FH_STATUS_FID __BITS( 0, 5)
93 #define ACPI_0FH_STATUS_VID __BITS( 6, 10)
94
95 #define ACPI_0FH_CONTROL_FID __BITS( 0, 5)
96 #define ACPI_0FH_CONTROL_VID __BITS( 6, 10)
97 #define ACPI_0FH_CONTROL_VST __BITS(11, 17)
98 #define ACPI_0FH_CONTROL_MVS __BITS(18, 19)
99 #define ACPI_0FH_CONTROL_PLL __BITS(20, 26)
100 #define ACPI_0FH_CONTROL_RVO __BITS(28, 29)
101 #define ACPI_0FH_CONTROL_IRT __BITS(30, 31)
102
103 #define FID_TO_VCO_FID(fidd) (((fid) < 8) ? (8 + ((fid) << 1)) : (fid))
104
105 static char native_idle_text[16];
106 void (*native_idle)(void) = NULL;
107
108 static int acpicpu_md_quirk_piix4(const struct pci_attach_args *);
109 static void acpicpu_md_pstate_hwf_reset(void *, void *);
110 static int acpicpu_md_pstate_fidvid_get(struct acpicpu_softc *,
111 uint32_t *);
112 static int acpicpu_md_pstate_fidvid_set(struct acpicpu_pstate *);
113 static int acpicpu_md_pstate_fidvid_read(uint32_t *, uint32_t *);
114 static void acpicpu_md_pstate_fidvid_write(uint32_t, uint32_t,
115 uint32_t, uint32_t);
116 static int acpicpu_md_pstate_sysctl_init(void);
117 static int acpicpu_md_pstate_sysctl_get(SYSCTLFN_PROTO);
118 static int acpicpu_md_pstate_sysctl_set(SYSCTLFN_PROTO);
119 static int acpicpu_md_pstate_sysctl_all(SYSCTLFN_PROTO);
120
121 extern struct acpicpu_softc **acpicpu_sc;
122 static struct sysctllog *acpicpu_log = NULL;
123
124 struct cpu_info *
125 acpicpu_md_match(device_t parent, cfdata_t match, void *aux)
126 {
127 struct cpufeature_attach_args *cfaa = aux;
128
129 if (strcmp(cfaa->name, "frequency") != 0)
130 return NULL;
131
132 return cfaa->ci;
133 }
134
135 struct cpu_info *
136 acpicpu_md_attach(device_t parent, device_t self, void *aux)
137 {
138 struct cpufeature_attach_args *cfaa = aux;
139
140 return cfaa->ci;
141 }
142
143 uint32_t
144 acpicpu_md_flags(void)
145 {
146 struct cpu_info *ci = curcpu();
147 struct pci_attach_args pa;
148 uint32_t family, val = 0;
149 uint32_t regs[4];
150 uint64_t msr;
151
152 if (acpi_md_ncpus() == 1)
153 val |= ACPICPU_FLAG_C_BM;
154
155 if ((ci->ci_feat_val[1] & CPUID2_MONITOR) != 0)
156 val |= ACPICPU_FLAG_C_FFH;
157
158 /*
159 * By default, assume that the local APIC timer
160 * as well as TSC are stalled during C3 sleep.
161 */
162 val |= ACPICPU_FLAG_C_APIC | ACPICPU_FLAG_C_TSC;
163
164 switch (cpu_vendor) {
165
166 case CPUVENDOR_IDT:
167
168 if ((ci->ci_feat_val[1] & CPUID2_EST) != 0)
169 val |= ACPICPU_FLAG_P_FFH;
170
171 if ((ci->ci_feat_val[0] & CPUID_ACPI) != 0)
172 val |= ACPICPU_FLAG_T_FFH;
173
174 break;
175
176 case CPUVENDOR_INTEL:
177
178 /*
179 * Bus master control and arbitration should be
180 * available on all supported Intel CPUs (to be
181 * sure, this is double-checked later from the
182 * firmware data). These flags imply that it is
183 * not necessary to flush caches before C3 state.
184 */
185 val |= ACPICPU_FLAG_C_BM | ACPICPU_FLAG_C_ARB;
186
187 /*
188 * Check if we can use "native", MSR-based,
189 * access. If not, we have to resort to I/O.
190 */
191 if ((ci->ci_feat_val[1] & CPUID2_EST) != 0)
192 val |= ACPICPU_FLAG_P_FFH;
193
194 if ((ci->ci_feat_val[0] & CPUID_ACPI) != 0)
195 val |= ACPICPU_FLAG_T_FFH;
196
197 /*
198 * Check whether MSR_APERF, MSR_MPERF, and Turbo
199 * Boost are available. Also see if we might have
200 * an invariant local APIC timer ("ARAT").
201 */
202 if (cpuid_level >= 0x06) {
203
204 x86_cpuid(0x00000006, regs);
205
206 if ((regs[2] & CPUID_DSPM_HWF) != 0)
207 val |= ACPICPU_FLAG_P_HWF;
208
209 if ((regs[0] & CPUID_DSPM_IDA) != 0)
210 val |= ACPICPU_FLAG_P_TURBO;
211
212 if ((regs[0] & CPUID_DSPM_ARAT) != 0)
213 val &= ~ACPICPU_FLAG_C_APIC;
214 }
215
216 /*
217 * Detect whether TSC is invariant. If it is not,
218 * we keep the flag to note that TSC will not run
219 * at constant rate. Depending on the CPU, this may
220 * affect P- and T-state changes, but especially
221 * relevant are C-states; with variant TSC, states
222 * larger than C1 may completely stop the counter.
223 */
224 x86_cpuid(0x80000000, regs);
225
226 if (regs[0] >= 0x80000007) {
227
228 x86_cpuid(0x80000007, regs);
229
230 if ((regs[3] & __BIT(8)) != 0)
231 val &= ~ACPICPU_FLAG_C_TSC;
232 }
233
234 break;
235
236 case CPUVENDOR_AMD:
237
238 x86_cpuid(0x80000000, regs);
239
240 if (regs[0] < 0x80000007)
241 break;
242
243 x86_cpuid(0x80000007, regs);
244
245 family = CPUID2FAMILY(ci->ci_signature);
246
247 if (family == 0xf)
248 family += CPUID2EXTFAMILY(ci->ci_signature);
249
250 switch (family) {
251
252 case 0x0f:
253
254 /*
255 * Evaluate support for the "FID/VID
256 * algorithm" also used by powernow(4).
257 */
258 if ((regs[3] & CPUID_APM_FID) == 0)
259 break;
260
261 if ((regs[3] & CPUID_APM_VID) == 0)
262 break;
263
264 val |= ACPICPU_FLAG_P_FFH | ACPICPU_FLAG_P_FIDVID;
265 break;
266
267 case 0x10:
268 case 0x11:
269
270 if (rdmsr_safe(MSR_CMPHALT, &msr) != EFAULT)
271 val |= ACPICPU_FLAG_C_C1E;
272
273 /* FALLTHROUGH */
274
275 case 0x14: /* AMD Fusion */
276
277 /*
278 * Like with Intel, detect invariant TSC,
279 * MSR-based P-states, and AMD's "turbo"
280 * (Core Performance Boost), respectively.
281 */
282 if ((regs[3] & CPUID_APM_TSC) != 0)
283 val &= ~ACPICPU_FLAG_C_TSC;
284
285 if ((regs[3] & CPUID_APM_HWP) != 0)
286 val |= ACPICPU_FLAG_P_FFH;
287
288 if ((regs[3] & CPUID_APM_CPB) != 0)
289 val |= ACPICPU_FLAG_P_TURBO;
290
291 /*
292 * Also check for APERF and MPERF,
293 * first available in the family 10h.
294 */
295 if (cpuid_level >= 0x06) {
296
297 x86_cpuid(0x00000006, regs);
298
299 if ((regs[2] & CPUID_DSPM_HWF) != 0)
300 val |= ACPICPU_FLAG_P_HWF;
301 }
302
303 break;
304 }
305
306 break;
307 }
308
309 /*
310 * There are several erratums for PIIX4.
311 */
312 if (pci_find_device(&pa, acpicpu_md_quirk_piix4) != 0)
313 val |= ACPICPU_FLAG_PIIX4;
314
315 return val;
316 }
317
318 static int
319 acpicpu_md_quirk_piix4(const struct pci_attach_args *pa)
320 {
321
322 /*
323 * XXX: The pci_find_device(9) function only
324 * deals with attached devices. Change this
325 * to use something like pci_device_foreach().
326 */
327 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
328 return 0;
329
330 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82371AB_ISA ||
331 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82440MX_PMC)
332 return 1;
333
334 return 0;
335 }
336
337 void
338 acpicpu_md_quirk_c1e(void)
339 {
340 const uint64_t c1e = MSR_CMPHALT_SMI | MSR_CMPHALT_C1E;
341 uint64_t val;
342
343 val = rdmsr(MSR_CMPHALT);
344
345 if ((val & c1e) != 0)
346 wrmsr(MSR_CMPHALT, val & ~c1e);
347 }
348
349 int
350 acpicpu_md_cstate_start(struct acpicpu_softc *sc)
351 {
352 const size_t size = sizeof(native_idle_text);
353 struct acpicpu_cstate *cs;
354 bool ipi = false;
355 int i;
356
357 /*
358 * Save the cpu_idle(9) loop used by default.
359 */
360 x86_cpu_idle_get(&native_idle, native_idle_text, size);
361
362 for (i = 0; i < ACPI_C_STATE_COUNT; i++) {
363
364 cs = &sc->sc_cstate[i];
365
366 if (cs->cs_method == ACPICPU_C_STATE_HALT) {
367 ipi = true;
368 break;
369 }
370 }
371
372 x86_cpu_idle_set(acpicpu_cstate_idle, "acpi", ipi);
373
374 return 0;
375 }
376
377 int
378 acpicpu_md_cstate_stop(void)
379 {
380 static char text[16];
381 void (*func)(void);
382 uint64_t xc;
383 bool ipi;
384
385 x86_cpu_idle_get(&func, text, sizeof(text));
386
387 if (func == native_idle)
388 return EALREADY;
389
390 ipi = (native_idle != x86_cpu_idle_halt) ? false : true;
391 x86_cpu_idle_set(native_idle, native_idle_text, ipi);
392
393 /*
394 * Run a cross-call to ensure that all CPUs are
395 * out from the ACPI idle-loop before detachment.
396 */
397 xc = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
398 xc_wait(xc);
399
400 return 0;
401 }
402
403 /*
404 * Called with interrupts enabled.
405 */
406 void
407 acpicpu_md_cstate_enter(int method, int state)
408 {
409 struct cpu_info *ci = curcpu();
410
411 KASSERT(ci->ci_ilevel == IPL_NONE);
412
413 switch (method) {
414
415 case ACPICPU_C_STATE_FFH:
416
417 x86_monitor(&ci->ci_want_resched, 0, 0);
418
419 if (__predict_false(ci->ci_want_resched != 0))
420 return;
421
422 x86_mwait((state - 1) << 4, 0);
423 break;
424
425 case ACPICPU_C_STATE_HALT:
426
427 x86_disable_intr();
428
429 if (__predict_false(ci->ci_want_resched != 0)) {
430 x86_enable_intr();
431 return;
432 }
433
434 x86_stihlt();
435 break;
436 }
437 }
438
439 int
440 acpicpu_md_pstate_start(struct acpicpu_softc *sc)
441 {
442 uint64_t xc, val;
443
444 switch (cpu_vendor) {
445
446 case CPUVENDOR_IDT:
447 case CPUVENDOR_INTEL:
448
449 /*
450 * Make sure EST is enabled.
451 */
452 if ((sc->sc_flags & ACPICPU_FLAG_P_FFH) != 0) {
453
454 val = rdmsr(MSR_MISC_ENABLE);
455
456 if ((val & MSR_MISC_ENABLE_EST) == 0) {
457
458 val |= MSR_MISC_ENABLE_EST;
459 wrmsr(MSR_MISC_ENABLE, val);
460 val = rdmsr(MSR_MISC_ENABLE);
461
462 if ((val & MSR_MISC_ENABLE_EST) == 0)
463 return ENOTTY;
464 }
465 }
466 }
467
468 /*
469 * Reset the APERF and MPERF counters.
470 */
471 if ((sc->sc_flags & ACPICPU_FLAG_P_HWF) != 0) {
472 xc = xc_broadcast(0, acpicpu_md_pstate_hwf_reset, NULL, NULL);
473 xc_wait(xc);
474 }
475
476 return acpicpu_md_pstate_sysctl_init();
477 }
478
479 int
480 acpicpu_md_pstate_stop(void)
481 {
482
483 if (acpicpu_log == NULL)
484 return EALREADY;
485
486 sysctl_teardown(&acpicpu_log);
487 acpicpu_log = NULL;
488
489 return 0;
490 }
491
492 int
493 acpicpu_md_pstate_init(struct acpicpu_softc *sc)
494 {
495 struct cpu_info *ci = sc->sc_ci;
496 struct acpicpu_pstate *ps, msr;
497 uint32_t family, i = 0;
498
499 (void)memset(&msr, 0, sizeof(struct acpicpu_pstate));
500
501 switch (cpu_vendor) {
502
503 case CPUVENDOR_IDT:
504 case CPUVENDOR_INTEL:
505
506 /*
507 * If the so-called Turbo Boost is present,
508 * the P0-state is always the "turbo state".
509 * It is shown as the P1 frequency + 1 MHz.
510 *
511 * For discussion, see:
512 *
513 * Intel Corporation: Intel Turbo Boost Technology
514 * in Intel Core(tm) Microarchitectures (Nehalem)
515 * Based Processors. White Paper, November 2008.
516 */
517 if (sc->sc_pstate_count >= 2 &&
518 (sc->sc_flags & ACPICPU_FLAG_P_TURBO) != 0) {
519
520 ps = &sc->sc_pstate[0];
521
522 if (ps->ps_freq == sc->sc_pstate[1].ps_freq + 1)
523 ps->ps_flags |= ACPICPU_FLAG_P_TURBO;
524 }
525
526 msr.ps_control_addr = MSR_PERF_CTL;
527 msr.ps_control_mask = __BITS(0, 15);
528
529 msr.ps_status_addr = MSR_PERF_STATUS;
530 msr.ps_status_mask = __BITS(0, 15);
531 break;
532
533 case CPUVENDOR_AMD:
534
535 if ((sc->sc_flags & ACPICPU_FLAG_P_FIDVID) != 0)
536 msr.ps_flags |= ACPICPU_FLAG_P_FIDVID;
537
538 family = CPUID2FAMILY(ci->ci_signature);
539
540 if (family == 0xf)
541 family += CPUID2EXTFAMILY(ci->ci_signature);
542
543 switch (family) {
544
545 case 0x0f:
546 msr.ps_control_addr = MSR_0FH_CONTROL;
547 msr.ps_status_addr = MSR_0FH_STATUS;
548 break;
549
550 case 0x10:
551 case 0x11:
552 case 0x14: /* AMD Fusion */
553 msr.ps_control_addr = MSR_10H_CONTROL;
554 msr.ps_control_mask = __BITS(0, 2);
555
556 msr.ps_status_addr = MSR_10H_STATUS;
557 msr.ps_status_mask = __BITS(0, 2);
558 break;
559
560 default:
561 /*
562 * If we have an unknown AMD CPU, rely on XPSS.
563 */
564 if ((sc->sc_flags & ACPICPU_FLAG_P_XPSS) == 0)
565 return EOPNOTSUPP;
566 }
567
568 break;
569
570 default:
571 return ENODEV;
572 }
573
574 /*
575 * Fill the P-state structures with MSR addresses that are
576 * known to be correct. If we do not know the addresses,
577 * leave the values intact. If a vendor uses XPSS, we do
578 * not necessarily need to do anything to support new CPUs.
579 */
580 while (i < sc->sc_pstate_count) {
581
582 ps = &sc->sc_pstate[i];
583
584 if (msr.ps_flags != 0)
585 ps->ps_flags |= msr.ps_flags;
586
587 if (msr.ps_status_addr != 0)
588 ps->ps_status_addr = msr.ps_status_addr;
589
590 if (msr.ps_status_mask != 0)
591 ps->ps_status_mask = msr.ps_status_mask;
592
593 if (msr.ps_control_addr != 0)
594 ps->ps_control_addr = msr.ps_control_addr;
595
596 if (msr.ps_control_mask != 0)
597 ps->ps_control_mask = msr.ps_control_mask;
598
599 i++;
600 }
601
602 return 0;
603 }
604
605 /*
606 * Read the IA32_APERF and IA32_MPERF counters. The first
607 * increments at the rate of the fixed maximum frequency
608 * configured during the boot, whereas APERF counts at the
609 * rate of the actual frequency. Note that the MSRs must be
610 * read without delay, and that only the ratio between
611 * IA32_APERF and IA32_MPERF is architecturally defined.
612 *
613 * The function thus returns the percentage of the actual
614 * frequency in terms of the maximum frequency of the calling
615 * CPU since the last call. A value zero implies an error.
616 *
617 * For further details, refer to:
618 *
619 * Intel Corporation: Intel 64 and IA-32 Architectures
620 * Software Developer's Manual. Section 13.2, Volume 3A:
621 * System Programming Guide, Part 1. July, 2008.
622 *
623 * Advanced Micro Devices: BIOS and Kernel Developer's
624 * Guide (BKDG) for AMD Family 10h Processors. Section
625 * 2.4.5, Revision 3.48, April 2010.
626 */
627 uint8_t
628 acpicpu_md_pstate_hwf(struct cpu_info *ci)
629 {
630 struct acpicpu_softc *sc;
631 uint64_t aperf, mperf;
632 uint8_t rv = 0;
633
634 sc = acpicpu_sc[ci->ci_acpiid];
635
636 if (__predict_false(sc == NULL))
637 return 0;
638
639 if (__predict_false((sc->sc_flags & ACPICPU_FLAG_P_HWF) == 0))
640 return 0;
641
642 aperf = sc->sc_pstate_aperf;
643 mperf = sc->sc_pstate_mperf;
644
645 x86_disable_intr();
646
647 sc->sc_pstate_aperf = rdmsr(MSR_APERF);
648 sc->sc_pstate_mperf = rdmsr(MSR_MPERF);
649
650 x86_enable_intr();
651
652 aperf = sc->sc_pstate_aperf - aperf;
653 mperf = sc->sc_pstate_mperf - mperf;
654
655 if (__predict_true(mperf != 0))
656 rv = (aperf * 100) / mperf;
657
658 return rv;
659 }
660
661 static void
662 acpicpu_md_pstate_hwf_reset(void *arg1, void *arg2)
663 {
664 struct cpu_info *ci = curcpu();
665 struct acpicpu_softc *sc;
666
667 sc = acpicpu_sc[ci->ci_acpiid];
668
669 if (__predict_false(sc == NULL))
670 return;
671
672 x86_disable_intr();
673
674 wrmsr(MSR_APERF, 0);
675 wrmsr(MSR_MPERF, 0);
676
677 x86_enable_intr();
678
679 sc->sc_pstate_aperf = 0;
680 sc->sc_pstate_mperf = 0;
681 }
682
683 int
684 acpicpu_md_pstate_get(struct acpicpu_softc *sc, uint32_t *freq)
685 {
686 struct acpicpu_pstate *ps = NULL;
687 uint64_t val;
688 uint32_t i;
689
690 if ((sc->sc_flags & ACPICPU_FLAG_P_FIDVID) != 0)
691 return acpicpu_md_pstate_fidvid_get(sc, freq);
692
693 /*
694 * Pick any P-state for the status address.
695 */
696 for (i = 0; i < sc->sc_pstate_count; i++) {
697
698 ps = &sc->sc_pstate[i];
699
700 if (__predict_true(ps->ps_freq != 0))
701 break;
702 }
703
704 if (__predict_false(ps == NULL))
705 return ENODEV;
706
707 if (__predict_false(ps->ps_status_addr == 0))
708 return EINVAL;
709
710 val = rdmsr(ps->ps_status_addr);
711
712 if (__predict_true(ps->ps_status_mask != 0))
713 val = val & ps->ps_status_mask;
714
715 /*
716 * Search for the value from known P-states.
717 */
718 for (i = 0; i < sc->sc_pstate_count; i++) {
719
720 ps = &sc->sc_pstate[i];
721
722 if (__predict_false(ps->ps_freq == 0))
723 continue;
724
725 if (val == ps->ps_status) {
726 *freq = ps->ps_freq;
727 return 0;
728 }
729 }
730
731 /*
732 * If the value was not found, try APERF/MPERF.
733 * The state is P0 if the return value is 100 %.
734 */
735 if ((sc->sc_flags & ACPICPU_FLAG_P_HWF) != 0) {
736
737 if (acpicpu_md_pstate_hwf(sc->sc_ci) == 100) {
738 *freq = sc->sc_pstate[0].ps_freq;
739 return 0;
740 }
741 }
742
743 return EIO;
744 }
745
746 int
747 acpicpu_md_pstate_set(struct acpicpu_pstate *ps)
748 {
749 uint64_t val = 0;
750
751 if (__predict_false(ps->ps_control_addr == 0))
752 return EINVAL;
753
754 if ((ps->ps_flags & ACPICPU_FLAG_P_FIDVID) != 0)
755 return acpicpu_md_pstate_fidvid_set(ps);
756
757 /*
758 * If the mask is set, do a read-modify-write.
759 */
760 if (__predict_true(ps->ps_control_mask != 0)) {
761 val = rdmsr(ps->ps_control_addr);
762 val &= ~ps->ps_control_mask;
763 }
764
765 val |= ps->ps_control;
766
767 wrmsr(ps->ps_control_addr, val);
768 DELAY(ps->ps_latency);
769
770 return 0;
771 }
772
773 static int
774 acpicpu_md_pstate_fidvid_get(struct acpicpu_softc *sc, uint32_t *freq)
775 {
776 struct acpicpu_pstate *ps;
777 uint32_t fid, i, vid;
778 uint32_t cfid, cvid;
779 int rv;
780
781 /*
782 * AMD family 0Fh needs special treatment.
783 * While it wants to use ACPI, it does not
784 * comply with the ACPI specifications.
785 */
786 rv = acpicpu_md_pstate_fidvid_read(&cfid, &cvid);
787
788 if (rv != 0)
789 return rv;
790
791 for (i = 0; i < sc->sc_pstate_count; i++) {
792
793 ps = &sc->sc_pstate[i];
794
795 if (__predict_false(ps->ps_freq == 0))
796 continue;
797
798 fid = __SHIFTOUT(ps->ps_status, ACPI_0FH_STATUS_FID);
799 vid = __SHIFTOUT(ps->ps_status, ACPI_0FH_STATUS_VID);
800
801 if (cfid == fid && cvid == vid) {
802 *freq = ps->ps_freq;
803 return 0;
804 }
805 }
806
807 return EIO;
808 }
809
810 static int
811 acpicpu_md_pstate_fidvid_set(struct acpicpu_pstate *ps)
812 {
813 const uint64_t ctrl = ps->ps_control;
814 uint32_t cfid, cvid, fid, i, irt;
815 uint32_t pll, vco_cfid, vco_fid;
816 uint32_t val, vid, vst;
817 int rv;
818
819 rv = acpicpu_md_pstate_fidvid_read(&cfid, &cvid);
820
821 if (rv != 0)
822 return rv;
823
824 fid = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_FID);
825 vid = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_VID);
826 irt = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_IRT);
827 vst = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_VST);
828 pll = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_PLL);
829
830 vst = vst * 20;
831 pll = pll * 1000 / 5;
832 irt = 10 * __BIT(irt);
833
834 /*
835 * Phase 1.
836 */
837 while (cvid > vid) {
838
839 val = 1 << __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_MVS);
840 val = (val > cvid) ? 0 : cvid - val;
841
842 acpicpu_md_pstate_fidvid_write(cfid, val, 1, vst);
843 rv = acpicpu_md_pstate_fidvid_read(NULL, &cvid);
844
845 if (rv != 0)
846 return rv;
847 }
848
849 i = __SHIFTOUT(ctrl, ACPI_0FH_CONTROL_RVO);
850
851 for (; i > 0 && cvid > 0; --i) {
852
853 acpicpu_md_pstate_fidvid_write(cfid, cvid - 1, 1, vst);
854 rv = acpicpu_md_pstate_fidvid_read(NULL, &cvid);
855
856 if (rv != 0)
857 return rv;
858 }
859
860 /*
861 * Phase 2.
862 */
863 if (cfid != fid) {
864
865 vco_fid = FID_TO_VCO_FID(fid);
866 vco_cfid = FID_TO_VCO_FID(cfid);
867
868 while (abs(vco_fid - vco_cfid) > 2) {
869
870 if (fid <= cfid)
871 val = cfid - 2;
872 else {
873 val = (cfid > 6) ? cfid + 2 :
874 FID_TO_VCO_FID(cfid) + 2;
875 }
876
877 acpicpu_md_pstate_fidvid_write(val, cvid, pll, irt);
878 rv = acpicpu_md_pstate_fidvid_read(&cfid, NULL);
879
880 if (rv != 0)
881 return rv;
882
883 vco_cfid = FID_TO_VCO_FID(cfid);
884 }
885
886 acpicpu_md_pstate_fidvid_write(fid, cvid, pll, irt);
887 rv = acpicpu_md_pstate_fidvid_read(&cfid, NULL);
888
889 if (rv != 0)
890 return rv;
891 }
892
893 /*
894 * Phase 3.
895 */
896 if (cvid != vid) {
897
898 acpicpu_md_pstate_fidvid_write(cfid, vid, 1, vst);
899 rv = acpicpu_md_pstate_fidvid_read(NULL, &cvid);
900
901 if (rv != 0)
902 return rv;
903 }
904
905 return 0;
906 }
907
908 static int
909 acpicpu_md_pstate_fidvid_read(uint32_t *cfid, uint32_t *cvid)
910 {
911 int i = ACPICPU_P_STATE_RETRY * 100;
912 uint64_t val;
913
914 do {
915 val = rdmsr(MSR_0FH_STATUS);
916
917 } while (__SHIFTOUT(val, MSR_0FH_STATUS_PENDING) != 0 && --i >= 0);
918
919 if (i == 0)
920 return EAGAIN;
921
922 if (cfid != NULL)
923 *cfid = __SHIFTOUT(val, MSR_0FH_STATUS_CFID);
924
925 if (cvid != NULL)
926 *cvid = __SHIFTOUT(val, MSR_0FH_STATUS_CVID);
927
928 return 0;
929 }
930
931 static void
932 acpicpu_md_pstate_fidvid_write(uint32_t fid,
933 uint32_t vid, uint32_t cnt, uint32_t tmo)
934 {
935 uint64_t val = 0;
936
937 val |= __SHIFTIN(fid, MSR_0FH_CONTROL_FID);
938 val |= __SHIFTIN(vid, MSR_0FH_CONTROL_VID);
939 val |= __SHIFTIN(cnt, MSR_0FH_CONTROL_CNT);
940 val |= __SHIFTIN(0x1, MSR_0FH_CONTROL_CHG);
941
942 wrmsr(MSR_0FH_CONTROL, val);
943 DELAY(tmo);
944 }
945
946 int
947 acpicpu_md_tstate_get(struct acpicpu_softc *sc, uint32_t *percent)
948 {
949 struct acpicpu_tstate *ts;
950 uint64_t val;
951 uint32_t i;
952
953 val = rdmsr(MSR_THERM_CONTROL);
954
955 for (i = 0; i < sc->sc_tstate_count; i++) {
956
957 ts = &sc->sc_tstate[i];
958
959 if (ts->ts_percent == 0)
960 continue;
961
962 if (val == ts->ts_status) {
963 *percent = ts->ts_percent;
964 return 0;
965 }
966 }
967
968 return EIO;
969 }
970
971 int
972 acpicpu_md_tstate_set(struct acpicpu_tstate *ts)
973 {
974 uint64_t val;
975 uint8_t i;
976
977 val = ts->ts_control;
978 val = val & __BITS(1, 4);
979
980 wrmsr(MSR_THERM_CONTROL, val);
981
982 if (ts->ts_status == 0) {
983 DELAY(ts->ts_latency);
984 return 0;
985 }
986
987 for (i = val = 0; i < ACPICPU_T_STATE_RETRY; i++) {
988
989 val = rdmsr(MSR_THERM_CONTROL);
990
991 if (val == ts->ts_status)
992 return 0;
993
994 DELAY(ts->ts_latency);
995 }
996
997 return EAGAIN;
998 }
999
1000 /*
1001 * A kludge for backwards compatibility.
1002 */
1003 static int
1004 acpicpu_md_pstate_sysctl_init(void)
1005 {
1006 const struct sysctlnode *fnode, *mnode, *rnode;
1007 const char *str;
1008 int rv;
1009
1010 switch (cpu_vendor) {
1011
1012 case CPUVENDOR_IDT:
1013 case CPUVENDOR_INTEL:
1014 str = "est";
1015 break;
1016
1017 case CPUVENDOR_AMD:
1018 str = "powernow";
1019 break;
1020
1021 default:
1022 return ENODEV;
1023 }
1024
1025
1026 rv = sysctl_createv(&acpicpu_log, 0, NULL, &rnode,
1027 CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
1028 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
1029
1030 if (rv != 0)
1031 goto fail;
1032
1033 rv = sysctl_createv(&acpicpu_log, 0, &rnode, &mnode,
1034 0, CTLTYPE_NODE, str, NULL,
1035 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
1036
1037 if (rv != 0)
1038 goto fail;
1039
1040 rv = sysctl_createv(&acpicpu_log, 0, &mnode, &fnode,
1041 0, CTLTYPE_NODE, "frequency", NULL,
1042 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
1043
1044 if (rv != 0)
1045 goto fail;
1046
1047 rv = sysctl_createv(&acpicpu_log, 0, &fnode, &rnode,
1048 CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL,
1049 acpicpu_md_pstate_sysctl_set, 0, NULL, 0, CTL_CREATE, CTL_EOL);
1050
1051 if (rv != 0)
1052 goto fail;
1053
1054 rv = sysctl_createv(&acpicpu_log, 0, &fnode, &rnode,
1055 CTLFLAG_READONLY, CTLTYPE_INT, "current", NULL,
1056 acpicpu_md_pstate_sysctl_get, 0, NULL, 0, CTL_CREATE, CTL_EOL);
1057
1058 if (rv != 0)
1059 goto fail;
1060
1061 rv = sysctl_createv(&acpicpu_log, 0, &fnode, &rnode,
1062 CTLFLAG_READONLY, CTLTYPE_STRING, "available", NULL,
1063 acpicpu_md_pstate_sysctl_all, 0, NULL, 0, CTL_CREATE, CTL_EOL);
1064
1065 if (rv != 0)
1066 goto fail;
1067
1068 return 0;
1069
1070 fail:
1071 if (acpicpu_log != NULL) {
1072 sysctl_teardown(&acpicpu_log);
1073 acpicpu_log = NULL;
1074 }
1075
1076 return rv;
1077 }
1078
1079 static int
1080 acpicpu_md_pstate_sysctl_get(SYSCTLFN_ARGS)
1081 {
1082 struct cpu_info *ci = curcpu();
1083 struct sysctlnode node;
1084 uint32_t freq;
1085 int err;
1086
1087 err = acpicpu_pstate_get(ci, &freq);
1088
1089 if (err != 0)
1090 return err;
1091
1092 node = *rnode;
1093 node.sysctl_data = &freq;
1094
1095 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1096
1097 if (err != 0 || newp == NULL)
1098 return err;
1099
1100 return 0;
1101 }
1102
1103 static int
1104 acpicpu_md_pstate_sysctl_set(SYSCTLFN_ARGS)
1105 {
1106 struct cpu_info *ci = curcpu();
1107 struct sysctlnode node;
1108 uint32_t freq;
1109 int err;
1110
1111 err = acpicpu_pstate_get(ci, &freq);
1112
1113 if (err != 0)
1114 return err;
1115
1116 node = *rnode;
1117 node.sysctl_data = &freq;
1118
1119 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1120
1121 if (err != 0 || newp == NULL)
1122 return err;
1123
1124 acpicpu_pstate_set(ci, freq);
1125
1126 return 0;
1127 }
1128
1129 static int
1130 acpicpu_md_pstate_sysctl_all(SYSCTLFN_ARGS)
1131 {
1132 struct cpu_info *ci = curcpu();
1133 struct acpicpu_softc *sc;
1134 struct sysctlnode node;
1135 char buf[1024];
1136 size_t len;
1137 uint32_t i;
1138 int err;
1139
1140 sc = acpicpu_sc[ci->ci_acpiid];
1141
1142 if (sc == NULL)
1143 return ENXIO;
1144
1145 (void)memset(&buf, 0, sizeof(buf));
1146
1147 mutex_enter(&sc->sc_mtx);
1148
1149 for (len = 0, i = sc->sc_pstate_max; i < sc->sc_pstate_count; i++) {
1150
1151 if (sc->sc_pstate[i].ps_freq == 0)
1152 continue;
1153
1154 len += snprintf(buf + len, sizeof(buf) - len, "%u%s",
1155 sc->sc_pstate[i].ps_freq,
1156 i < (sc->sc_pstate_count - 1) ? " " : "");
1157 }
1158
1159 mutex_exit(&sc->sc_mtx);
1160
1161 node = *rnode;
1162 node.sysctl_data = buf;
1163
1164 err = sysctl_lookup(SYSCTLFN_CALL(&node));
1165
1166 if (err != 0 || newp == NULL)
1167 return err;
1168
1169 return 0;
1170 }
1171
1172