2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/queue.h>
43 #include <sys/sysctl.h>
44 #include <sys/msgport2.h>
45 #include <sys/cpu_topology.h>
47 #include <net/netisr2.h>
48 #include <net/netmsg2.h>
49 #include <net/if_var.h>
54 #include "acpi_cpu_pstate.h"
56 #define ACPI_NPSTATE_MAX 16
58 #define ACPI_PSS_PX_NENTRY 6
60 #define ACPI_PSD_COORD_SWALL 0xfc
61 #define ACPI_PSD_COORD_SWANY 0xfd
62 #define ACPI_PSD_COORD_HWALL 0xfe
63 #define ACPI_PSD_COORD_VALID(coord) \
64 ((coord) == ACPI_PSD_COORD_SWALL || \
65 (coord) == ACPI_PSD_COORD_SWANY || \
66 (coord) == ACPI_PSD_COORD_HWALL)
68 struct acpi_pst_softc;
69 LIST_HEAD(acpi_pst_list, acpi_pst_softc);
71 struct netmsg_acpi_pst {
72 struct netmsg_base base;
73 const struct acpi_pst_res *ctrl;
74 const struct acpi_pst_res *status;
77 struct acpi_pst_domain {
81 LIST_ENTRY(acpi_pst_domain) pd_link;
87 struct acpi_pst_list pd_pstlist;
89 struct sysctl_ctx_list pd_sysctl_ctx;
90 struct sysctl_oid *pd_sysctl_tree;
92 LIST_HEAD(acpi_pst_domlist, acpi_pst_domain);
94 #define ACPI_PSTDOM_FLAG_STUB 0x1 /* stub domain, no _PSD */
95 #define ACPI_PSTDOM_FLAG_DEAD 0x2 /* domain can't be started */
96 #define ACPI_PSTDOM_FLAG_INT 0x4 /* domain created from Integer _PSD */
98 struct acpi_pst_softc {
100 struct acpi_cpu_softc *pst_parent;
101 struct acpi_pst_domain *pst_domain;
102 struct acpi_pst_res pst_creg;
103 struct acpi_pst_res pst_sreg;
109 ACPI_HANDLE pst_handle;
111 LIST_ENTRY(acpi_pst_softc) pst_link;
114 static int acpi_pst_probe(device_t dev);
115 static int acpi_pst_attach(device_t dev);
117 static void acpi_pst_postattach(void *);
118 static struct acpi_pst_domain *
119 acpi_pst_domain_create_int(device_t, uint32_t);
120 static struct acpi_pst_domain *
121 acpi_pst_domain_create_pkg(device_t, ACPI_OBJECT *);
122 static struct acpi_pst_domain *
123 acpi_pst_domain_find(uint32_t);
124 static struct acpi_pst_domain *
125 acpi_pst_domain_alloc(uint32_t, uint32_t, uint32_t);
126 static int acpi_pst_domain_set_pstate(struct acpi_pst_domain *, int);
127 static void acpi_pst_domain_check_nproc(device_t, struct acpi_pst_domain *);
128 static int acpi_pst_global_set_pstate(int);
130 static int acpi_pst_check_csr(struct acpi_pst_softc *);
131 static int acpi_pst_check_pstates(struct acpi_pst_softc *);
132 static int acpi_pst_init(struct acpi_pst_softc *);
133 static int acpi_pst_set_pstate(struct acpi_pst_softc *,
134 const struct acpi_pstate *);
135 static const struct acpi_pstate *
136 acpi_pst_get_pstate(struct acpi_pst_softc *);
137 static int acpi_pst_alloc_resource(device_t, ACPI_OBJECT *, int,
138 struct acpi_pst_res *);
140 static void acpi_pst_check_csr_handler(netmsg_t);
141 static void acpi_pst_check_pstates_handler(netmsg_t);
142 static void acpi_pst_init_handler(netmsg_t);
143 static void acpi_pst_set_pstate_handler(netmsg_t);
144 static void acpi_pst_get_pstate_handler(netmsg_t);
146 static int acpi_pst_sysctl_freqs(SYSCTL_HANDLER_ARGS);
147 static int acpi_pst_sysctl_freqs_bin(SYSCTL_HANDLER_ARGS);
148 static int acpi_pst_sysctl_members(SYSCTL_HANDLER_ARGS);
149 static int acpi_pst_sysctl_select(SYSCTL_HANDLER_ARGS);
150 static int acpi_pst_sysctl_global(SYSCTL_HANDLER_ARGS);
152 static struct acpi_pst_domlist acpi_pst_domains =
153 LIST_HEAD_INITIALIZER(acpi_pst_domains);
154 static int acpi_pst_domain_id;
156 static int acpi_pst_global_state;
158 static int acpi_npstates;
159 static struct acpi_pstate *acpi_pstates;
161 static const struct acpi_pst_md *acpi_pst_md;
163 static int acpi_pst_ht_reuse_domain = 1;
164 TUNABLE_INT("hw.acpi.cpu.pst.ht_reuse_domain", &acpi_pst_ht_reuse_domain);
166 static int acpi_pst_force_pkg_domain = 0;
167 TUNABLE_INT("hw.acpi.cpu.pst.force_pkg_domain", &acpi_pst_force_pkg_domain);
169 static device_method_t acpi_pst_methods[] = {
170 /* Device interface */
171 DEVMETHOD(device_probe, acpi_pst_probe),
172 DEVMETHOD(device_attach, acpi_pst_attach),
173 DEVMETHOD(device_detach, bus_generic_detach),
174 DEVMETHOD(device_shutdown, bus_generic_shutdown),
175 DEVMETHOD(device_suspend, bus_generic_suspend),
176 DEVMETHOD(device_resume, bus_generic_resume),
179 DEVMETHOD(bus_add_child, bus_generic_add_child),
180 DEVMETHOD(bus_print_child, bus_generic_print_child),
181 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar),
182 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar),
183 DEVMETHOD(bus_get_resource_list, bus_generic_get_resource_list),
184 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
185 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
186 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
187 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
188 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
189 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
190 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
191 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
192 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
197 static driver_t acpi_pst_driver = {
200 sizeof(struct acpi_pst_softc)
203 static devclass_t acpi_pst_devclass;
204 DRIVER_MODULE(cpu_pst, cpu, acpi_pst_driver, acpi_pst_devclass, NULL, NULL);
205 MODULE_DEPEND(cpu_pst, acpi, 1, 1, 1);
208 acpi_pst_freq2index(int freq)
212 for (i = 0; i < acpi_npstates; ++i) {
213 if (acpi_pstates[i].st_freq == freq)
220 acpi_pst_probe(device_t dev)
227 if (acpi_disabled("cpu_pst") ||
228 acpi_get_type(dev) != ACPI_TYPE_PROCESSOR)
231 if (acpi_pst_md == NULL)
232 acpi_pst_md = acpi_pst_md_probe();
234 handle = acpi_get_handle(dev);
240 * Some BIOSes do not expose _PCT for the second thread of
241 * CPU cores. In this case, _PSD should be enough to get the
242 * P-state of the second thread working, since it must have
243 * the same _PCT and _PSS as the first thread in the same
247 buf.Length = ACPI_ALLOCATE_BUFFER;
248 status = AcpiEvaluateObject(handle, "_PSD", NULL, &buf);
249 if (!ACPI_FAILURE(status)) {
250 AcpiOsFree((ACPI_OBJECT *)buf.Pointer);
258 buf.Length = ACPI_ALLOCATE_BUFFER;
259 status = AcpiEvaluateObject(handle, "_PCT", NULL, &buf);
260 if (ACPI_FAILURE(status)) {
262 device_printf(dev, "Can't get _PCT package - %s\n",
263 AcpiFormatException(status));
268 obj = (ACPI_OBJECT *)buf.Pointer;
269 if (!ACPI_PKG_VALID_EQ(obj, 2)) {
270 device_printf(dev, "Invalid _PCT package\n");
280 buf.Length = ACPI_ALLOCATE_BUFFER;
281 status = AcpiEvaluateObject(handle, "_PSS", NULL, &buf);
282 if (ACPI_FAILURE(status)) {
283 device_printf(dev, "Can't get _PSS package - %s\n",
284 AcpiFormatException(status));
288 obj = (ACPI_OBJECT *)buf.Pointer;
289 if (!ACPI_PKG_VALID(obj, 1)) {
290 device_printf(dev, "Invalid _PSS package\n");
297 device_set_desc(dev, "ACPI CPU P-State");
302 acpi_pst_attach(device_t dev)
304 struct acpi_pst_softc *sc = device_get_softc(dev);
305 struct acpi_pst_domain *dom = NULL;
309 struct acpi_pstate *pstate, *p;
310 int i, npstate, error;
313 sc->pst_parent = device_get_softc(device_get_parent(dev));
314 sc->pst_handle = acpi_get_handle(dev);
315 sc->pst_cpuid = acpi_get_magic(dev);
318 * If there is a _PSD, then we create procossor domain
319 * accordingly. If there is no _PSD, we just fake a
320 * default processor domain0.
323 buf.Length = ACPI_ALLOCATE_BUFFER;
324 status = AcpiEvaluateObject(sc->pst_handle, "_PSD", NULL, &buf);
325 if (!ACPI_FAILURE(status)) {
326 obj = (ACPI_OBJECT *)buf.Pointer;
328 if (acpi_pst_domain_id > 0) {
329 device_printf(dev, "Missing _PSD for certain CPUs\n");
333 acpi_pst_domain_id = -1;
335 if (ACPI_PKG_VALID_EQ(obj, 1)) {
336 dom = acpi_pst_domain_create_pkg(dev,
337 &obj->Package.Elements[0]);
343 if (obj->Type != ACPI_TYPE_INTEGER) {
345 "Invalid _PSD package, Type 0x%x\n",
350 device_printf(dev, "Integer _PSD %ju\n",
351 (uintmax_t)obj->Integer.Value);
352 dom = acpi_pst_domain_create_int(dev,
362 AcpiOsFree(buf.Pointer);
364 if (acpi_pst_domain_id < 0) {
365 device_printf(dev, "Missing _PSD for cpu%d\n",
371 * Create a stub one processor domain for each processor
373 dom = acpi_pst_domain_alloc(acpi_pst_domain_id,
374 ACPI_PSD_COORD_SWANY, 1);
375 dom->pd_flags |= ACPI_PSTDOM_FLAG_STUB;
377 ++acpi_pst_domain_id;
380 /* Make sure that adding us will not overflow our domain */
381 acpi_pst_domain_check_nproc(dev, dom);
384 * Get control/status registers from _PCT
387 buf.Length = ACPI_ALLOCATE_BUFFER;
388 status = AcpiEvaluateObject(sc->pst_handle, "_PCT", NULL, &buf);
389 if (ACPI_FAILURE(status)) {
390 struct acpi_pst_softc *pst;
393 * No _PCT. See the comment in acpi_pst_probe() near
396 * Use control/status registers of another CPU in the
397 * same domain, or in the same core, if the type of
398 * these registers are "Fixed Hardware", e.g. on most
399 * of the model Intel CPUs.
401 pst = LIST_FIRST(&dom->pd_pstlist);
405 mask = get_cpumask_from_level(sc->pst_cpuid,
407 if (CPUMASK_TESTNZERO(mask)) {
408 struct acpi_pst_domain *dom1;
410 LIST_FOREACH(dom1, &acpi_pst_domains, pd_link) {
411 LIST_FOREACH(pst, &dom1->pd_pstlist,
413 if (CPUMASK_TESTBIT(mask,
420 if (pst != NULL && acpi_pst_ht_reuse_domain) {
422 * Use the same domain for CPUs in the
425 device_printf(dev, "Destroy domain%u, "
427 dom->pd_dom, dom1->pd_dom);
428 LIST_REMOVE(dom, pd_link);
429 kfree(dom, M_DEVBUF);
432 * Make sure that adding us will not
433 * overflow the domain containing
434 * siblings in the same core.
436 acpi_pst_domain_check_nproc(dev, dom);
441 pst->pst_creg.pr_res == NULL &&
442 pst->pst_creg.pr_rid == 0 &&
443 pst->pst_creg.pr_gas.SpaceId ==
444 ACPI_ADR_SPACE_FIXED_HARDWARE &&
445 pst->pst_sreg.pr_res == NULL &&
446 pst->pst_sreg.pr_rid == 0 &&
447 pst->pst_sreg.pr_gas.SpaceId ==
448 ACPI_ADR_SPACE_FIXED_HARDWARE) {
449 sc->pst_creg = pst->pst_creg;
450 sc->pst_sreg = pst->pst_sreg;
452 "No _PCT; reuse %s control/status regs\n",
453 device_get_nameunit(pst->pst_dev));
456 device_printf(dev, "Can't get _PCT package - %s\n",
457 AcpiFormatException(status));
461 obj = (ACPI_OBJECT *)buf.Pointer;
462 if (!ACPI_PKG_VALID_EQ(obj, 2)) {
463 device_printf(dev, "Invalid _PCT package\n");
468 /* Save and try allocating control register */
469 error = acpi_pst_alloc_resource(dev, obj, 0, &sc->pst_creg);
475 device_printf(dev, "control reg %d %jx\n",
476 sc->pst_creg.pr_gas.SpaceId,
477 (uintmax_t)sc->pst_creg.pr_gas.Address);
480 /* Save and try allocating status register */
481 error = acpi_pst_alloc_resource(dev, obj, 1, &sc->pst_sreg);
487 device_printf(dev, "status reg %d %jx\n",
488 sc->pst_sreg.pr_gas.SpaceId,
489 (uintmax_t)sc->pst_sreg.pr_gas.Address);
497 * Create P-State table according to _PSS
500 buf.Length = ACPI_ALLOCATE_BUFFER;
501 status = AcpiEvaluateObject(sc->pst_handle, "_PSS", NULL, &buf);
502 if (ACPI_FAILURE(status)) {
504 * No _PSS. See the comment in acpi_pst_probe() near
507 * Assume _PSS are same across all CPUs; well, they
508 * should/have to be so.
510 if (acpi_npstates > 0 && acpi_pstates != NULL) {
511 device_printf(dev, "No _PSS\n");
514 device_printf(dev, "Can't get _PSS package - %s\n",
515 AcpiFormatException(status));
519 obj = (ACPI_OBJECT *)buf.Pointer;
520 if (!ACPI_PKG_VALID(obj, 1)) {
521 device_printf(dev, "Invalid _PSS package\n");
526 /* Don't create too many P-States */
527 npstate = obj->Package.Count;
528 if (npstate > ACPI_NPSTATE_MAX) {
529 device_printf(dev, "Too many P-States, %d->%d\n",
530 npstate, ACPI_NPSTATE_MAX);
531 npstate = ACPI_NPSTATE_MAX;
535 * If we have already created P-State table,
536 * we must make sure that number of entries
539 if (acpi_pstates != NULL && acpi_npstates != npstate) {
540 device_printf(dev, "Inconsistent # of P-States "
541 "cross Processor objects\n");
547 * Create a temporary P-State table
549 pstate = kmalloc(sizeof(*pstate) * npstate, M_TEMP, M_WAITOK);
550 for (i = 0, p = pstate; i < npstate; ++i, ++p) {
552 uint32_t *ptr[ACPI_PSS_PX_NENTRY] = {
553 &p->st_freq, &p->st_power, &p->st_xsit_lat,
554 &p->st_bm_lat, &p->st_cval, &p->st_sval
558 pkg = &obj->Package.Elements[i];
559 if (!ACPI_PKG_VALID(pkg, ACPI_PSS_PX_NENTRY)) {
560 device_printf(dev, "Invalud _PSS P%d\n", i);
562 kfree(pstate, M_TEMP);
565 for (j = 0; j < ACPI_PSS_PX_NENTRY; ++j) {
566 if (acpi_PkgInt32(pkg, j, ptr[j]) != 0) {
567 device_printf(dev, "Can't extract "
568 "_PSS P%d %dth entry\n", i, j);
570 kfree(pstate, M_TEMP);
579 if (acpi_pstates == NULL) {
581 * If no P-State table is created yet,
582 * save the temporary one we just created.
584 acpi_pstates = pstate;
585 acpi_npstates = npstate;
589 for (i = 0; i < acpi_npstates; ++i) {
591 "freq %u, pwr %u, xlat %u, blat %u, "
592 "cv %08x, sv %08x\n",
593 acpi_pstates[i].st_freq,
594 acpi_pstates[i].st_power,
595 acpi_pstates[i].st_xsit_lat,
596 acpi_pstates[i].st_bm_lat,
597 acpi_pstates[i].st_cval,
598 acpi_pstates[i].st_sval);
603 * Make sure that P-State tables are same
604 * for all processors.
606 if (memcmp(pstate, acpi_pstates,
607 sizeof(*pstate) * npstate) != 0) {
608 device_printf(dev, "Inconsistent _PSS "
609 "cross Processor objects\n");
612 * Some BIOSes create different P-State tables;
613 * just trust the one from the BSP and move on.
615 kfree(pstate, M_TEMP);
619 kfree(pstate, M_TEMP);
623 /* By default, we start from P-State table's first entry */
627 * Adjust the usable first entry of P-State table,
628 * if there is _PPC object.
631 buf.Length = ACPI_ALLOCATE_BUFFER;
632 status = AcpiEvaluateObject(sc->pst_handle, "_PPC", NULL, &buf);
633 if (!ACPI_FAILURE(status)) {
634 obj = (ACPI_OBJECT *)buf.Pointer;
635 if (obj->Type == ACPI_TYPE_INTEGER) {
636 if (obj->Integer.Value >= acpi_npstates) {
637 device_printf(dev, "Invalid _PPC value\n");
641 sc->pst_sstart = obj->Integer.Value;
643 device_printf(dev, "_PPC %d\n", sc->pst_sstart);
645 /* TODO: Install notifiy handler */
647 device_printf(dev, "Invalid _PPC object\n");
656 sc->pst_state = sc->pst_sstart;
659 * Some CPUs only have package P-states, but some BIOSes put each
660 * hyperthread to its own P-state domain; allow user to override.
662 if (LIST_EMPTY(&dom->pd_pstlist) && acpi_pst_force_pkg_domain) {
665 mask = get_cpumask_from_level(sc->pst_cpuid, CHIP_LEVEL);
666 if (CPUMASK_TESTNZERO(mask)) {
667 struct acpi_pst_softc *pst = NULL;
668 struct acpi_pst_domain *dom1;
670 LIST_FOREACH(dom1, &acpi_pst_domains, pd_link) {
671 LIST_FOREACH(pst, &dom1->pd_pstlist,
673 if (CPUMASK_TESTBIT(mask,
681 memcmp(&pst->pst_creg, &sc->pst_creg,
682 sizeof(sc->pst_creg)) == 0 &&
683 memcmp(&pst->pst_sreg, &sc->pst_sreg,
684 sizeof(sc->pst_sreg)) == 0) {
686 * Use the same domain for CPUs in the
689 device_printf(dev, "Destroy domain%u, "
690 "force pkg domain%u\n",
691 dom->pd_dom, dom1->pd_dom);
692 LIST_REMOVE(dom, pd_link);
693 kfree(dom, M_DEVBUF);
696 * Make sure that adding us will not
697 * overflow the domain containing
698 * siblings in the same package.
700 acpi_pst_domain_check_nproc(dev, dom);
705 /* Link us with the domain */
706 sc->pst_domain = dom;
707 LIST_INSERT_HEAD(&dom->pd_pstlist, sc, pst_link);
709 if (device_get_unit(dev) == 0)
710 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_pst_postattach, NULL);
715 static struct acpi_pst_domain *
716 acpi_pst_domain_create_pkg(device_t dev, ACPI_OBJECT *obj)
718 struct acpi_pst_domain *dom;
719 uint32_t val, domain, coord, nproc;
721 if (!ACPI_PKG_VALID_EQ(obj, 5)) {
722 device_printf(dev, "Invalid _PSD package\n");
726 /* NumberOfEntries */
727 if (acpi_PkgInt32(obj, 0, &val) != 0 || val != 5) {
728 device_printf(dev, "Invalid _PSD NumberOfEntries\n");
733 if (acpi_PkgInt32(obj, 1, &val) != 0 || val != 0) {
734 device_printf(dev, "Invalid _PSD Revision\n");
738 if (acpi_PkgInt32(obj, 2, &domain) != 0 ||
739 acpi_PkgInt32(obj, 3, &coord) != 0 ||
740 acpi_PkgInt32(obj, 4, &nproc) != 0) {
741 device_printf(dev, "Can't extract _PSD package\n");
745 if (!ACPI_PSD_COORD_VALID(coord)) {
746 device_printf(dev, "Invalid _PSD CoordType (%#x)\n", coord);
750 if (nproc > MAXCPU) {
752 * If NumProcessors is greater than MAXCPU
753 * and domain's coordination is SWALL, then
754 * we will never be able to start all CPUs
755 * within this domain, and power state
756 * transition will never be completed, so we
757 * just bail out here.
759 if (coord == ACPI_PSD_COORD_SWALL) {
760 device_printf(dev, "Unsupported _PSD NumProcessors "
764 } else if (nproc == 0) {
765 device_printf(dev, "_PSD NumProcessors are zero\n");
769 dom = acpi_pst_domain_find(domain);
771 if (dom->pd_flags & ACPI_PSTDOM_FLAG_INT) {
772 device_printf(dev, "Mixed Integer _PSD and "
776 if (dom->pd_coord != coord) {
777 device_printf(dev, "Inconsistent _PSD coord "
778 "information cross Processor objects\n");
781 if (dom->pd_nproc != nproc) {
782 device_printf(dev, "Inconsistent _PSD nproc "
783 "information cross Processor objects\n");
785 * Some stupid BIOSes will set wrong "# of processors",
786 * e.g. 1 for CPU w/ hyperthreading; Be lenient here.
792 dom = acpi_pst_domain_alloc(domain, coord, nproc);
794 device_printf(dev, "create pkg domain%u, coord %#x\n",
795 dom->pd_dom, dom->pd_coord);
801 static struct acpi_pst_domain *
802 acpi_pst_domain_create_int(device_t dev, uint32_t domain)
804 struct acpi_pst_domain *dom;
806 dom = acpi_pst_domain_find(domain);
808 if ((dom->pd_flags & ACPI_PSTDOM_FLAG_INT) == 0) {
809 device_printf(dev, "Mixed Package _PSD and "
813 KKASSERT(dom->pd_coord == ACPI_PSD_COORD_SWALL);
819 dom = acpi_pst_domain_alloc(domain, ACPI_PSD_COORD_SWALL, 1);
820 dom->pd_flags |= ACPI_PSTDOM_FLAG_INT;
823 device_printf(dev, "create int domain%u\n", dom->pd_dom);
828 static struct acpi_pst_domain *
829 acpi_pst_domain_find(uint32_t domain)
831 struct acpi_pst_domain *dom;
833 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
834 if (dom->pd_flags & ACPI_PSTDOM_FLAG_STUB)
836 if (dom->pd_dom == domain)
842 static struct acpi_pst_domain *
843 acpi_pst_domain_alloc(uint32_t domain, uint32_t coord, uint32_t nproc)
845 struct acpi_pst_domain *dom;
847 dom = kmalloc(sizeof(*dom), M_DEVBUF, M_WAITOK | M_ZERO);
848 dom->pd_dom = domain;
849 dom->pd_coord = coord;
850 dom->pd_nproc = nproc;
851 dom->pd_state = 0; /* XXX */
852 dom->pd_sstart = 0; /* XXX */
853 LIST_INIT(&dom->pd_pstlist);
855 LIST_INSERT_HEAD(&acpi_pst_domains, dom, pd_link);
861 acpi_pst_domain_set_pstate(struct acpi_pst_domain *dom, int i)
863 const struct acpi_pstate *pstate;
864 struct acpi_pst_softc *sc;
867 KKASSERT(i >= 0 && i < acpi_npstates);
868 pstate = &acpi_pstates[i];
871 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
873 error = acpi_pst_set_pstate(sc, pstate);
875 device_printf(sc->pst_dev, "can't set "
876 "freq %d\n", pstate->st_freq);
877 /* XXX error cleanup? */
879 if (dom->pd_coord == ACPI_PSD_COORD_SWANY)
890 acpi_pst_global_set_pstate(int i)
892 struct acpi_pst_domain *dom;
894 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
895 /* Skip dead domain */
896 if (dom->pd_flags & ACPI_PSTDOM_FLAG_DEAD)
898 acpi_pst_domain_set_pstate(dom, i);
900 acpi_pst_global_state = i;
906 acpi_pst_postattach(void *arg __unused)
908 struct acpi_pst_domain *dom;
909 struct acpi_cpu_softc *cpu;
911 int i, ndevices, error, has_domain, sstate;
915 error = devclass_get_devices(acpi_pst_devclass, &devices, &ndevices);
923 for (i = 0; i < ndevices; ++i) {
924 cpu = device_get_softc(device_get_parent(devices[i]));
925 if (cpu->glob_sysctl_tree != NULL)
928 kfree(devices, M_TEMP);
929 KKASSERT(cpu != NULL);
931 if (acpi_pst_md == NULL)
932 kprintf("ACPI: no P-State CPU driver\n");
936 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
937 struct acpi_pst_softc *sc;
941 * Make sure that all processors belonging to this
942 * domain are located.
945 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link)
947 if (i != dom->pd_nproc) {
948 KKASSERT(i < dom->pd_nproc);
950 kprintf("ACPI: domain%u misses processors, "
951 "should be %d, got %d\n", dom->pd_dom,
953 if (dom->pd_coord == ACPI_PSD_COORD_SWALL) {
955 * If this domain's coordination is
956 * SWALL and we don't see all of the
957 * member CPUs of this domain, then
958 * the P-State transition will never
959 * be completed, so just leave this
962 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
969 * Validate P-State configurations for this domain
971 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
972 error = acpi_pst_check_csr(sc);
976 error = acpi_pst_check_pstates(sc);
981 kprintf("ACPI: domain%u P-State configuration "
982 "check failed\n", dom->pd_dom);
983 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
988 * Do necssary P-State initialization
990 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
991 error = acpi_pst_init(sc);
996 kprintf("ACPI: domain%u P-State initialization "
997 "check failed\n", dom->pd_dom);
998 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
1004 ksnprintf(buf, sizeof(buf), "px_dom%u", dom->pd_dom);
1006 sysctl_ctx_init(&dom->pd_sysctl_ctx);
1007 dom->pd_sysctl_tree =
1008 SYSCTL_ADD_NODE(&dom->pd_sysctl_ctx,
1009 SYSCTL_CHILDREN(cpu->glob_sysctl_tree),
1010 OID_AUTO, buf, CTLFLAG_RD, 0,
1012 if (dom->pd_sysctl_tree == NULL) {
1013 kprintf("ACPI: Can't create sysctl tree for domain%u",
1018 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
1019 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
1020 OID_AUTO, "available",
1021 CTLTYPE_STRING | CTLFLAG_RD,
1022 dom, 0, acpi_pst_sysctl_freqs, "A",
1023 "available frequencies");
1025 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
1026 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
1027 OID_AUTO, "available_bin",
1028 CTLTYPE_OPAQUE | CTLFLAG_RD,
1029 dom, 0, acpi_pst_sysctl_freqs_bin, "IU",
1030 "available frequencies (binary format)");
1032 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
1033 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
1034 OID_AUTO, "members",
1035 CTLTYPE_STRING | CTLFLAG_RD,
1036 dom, 0, acpi_pst_sysctl_members, "A",
1039 if (acpi_pst_md != NULL &&
1040 acpi_pst_md->pmd_set_pstate != NULL) {
1041 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
1042 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
1044 CTLTYPE_UINT | CTLFLAG_RW,
1045 dom, 0, acpi_pst_sysctl_select,
1046 "IU", "select freq");
1049 if (dom->pd_state < sstate)
1050 sstate = dom->pd_state;
1053 if (has_domain && acpi_pst_md != NULL &&
1054 acpi_pst_md->pmd_set_pstate != NULL) {
1055 SYSCTL_ADD_PROC(&cpu->glob_sysctl_ctx,
1056 SYSCTL_CHILDREN(cpu->glob_sysctl_tree),
1057 OID_AUTO, "px_global",
1058 CTLTYPE_UINT | CTLFLAG_RW,
1059 NULL, 0, acpi_pst_sysctl_global,
1060 "IU", "select freq for all domains");
1062 acpi_pst_global_set_pstate(sstate);
1067 acpi_pst_sysctl_freqs(SYSCTL_HANDLER_ARGS)
1069 struct acpi_pst_domain *dom = arg1;
1073 for (i = 0; i < acpi_npstates; ++i) {
1074 if (error == 0 && i)
1075 error = SYSCTL_OUT(req, " ", 1);
1080 if (i < dom->pd_sstart)
1085 ksnprintf(buf, sizeof(buf), pat,
1086 acpi_pstates[i].st_freq);
1087 error = SYSCTL_OUT(req, buf, strlen(buf));
1094 acpi_pst_sysctl_freqs_bin(SYSCTL_HANDLER_ARGS)
1096 struct acpi_pst_domain *dom = arg1;
1097 uint32_t freqs[ACPI_NPSTATE_MAX];
1100 cnt = acpi_npstates - dom->pd_sstart;
1101 for (i = 0; i < cnt; ++i)
1102 freqs[i] = acpi_pstates[dom->pd_sstart + i].st_freq;
1104 return sysctl_handle_opaque(oidp, freqs, cnt * sizeof(freqs[0]), req);
1108 acpi_pst_sysctl_members(SYSCTL_HANDLER_ARGS)
1110 struct acpi_pst_domain *dom = arg1;
1111 struct acpi_pst_softc *sc;
1115 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
1118 if (error == 0 && loop)
1119 error = SYSCTL_OUT(req, " ", 1);
1121 ksnprintf(buf, sizeof(buf), "cpu%d", sc->pst_cpuid);
1122 error = SYSCTL_OUT(req, buf, strlen(buf));
1125 if (error == 0 && acpi_pst_md && acpi_pst_md->pmd_get_pstate) {
1126 const struct acpi_pstate *pstate;
1129 pstate = acpi_pst_get_pstate(sc);
1130 if (pstate == NULL) {
1133 ksnprintf(buf, sizeof(buf), "(%d)",
1137 error = SYSCTL_OUT(req, str, strlen(str));
1145 acpi_pst_sysctl_select(SYSCTL_HANDLER_ARGS)
1147 struct acpi_pst_domain *dom = arg1;
1150 KKASSERT(dom->pd_state >= 0 && dom->pd_state < acpi_npstates);
1152 freq = acpi_pstates[dom->pd_state].st_freq;
1154 error = sysctl_handle_int(oidp, &freq, 0, req);
1155 if (error || req->newptr == NULL)
1158 i = acpi_pst_freq2index(freq);
1162 acpi_pst_domain_set_pstate(dom, i);
1167 acpi_pst_sysctl_global(SYSCTL_HANDLER_ARGS)
1171 KKASSERT(acpi_pst_global_state >= 0 &&
1172 acpi_pst_global_state < acpi_npstates);
1174 freq = acpi_pstates[acpi_pst_global_state].st_freq;
1176 error = sysctl_handle_int(oidp, &freq, 0, req);
1177 if (error || req->newptr == NULL)
1180 i = acpi_pst_freq2index(freq);
1184 acpi_pst_global_set_pstate(i);
1190 acpi_pst_check_csr_handler(netmsg_t msg)
1192 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1195 error = acpi_pst_md->pmd_check_csr(rmsg->ctrl, rmsg->status);
1196 lwkt_replymsg(&rmsg->base.lmsg, error);
1200 acpi_pst_check_csr(struct acpi_pst_softc *sc)
1202 struct netmsg_acpi_pst msg;
1204 if (acpi_pst_md == NULL)
1207 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1208 MSGF_PRIORITY, acpi_pst_check_csr_handler);
1209 msg.ctrl = &sc->pst_creg;
1210 msg.status = &sc->pst_sreg;
1212 return lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.base.lmsg, 0);
1216 acpi_pst_check_pstates_handler(netmsg_t msg)
1220 error = acpi_pst_md->pmd_check_pstates(acpi_pstates, acpi_npstates);
1221 lwkt_replymsg(&msg->lmsg, error);
1225 acpi_pst_check_pstates(struct acpi_pst_softc *sc)
1227 struct netmsg_base msg;
1229 if (acpi_pst_md == NULL)
1232 netmsg_init(&msg, NULL, &curthread->td_msgport,
1233 MSGF_PRIORITY, acpi_pst_check_pstates_handler);
1235 return lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.lmsg, 0);
1239 acpi_pst_init_handler(netmsg_t msg)
1241 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1244 error = acpi_pst_md->pmd_init(rmsg->ctrl, rmsg->status);
1245 lwkt_replymsg(&rmsg->base.lmsg, error);
1249 acpi_pst_init(struct acpi_pst_softc *sc)
1251 struct netmsg_acpi_pst msg;
1253 if (acpi_pst_md == NULL)
1256 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1257 MSGF_PRIORITY, acpi_pst_init_handler);
1258 msg.ctrl = &sc->pst_creg;
1259 msg.status = &sc->pst_sreg;
1261 return lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.base.lmsg, 0);
1265 acpi_pst_set_pstate_handler(netmsg_t msg)
1267 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1270 error = acpi_pst_md->pmd_set_pstate(rmsg->ctrl, rmsg->status,
1271 rmsg->base.lmsg.u.ms_resultp);
1272 lwkt_replymsg(&rmsg->base.lmsg, error);
1276 acpi_pst_set_pstate(struct acpi_pst_softc *sc, const struct acpi_pstate *pstate)
1278 struct netmsg_acpi_pst msg;
1280 KKASSERT(acpi_pst_md != NULL);
1282 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1283 MSGF_PRIORITY, acpi_pst_set_pstate_handler);
1284 msg.base.lmsg.u.ms_resultp = __DECONST(void *, pstate);
1285 msg.ctrl = &sc->pst_creg;
1286 msg.status = &sc->pst_sreg;
1288 return lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.base.lmsg, 0);
1292 acpi_pst_get_pstate_handler(netmsg_t msg)
1294 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1295 const struct acpi_pstate *pstate;
1297 pstate = acpi_pst_md->pmd_get_pstate(rmsg->status, acpi_pstates,
1299 rmsg->base.lmsg.u.ms_resultp = __DECONST(void *, pstate);
1300 lwkt_replymsg(&rmsg->base.lmsg, 0);
1303 static const struct acpi_pstate *
1304 acpi_pst_get_pstate(struct acpi_pst_softc *sc)
1306 struct netmsg_acpi_pst msg;
1308 if (acpi_pst_md == NULL)
1311 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1312 MSGF_PRIORITY, acpi_pst_get_pstate_handler);
1313 msg.status = &sc->pst_sreg;
1315 lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.base.lmsg, 0);
1316 return msg.base.lmsg.u.ms_resultp;
1320 acpi_pst_alloc_resource(device_t dev, ACPI_OBJECT *obj, int idx,
1321 struct acpi_pst_res *res)
1323 struct acpi_pst_softc *sc = device_get_softc(dev);
1327 error = acpi_PkgRawGas(obj, idx, &res->pr_gas);
1331 /* Allocate resource, if possible */
1332 res->pr_rid = sc->pst_parent->cpu_next_rid;
1333 acpi_bus_alloc_gas(dev, &type, &res->pr_rid, &res->pr_gas, &res->pr_res, 0);
1334 if (res->pr_res != NULL) {
1335 sc->pst_parent->cpu_next_rid++;
1336 res->pr_bt = rman_get_bustag(res->pr_res);
1337 res->pr_bh = rman_get_bushandle(res->pr_res);
1345 acpi_pst_domain_check_nproc(device_t dev, struct acpi_pst_domain *dom)
1347 struct acpi_pst_softc *pst;
1351 LIST_FOREACH(pst, &dom->pd_pstlist, pst_link)
1353 if (i == dom->pd_nproc) {
1355 * Some stupid BIOSes will set wrong "# of processors",
1356 * e.g. 1 for CPU w/ hyperthreading; Be lenient here.
1359 device_printf(dev, "domain%u already contains %d "
1360 "P-States\n", dom->pd_dom, dom->pd_nproc);
1364 KKASSERT(i < dom->pd_nproc);