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