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