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