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