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_members(SYSCTL_HANDLER_ARGS);
148 static int acpi_pst_sysctl_select(SYSCTL_HANDLER_ARGS);
149 static int acpi_pst_sysctl_global(SYSCTL_HANDLER_ARGS);
151 static struct acpi_pst_domlist acpi_pst_domains =
152 LIST_HEAD_INITIALIZER(acpi_pst_domains);
153 static int acpi_pst_domain_id;
155 static int acpi_pst_global_state;
157 static int acpi_npstates;
158 static struct acpi_pstate *acpi_pstates;
160 static const struct acpi_pst_md *acpi_pst_md;
162 static int acpi_pst_ht_reuse_domain = 1;
163 TUNABLE_INT("hw.acpi.cpu.pst.ht_reuse_domain", &acpi_pst_ht_reuse_domain);
165 static int acpi_pst_force_pkg_domain = 0;
166 TUNABLE_INT("hw.acpi.cpu.pst.force_pkg_domain", &acpi_pst_force_pkg_domain);
168 static device_method_t acpi_pst_methods[] = {
169 /* Device interface */
170 DEVMETHOD(device_probe, acpi_pst_probe),
171 DEVMETHOD(device_attach, acpi_pst_attach),
172 DEVMETHOD(device_detach, bus_generic_detach),
173 DEVMETHOD(device_shutdown, bus_generic_shutdown),
174 DEVMETHOD(device_suspend, bus_generic_suspend),
175 DEVMETHOD(device_resume, bus_generic_resume),
178 DEVMETHOD(bus_add_child, bus_generic_add_child),
179 DEVMETHOD(bus_print_child, bus_generic_print_child),
180 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar),
181 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar),
182 DEVMETHOD(bus_get_resource_list, bus_generic_get_resource_list),
183 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
184 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
185 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
186 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
187 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
188 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
189 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
190 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
191 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
196 static driver_t acpi_pst_driver = {
199 sizeof(struct acpi_pst_softc)
202 static devclass_t acpi_pst_devclass;
203 DRIVER_MODULE(cpu_pst, cpu, acpi_pst_driver, acpi_pst_devclass, NULL, NULL);
204 MODULE_DEPEND(cpu_pst, acpi, 1, 1, 1);
207 acpi_pst_freq2index(int freq)
211 for (i = 0; i < acpi_npstates; ++i) {
212 if (acpi_pstates[i].st_freq == freq)
219 acpi_pst_probe(device_t dev)
226 if (acpi_disabled("cpu_pst") ||
227 acpi_get_type(dev) != ACPI_TYPE_PROCESSOR)
230 if (acpi_pst_md == NULL)
231 acpi_pst_md = acpi_pst_md_probe();
233 handle = acpi_get_handle(dev);
239 * Some BIOSes do not expose _PCT for the second thread of
240 * CPU cores. In this case, _PSD should be enough to get the
241 * P-state of the second thread working, since it must have
242 * the same _PCT and _PSS as the first thread in the same
246 buf.Length = ACPI_ALLOCATE_BUFFER;
247 status = AcpiEvaluateObject(handle, "_PSD", NULL, &buf);
248 if (!ACPI_FAILURE(status)) {
249 AcpiOsFree((ACPI_OBJECT *)buf.Pointer);
257 buf.Length = ACPI_ALLOCATE_BUFFER;
258 status = AcpiEvaluateObject(handle, "_PCT", NULL, &buf);
259 if (ACPI_FAILURE(status)) {
261 device_printf(dev, "Can't get _PCT package - %s\n",
262 AcpiFormatException(status));
267 obj = (ACPI_OBJECT *)buf.Pointer;
268 if (!ACPI_PKG_VALID_EQ(obj, 2)) {
269 device_printf(dev, "Invalid _PCT package\n");
279 buf.Length = ACPI_ALLOCATE_BUFFER;
280 status = AcpiEvaluateObject(handle, "_PSS", NULL, &buf);
281 if (ACPI_FAILURE(status)) {
282 device_printf(dev, "Can't get _PSS package - %s\n",
283 AcpiFormatException(status));
287 obj = (ACPI_OBJECT *)buf.Pointer;
288 if (!ACPI_PKG_VALID(obj, 1)) {
289 device_printf(dev, "Invalid _PSS package\n");
296 device_set_desc(dev, "ACPI CPU P-State");
301 acpi_pst_attach(device_t dev)
303 struct acpi_pst_softc *sc = device_get_softc(dev);
304 struct acpi_pst_domain *dom = NULL;
308 struct acpi_pstate *pstate, *p;
309 int i, npstate, error;
312 sc->pst_parent = device_get_softc(device_get_parent(dev));
313 sc->pst_handle = acpi_get_handle(dev);
314 sc->pst_cpuid = acpi_get_magic(dev);
317 * If there is a _PSD, then we create procossor domain
318 * accordingly. If there is no _PSD, we just fake a
319 * default processor domain0.
322 buf.Length = ACPI_ALLOCATE_BUFFER;
323 status = AcpiEvaluateObject(sc->pst_handle, "_PSD", NULL, &buf);
324 if (!ACPI_FAILURE(status)) {
325 obj = (ACPI_OBJECT *)buf.Pointer;
327 if (acpi_pst_domain_id > 0) {
328 device_printf(dev, "Missing _PSD for certain CPUs\n");
332 acpi_pst_domain_id = -1;
334 if (ACPI_PKG_VALID_EQ(obj, 1)) {
335 dom = acpi_pst_domain_create_pkg(dev,
336 &obj->Package.Elements[0]);
342 if (obj->Type != ACPI_TYPE_INTEGER) {
344 "Invalid _PSD package, Type 0x%x\n",
349 device_printf(dev, "Integer _PSD %ju\n",
350 (uintmax_t)obj->Integer.Value);
351 dom = acpi_pst_domain_create_int(dev,
361 AcpiOsFree(buf.Pointer);
363 if (acpi_pst_domain_id < 0) {
364 device_printf(dev, "Missing _PSD for cpu%d\n",
370 * Create a stub one processor domain for each processor
372 dom = acpi_pst_domain_alloc(acpi_pst_domain_id,
373 ACPI_PSD_COORD_SWANY, 1);
374 dom->pd_flags |= ACPI_PSTDOM_FLAG_STUB;
376 ++acpi_pst_domain_id;
379 /* Make sure that adding us will not overflow our domain */
380 acpi_pst_domain_check_nproc(dev, dom);
383 * Get control/status registers from _PCT
386 buf.Length = ACPI_ALLOCATE_BUFFER;
387 status = AcpiEvaluateObject(sc->pst_handle, "_PCT", NULL, &buf);
388 if (ACPI_FAILURE(status)) {
389 struct acpi_pst_softc *pst;
392 * No _PCT. See the comment in acpi_pst_probe() near
395 * Use control/status registers of another CPU in the
396 * same domain, or in the same core, if the type of
397 * these registers are "Fixed Hardware", e.g. on most
398 * of the model Intel CPUs.
400 pst = LIST_FIRST(&dom->pd_pstlist);
404 mask = get_cpumask_from_level(sc->pst_cpuid,
406 if (CPUMASK_TESTNZERO(mask)) {
407 struct acpi_pst_domain *dom1;
409 LIST_FOREACH(dom1, &acpi_pst_domains, pd_link) {
410 LIST_FOREACH(pst, &dom1->pd_pstlist,
412 if (CPUMASK_TESTBIT(mask,
419 if (pst != NULL && acpi_pst_ht_reuse_domain) {
421 * Use the same domain for CPUs in the
424 device_printf(dev, "Destroy domain%u, "
426 dom->pd_dom, dom1->pd_dom);
427 LIST_REMOVE(dom, pd_link);
428 kfree(dom, M_DEVBUF);
431 * Make sure that adding us will not
432 * overflow the domain containing
433 * siblings in the same core.
435 acpi_pst_domain_check_nproc(dev, dom);
440 pst->pst_creg.pr_res == NULL &&
441 pst->pst_creg.pr_rid == 0 &&
442 pst->pst_creg.pr_gas.SpaceId ==
443 ACPI_ADR_SPACE_FIXED_HARDWARE &&
444 pst->pst_sreg.pr_res == NULL &&
445 pst->pst_sreg.pr_rid == 0 &&
446 pst->pst_sreg.pr_gas.SpaceId ==
447 ACPI_ADR_SPACE_FIXED_HARDWARE) {
448 sc->pst_creg = pst->pst_creg;
449 sc->pst_sreg = pst->pst_sreg;
451 "No _PCT; reuse %s control/status regs\n",
452 device_get_nameunit(pst->pst_dev));
455 device_printf(dev, "Can't get _PCT package - %s\n",
456 AcpiFormatException(status));
460 obj = (ACPI_OBJECT *)buf.Pointer;
461 if (!ACPI_PKG_VALID_EQ(obj, 2)) {
462 device_printf(dev, "Invalid _PCT package\n");
467 /* Save and try allocating control register */
468 error = acpi_pst_alloc_resource(dev, obj, 0, &sc->pst_creg);
474 device_printf(dev, "control reg %d %jx\n",
475 sc->pst_creg.pr_gas.SpaceId,
476 (uintmax_t)sc->pst_creg.pr_gas.Address);
479 /* Save and try allocating status register */
480 error = acpi_pst_alloc_resource(dev, obj, 1, &sc->pst_sreg);
486 device_printf(dev, "status reg %d %jx\n",
487 sc->pst_sreg.pr_gas.SpaceId,
488 (uintmax_t)sc->pst_sreg.pr_gas.Address);
496 * Create P-State table according to _PSS
499 buf.Length = ACPI_ALLOCATE_BUFFER;
500 status = AcpiEvaluateObject(sc->pst_handle, "_PSS", NULL, &buf);
501 if (ACPI_FAILURE(status)) {
503 * No _PSS. See the comment in acpi_pst_probe() near
506 * Assume _PSS are same across all CPUs; well, they
507 * should/have to be so.
509 if (acpi_npstates > 0 && acpi_pstates != NULL) {
510 device_printf(dev, "No _PSS\n");
513 device_printf(dev, "Can't get _PSS package - %s\n",
514 AcpiFormatException(status));
518 obj = (ACPI_OBJECT *)buf.Pointer;
519 if (!ACPI_PKG_VALID(obj, 1)) {
520 device_printf(dev, "Invalid _PSS package\n");
525 /* Don't create too many P-States */
526 npstate = obj->Package.Count;
527 if (npstate > ACPI_NPSTATE_MAX) {
528 device_printf(dev, "Too many P-States, %d->%d\n",
529 npstate, ACPI_NPSTATE_MAX);
530 npstate = ACPI_NPSTATE_MAX;
534 * If we have already created P-State table,
535 * we must make sure that number of entries
538 if (acpi_pstates != NULL && acpi_npstates != npstate) {
539 device_printf(dev, "Inconsistent # of P-States "
540 "cross Processor objects\n");
546 * Create a temporary P-State table
548 pstate = kmalloc(sizeof(*pstate) * npstate, M_TEMP, M_WAITOK);
549 for (i = 0, p = pstate; i < npstate; ++i, ++p) {
551 uint32_t *ptr[ACPI_PSS_PX_NENTRY] = {
552 &p->st_freq, &p->st_power, &p->st_xsit_lat,
553 &p->st_bm_lat, &p->st_cval, &p->st_sval
557 pkg = &obj->Package.Elements[i];
558 if (!ACPI_PKG_VALID(pkg, ACPI_PSS_PX_NENTRY)) {
559 device_printf(dev, "Invalud _PSS P%d\n", i);
561 kfree(pstate, M_TEMP);
564 for (j = 0; j < ACPI_PSS_PX_NENTRY; ++j) {
565 if (acpi_PkgInt32(pkg, j, ptr[j]) != 0) {
566 device_printf(dev, "Can't extract "
567 "_PSS P%d %dth entry\n", i, j);
569 kfree(pstate, M_TEMP);
578 if (acpi_pstates == NULL) {
580 * If no P-State table is created yet,
581 * save the temporary one we just created.
583 acpi_pstates = pstate;
584 acpi_npstates = npstate;
588 for (i = 0; i < acpi_npstates; ++i) {
590 "freq %u, pwr %u, xlat %u, blat %u, "
591 "cv %08x, sv %08x\n",
592 acpi_pstates[i].st_freq,
593 acpi_pstates[i].st_power,
594 acpi_pstates[i].st_xsit_lat,
595 acpi_pstates[i].st_bm_lat,
596 acpi_pstates[i].st_cval,
597 acpi_pstates[i].st_sval);
602 * Make sure that P-State tables are same
603 * for all processors.
605 if (memcmp(pstate, acpi_pstates,
606 sizeof(*pstate) * npstate) != 0) {
607 device_printf(dev, "Inconsistent _PSS "
608 "cross Processor objects\n");
609 kfree(pstate, M_TEMP);
612 kfree(pstate, M_TEMP);
616 /* By default, we start from P-State table's first entry */
620 * Adjust the usable first entry of P-State table,
621 * if there is _PPC object.
624 buf.Length = ACPI_ALLOCATE_BUFFER;
625 status = AcpiEvaluateObject(sc->pst_handle, "_PPC", NULL, &buf);
626 if (!ACPI_FAILURE(status)) {
627 obj = (ACPI_OBJECT *)buf.Pointer;
628 if (obj->Type == ACPI_TYPE_INTEGER) {
629 if (obj->Integer.Value >= acpi_npstates) {
630 device_printf(dev, "Invalid _PPC value\n");
634 sc->pst_sstart = obj->Integer.Value;
636 device_printf(dev, "_PPC %d\n", sc->pst_sstart);
638 /* TODO: Install notifiy handler */
640 device_printf(dev, "Invalid _PPC object\n");
649 sc->pst_state = sc->pst_sstart;
652 * Some CPUs only have package P-states, but some BIOSes put each
653 * hyperthread to its own P-state domain; allow user to override.
655 if (LIST_EMPTY(&dom->pd_pstlist) && acpi_pst_force_pkg_domain) {
658 mask = get_cpumask_from_level(sc->pst_cpuid, CHIP_LEVEL);
659 if (CPUMASK_TESTNZERO(mask)) {
660 struct acpi_pst_softc *pst = NULL;
661 struct acpi_pst_domain *dom1;
663 LIST_FOREACH(dom1, &acpi_pst_domains, pd_link) {
664 LIST_FOREACH(pst, &dom1->pd_pstlist,
666 if (CPUMASK_TESTBIT(mask,
674 memcmp(&pst->pst_creg, &sc->pst_creg,
675 sizeof(sc->pst_creg)) == 0 &&
676 memcmp(&pst->pst_sreg, &sc->pst_sreg,
677 sizeof(sc->pst_sreg)) == 0) {
679 * Use the same domain for CPUs in the
682 device_printf(dev, "Destroy domain%u, "
683 "force pkg domain%u\n",
684 dom->pd_dom, dom1->pd_dom);
685 LIST_REMOVE(dom, pd_link);
686 kfree(dom, M_DEVBUF);
689 * Make sure that adding us will not
690 * overflow the domain containing
691 * siblings in the same package.
693 acpi_pst_domain_check_nproc(dev, dom);
698 /* Link us with the domain */
699 sc->pst_domain = dom;
700 LIST_INSERT_HEAD(&dom->pd_pstlist, sc, pst_link);
702 if (device_get_unit(dev) == 0)
703 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_pst_postattach, NULL);
708 static struct acpi_pst_domain *
709 acpi_pst_domain_create_pkg(device_t dev, ACPI_OBJECT *obj)
711 struct acpi_pst_domain *dom;
712 uint32_t val, domain, coord, nproc;
714 if (!ACPI_PKG_VALID_EQ(obj, 5)) {
715 device_printf(dev, "Invalid _PSD package\n");
719 /* NumberOfEntries */
720 if (acpi_PkgInt32(obj, 0, &val) != 0 || val != 5) {
721 device_printf(dev, "Invalid _PSD NumberOfEntries\n");
726 if (acpi_PkgInt32(obj, 1, &val) != 0 || val != 0) {
727 device_printf(dev, "Invalid _PSD Revision\n");
731 if (acpi_PkgInt32(obj, 2, &domain) != 0 ||
732 acpi_PkgInt32(obj, 3, &coord) != 0 ||
733 acpi_PkgInt32(obj, 4, &nproc) != 0) {
734 device_printf(dev, "Can't extract _PSD package\n");
738 if (!ACPI_PSD_COORD_VALID(coord)) {
739 device_printf(dev, "Invalid _PSD CoordType (%#x)\n", coord);
743 if (nproc > MAXCPU) {
745 * If NumProcessors is greater than MAXCPU
746 * and domain's coordination is SWALL, then
747 * we will never be able to start all CPUs
748 * within this domain, and power state
749 * transition will never be completed, so we
750 * just bail out here.
752 if (coord == ACPI_PSD_COORD_SWALL) {
753 device_printf(dev, "Unsupported _PSD NumProcessors "
757 } else if (nproc == 0) {
758 device_printf(dev, "_PSD NumProcessors are zero\n");
762 dom = acpi_pst_domain_find(domain);
764 if (dom->pd_flags & ACPI_PSTDOM_FLAG_INT) {
765 device_printf(dev, "Mixed Integer _PSD and "
769 if (dom->pd_coord != coord) {
770 device_printf(dev, "Inconsistent _PSD coord "
771 "information cross Processor objects\n");
774 if (dom->pd_nproc != nproc) {
775 device_printf(dev, "Inconsistent _PSD nproc "
776 "information cross Processor objects\n");
778 * Some stupid BIOSes will set wrong "# of processors",
779 * e.g. 1 for CPU w/ hyperthreading; Be lenient here.
785 dom = acpi_pst_domain_alloc(domain, coord, nproc);
787 device_printf(dev, "create pkg domain%u, coord %#x\n",
788 dom->pd_dom, dom->pd_coord);
794 static struct acpi_pst_domain *
795 acpi_pst_domain_create_int(device_t dev, uint32_t domain)
797 struct acpi_pst_domain *dom;
799 dom = acpi_pst_domain_find(domain);
801 if ((dom->pd_flags & ACPI_PSTDOM_FLAG_INT) == 0) {
802 device_printf(dev, "Mixed Package _PSD and "
806 KKASSERT(dom->pd_coord == ACPI_PSD_COORD_SWALL);
812 dom = acpi_pst_domain_alloc(domain, ACPI_PSD_COORD_SWALL, 1);
813 dom->pd_flags |= ACPI_PSTDOM_FLAG_INT;
816 device_printf(dev, "create int domain%u\n", dom->pd_dom);
821 static struct acpi_pst_domain *
822 acpi_pst_domain_find(uint32_t domain)
824 struct acpi_pst_domain *dom;
826 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
827 if (dom->pd_flags & ACPI_PSTDOM_FLAG_STUB)
829 if (dom->pd_dom == domain)
835 static struct acpi_pst_domain *
836 acpi_pst_domain_alloc(uint32_t domain, uint32_t coord, uint32_t nproc)
838 struct acpi_pst_domain *dom;
840 dom = kmalloc(sizeof(*dom), M_DEVBUF, M_WAITOK | M_ZERO);
841 dom->pd_dom = domain;
842 dom->pd_coord = coord;
843 dom->pd_nproc = nproc;
844 dom->pd_state = 0; /* XXX */
845 dom->pd_sstart = 0; /* XXX */
846 LIST_INIT(&dom->pd_pstlist);
848 LIST_INSERT_HEAD(&acpi_pst_domains, dom, pd_link);
854 acpi_pst_domain_set_pstate(struct acpi_pst_domain *dom, int i)
856 const struct acpi_pstate *pstate;
857 struct acpi_pst_softc *sc;
860 KKASSERT(i >= 0 && i < acpi_npstates);
861 pstate = &acpi_pstates[i];
864 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
866 error = acpi_pst_set_pstate(sc, pstate);
868 device_printf(sc->pst_dev, "can't set "
869 "freq %d\n", pstate->st_freq);
870 /* XXX error cleanup? */
872 if (dom->pd_coord == ACPI_PSD_COORD_SWANY)
883 acpi_pst_global_set_pstate(int i)
885 struct acpi_pst_domain *dom;
887 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
888 /* Skip dead domain */
889 if (dom->pd_flags & ACPI_PSTDOM_FLAG_DEAD)
891 acpi_pst_domain_set_pstate(dom, i);
893 acpi_pst_global_state = i;
899 acpi_pst_postattach(void *arg __unused)
901 struct acpi_pst_domain *dom;
902 struct acpi_cpu_softc *cpu;
904 int i, ndevices, error, has_domain, sstate;
908 error = devclass_get_devices(acpi_pst_devclass, &devices, &ndevices);
916 for (i = 0; i < ndevices; ++i) {
917 cpu = device_get_softc(device_get_parent(devices[i]));
918 if (cpu->glob_sysctl_tree != NULL)
921 kfree(devices, M_TEMP);
922 KKASSERT(cpu != NULL);
924 if (acpi_pst_md == NULL)
925 kprintf("ACPI: no P-State CPU driver\n");
929 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
930 struct acpi_pst_softc *sc;
934 * Make sure that all processors belonging to this
935 * domain are located.
938 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link)
940 if (i != dom->pd_nproc) {
941 KKASSERT(i < dom->pd_nproc);
943 kprintf("ACPI: domain%u misses processors, "
944 "should be %d, got %d\n", dom->pd_dom,
946 if (dom->pd_coord == ACPI_PSD_COORD_SWALL) {
948 * If this domain's coordination is
949 * SWALL and we don't see all of the
950 * member CPUs of this domain, then
951 * the P-State transition will never
952 * be completed, so just leave this
955 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
962 * Validate P-State configurations for this domain
964 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
965 error = acpi_pst_check_csr(sc);
969 error = acpi_pst_check_pstates(sc);
974 kprintf("ACPI: domain%u P-State configuration "
975 "check failed\n", dom->pd_dom);
976 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
981 * Do necssary P-State initialization
983 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
984 error = acpi_pst_init(sc);
989 kprintf("ACPI: domain%u P-State initialization "
990 "check failed\n", dom->pd_dom);
991 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
997 ksnprintf(buf, sizeof(buf), "px_dom%u", dom->pd_dom);
999 sysctl_ctx_init(&dom->pd_sysctl_ctx);
1000 dom->pd_sysctl_tree =
1001 SYSCTL_ADD_NODE(&dom->pd_sysctl_ctx,
1002 SYSCTL_CHILDREN(cpu->glob_sysctl_tree),
1003 OID_AUTO, buf, CTLFLAG_RD, 0,
1005 if (dom->pd_sysctl_tree == NULL) {
1006 kprintf("ACPI: Can't create sysctl tree for domain%u",
1011 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
1012 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
1013 OID_AUTO, "available",
1014 CTLTYPE_STRING | CTLFLAG_RD,
1015 dom, 0, acpi_pst_sysctl_freqs, "A",
1016 "available frequencies");
1018 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
1019 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
1020 OID_AUTO, "members",
1021 CTLTYPE_STRING | CTLFLAG_RD,
1022 dom, 0, acpi_pst_sysctl_members, "A",
1025 if (acpi_pst_md != NULL &&
1026 acpi_pst_md->pmd_set_pstate != NULL) {
1027 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
1028 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
1030 CTLTYPE_UINT | CTLFLAG_RW,
1031 dom, 0, acpi_pst_sysctl_select,
1032 "IU", "select freq");
1035 if (dom->pd_state < sstate)
1036 sstate = dom->pd_state;
1039 if (has_domain && acpi_pst_md != NULL &&
1040 acpi_pst_md->pmd_set_pstate != NULL) {
1041 SYSCTL_ADD_PROC(&cpu->glob_sysctl_ctx,
1042 SYSCTL_CHILDREN(cpu->glob_sysctl_tree),
1043 OID_AUTO, "px_global",
1044 CTLTYPE_UINT | CTLFLAG_RW,
1045 NULL, 0, acpi_pst_sysctl_global,
1046 "IU", "select freq for all domains");
1048 acpi_pst_global_set_pstate(sstate);
1053 acpi_pst_sysctl_freqs(SYSCTL_HANDLER_ARGS)
1055 struct acpi_pst_domain *dom = arg1;
1059 for (i = 0; i < acpi_npstates; ++i) {
1060 if (error == 0 && i)
1061 error = SYSCTL_OUT(req, " ", 1);
1066 if (i < dom->pd_sstart)
1071 ksnprintf(buf, sizeof(buf), pat,
1072 acpi_pstates[i].st_freq);
1073 error = SYSCTL_OUT(req, buf, strlen(buf));
1080 acpi_pst_sysctl_members(SYSCTL_HANDLER_ARGS)
1082 struct acpi_pst_domain *dom = arg1;
1083 struct acpi_pst_softc *sc;
1087 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
1090 if (error == 0 && loop)
1091 error = SYSCTL_OUT(req, " ", 1);
1093 ksnprintf(buf, sizeof(buf), "cpu%d", sc->pst_cpuid);
1094 error = SYSCTL_OUT(req, buf, strlen(buf));
1097 if (error == 0 && acpi_pst_md && acpi_pst_md->pmd_get_pstate) {
1098 const struct acpi_pstate *pstate;
1101 pstate = acpi_pst_get_pstate(sc);
1102 if (pstate == NULL) {
1105 ksnprintf(buf, sizeof(buf), "(%d)",
1109 error = SYSCTL_OUT(req, str, strlen(str));
1117 acpi_pst_sysctl_select(SYSCTL_HANDLER_ARGS)
1119 struct acpi_pst_domain *dom = arg1;
1122 KKASSERT(dom->pd_state >= 0 && dom->pd_state < acpi_npstates);
1124 freq = acpi_pstates[dom->pd_state].st_freq;
1126 error = sysctl_handle_int(oidp, &freq, 0, req);
1127 if (error || req->newptr == NULL)
1130 i = acpi_pst_freq2index(freq);
1134 acpi_pst_domain_set_pstate(dom, i);
1139 acpi_pst_sysctl_global(SYSCTL_HANDLER_ARGS)
1143 KKASSERT(acpi_pst_global_state >= 0 &&
1144 acpi_pst_global_state < acpi_npstates);
1146 freq = acpi_pstates[acpi_pst_global_state].st_freq;
1148 error = sysctl_handle_int(oidp, &freq, 0, req);
1149 if (error || req->newptr == NULL)
1152 i = acpi_pst_freq2index(freq);
1156 acpi_pst_global_set_pstate(i);
1162 acpi_pst_check_csr_handler(netmsg_t msg)
1164 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1167 error = acpi_pst_md->pmd_check_csr(rmsg->ctrl, rmsg->status);
1168 lwkt_replymsg(&rmsg->base.lmsg, error);
1172 acpi_pst_check_csr(struct acpi_pst_softc *sc)
1174 struct netmsg_acpi_pst msg;
1176 if (acpi_pst_md == NULL)
1179 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1180 MSGF_PRIORITY, acpi_pst_check_csr_handler);
1181 msg.ctrl = &sc->pst_creg;
1182 msg.status = &sc->pst_sreg;
1184 return lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.base.lmsg, 0);
1188 acpi_pst_check_pstates_handler(netmsg_t msg)
1192 error = acpi_pst_md->pmd_check_pstates(acpi_pstates, acpi_npstates);
1193 lwkt_replymsg(&msg->lmsg, error);
1197 acpi_pst_check_pstates(struct acpi_pst_softc *sc)
1199 struct netmsg_base msg;
1201 if (acpi_pst_md == NULL)
1204 netmsg_init(&msg, NULL, &curthread->td_msgport,
1205 MSGF_PRIORITY, acpi_pst_check_pstates_handler);
1207 return lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.lmsg, 0);
1211 acpi_pst_init_handler(netmsg_t msg)
1213 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1216 error = acpi_pst_md->pmd_init(rmsg->ctrl, rmsg->status);
1217 lwkt_replymsg(&rmsg->base.lmsg, error);
1221 acpi_pst_init(struct acpi_pst_softc *sc)
1223 struct netmsg_acpi_pst msg;
1225 if (acpi_pst_md == NULL)
1228 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1229 MSGF_PRIORITY, acpi_pst_init_handler);
1230 msg.ctrl = &sc->pst_creg;
1231 msg.status = &sc->pst_sreg;
1233 return lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.base.lmsg, 0);
1237 acpi_pst_set_pstate_handler(netmsg_t msg)
1239 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1242 error = acpi_pst_md->pmd_set_pstate(rmsg->ctrl, rmsg->status,
1243 rmsg->base.lmsg.u.ms_resultp);
1244 lwkt_replymsg(&rmsg->base.lmsg, error);
1248 acpi_pst_set_pstate(struct acpi_pst_softc *sc, const struct acpi_pstate *pstate)
1250 struct netmsg_acpi_pst msg;
1252 KKASSERT(acpi_pst_md != NULL);
1254 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1255 MSGF_PRIORITY, acpi_pst_set_pstate_handler);
1256 msg.base.lmsg.u.ms_resultp = __DECONST(void *, pstate);
1257 msg.ctrl = &sc->pst_creg;
1258 msg.status = &sc->pst_sreg;
1260 return lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.base.lmsg, 0);
1264 acpi_pst_get_pstate_handler(netmsg_t msg)
1266 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1267 const struct acpi_pstate *pstate;
1269 pstate = acpi_pst_md->pmd_get_pstate(rmsg->status, acpi_pstates,
1271 rmsg->base.lmsg.u.ms_resultp = __DECONST(void *, pstate);
1272 lwkt_replymsg(&rmsg->base.lmsg, 0);
1275 static const struct acpi_pstate *
1276 acpi_pst_get_pstate(struct acpi_pst_softc *sc)
1278 struct netmsg_acpi_pst msg;
1280 if (acpi_pst_md == NULL)
1283 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1284 MSGF_PRIORITY, acpi_pst_get_pstate_handler);
1285 msg.status = &sc->pst_sreg;
1287 lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.base.lmsg, 0);
1288 return msg.base.lmsg.u.ms_resultp;
1292 acpi_pst_alloc_resource(device_t dev, ACPI_OBJECT *obj, int idx,
1293 struct acpi_pst_res *res)
1295 struct acpi_pst_softc *sc = device_get_softc(dev);
1299 error = acpi_PkgRawGas(obj, idx, &res->pr_gas);
1303 /* Allocate resource, if possible */
1304 res->pr_rid = sc->pst_parent->cpu_next_rid;
1305 acpi_bus_alloc_gas(dev, &type, &res->pr_rid, &res->pr_gas, &res->pr_res, 0);
1306 if (res->pr_res != NULL) {
1307 sc->pst_parent->cpu_next_rid++;
1308 res->pr_bt = rman_get_bustag(res->pr_res);
1309 res->pr_bh = rman_get_bushandle(res->pr_res);
1317 acpi_pst_domain_check_nproc(device_t dev, struct acpi_pst_domain *dom)
1319 struct acpi_pst_softc *pst;
1323 LIST_FOREACH(pst, &dom->pd_pstlist, pst_link)
1325 if (i == dom->pd_nproc) {
1327 * Some stupid BIOSes will set wrong "# of processors",
1328 * e.g. 1 for CPU w/ hyperthreading; Be lenient here.
1331 device_printf(dev, "domain%u already contains %d "
1332 "P-States\n", dom->pd_dom, dom->pd_nproc);
1336 KKASSERT(i < dom->pd_nproc);