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