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 32
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;
86 struct acpi_pst_list pd_pstlist;
88 struct sysctl_ctx_list pd_sysctl_ctx;
89 struct sysctl_oid *pd_sysctl_tree;
91 LIST_HEAD(acpi_pst_domlist, acpi_pst_domain);
93 #define ACPI_PSTDOM_FLAG_STUB 0x1 /* stub domain, no _PSD */
94 #define ACPI_PSTDOM_FLAG_DEAD 0x2 /* domain can't be started */
95 #define ACPI_PSTDOM_FLAG_INT 0x4 /* domain created from Integer _PSD */
97 struct acpi_pst_softc {
99 struct acpi_cpu_softc *pst_parent;
100 struct acpi_pst_domain *pst_domain;
101 struct acpi_pst_res pst_creg;
102 struct acpi_pst_res pst_sreg;
107 ACPI_HANDLE pst_handle;
109 LIST_ENTRY(acpi_pst_softc) pst_link;
112 static int acpi_pst_probe(device_t dev);
113 static int acpi_pst_attach(device_t dev);
115 static void acpi_pst_postattach(void *);
116 static struct acpi_pst_domain *
117 acpi_pst_domain_create_int(device_t, uint32_t);
118 static struct acpi_pst_domain *
119 acpi_pst_domain_create_pkg(device_t, ACPI_OBJECT *);
120 static struct acpi_pst_domain *
121 acpi_pst_domain_find(uint32_t);
122 static struct acpi_pst_domain *
123 acpi_pst_domain_alloc(uint32_t, uint32_t, uint32_t);
124 static int acpi_pst_domain_set_pstate(struct acpi_pst_domain *, int);
125 static void acpi_pst_domain_check_nproc(device_t, struct acpi_pst_domain *);
126 static int acpi_pst_global_set_pstate(int);
128 static int acpi_pst_check_csr(struct acpi_pst_softc *);
129 static int acpi_pst_check_pstates(struct acpi_pst_softc *);
130 static int acpi_pst_init(struct acpi_pst_softc *);
131 static int acpi_pst_set_pstate(struct acpi_pst_softc *,
132 const struct acpi_pstate *);
133 static const struct acpi_pstate *
134 acpi_pst_get_pstate(struct acpi_pst_softc *);
135 static int acpi_pst_alloc_resource(device_t, ACPI_OBJECT *, int,
136 struct acpi_pst_res *);
138 static void acpi_pst_check_csr_handler(netmsg_t);
139 static void acpi_pst_check_pstates_handler(netmsg_t);
140 static void acpi_pst_init_handler(netmsg_t);
141 static void acpi_pst_set_pstate_handler(netmsg_t);
142 static void acpi_pst_get_pstate_handler(netmsg_t);
144 static int acpi_pst_sysctl_freqs(SYSCTL_HANDLER_ARGS);
145 static int acpi_pst_sysctl_freqs_bin(SYSCTL_HANDLER_ARGS);
146 static int acpi_pst_sysctl_power(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_pstate_start = -1;
158 static int acpi_pstate_count;
159 static int acpi_npstates;
160 static struct acpi_pstate *acpi_pstates;
162 static const struct acpi_pst_md *acpi_pst_md;
164 static int acpi_pst_pdl = -1;
165 TUNABLE_INT("hw.acpi.cpu.pst.pdl", &acpi_pst_pdl);
167 static int acpi_pst_ht_reuse_domain = 1;
168 TUNABLE_INT("hw.acpi.cpu.pst.ht_reuse_domain", &acpi_pst_ht_reuse_domain);
170 static int acpi_pst_force_pkg_domain = 0;
171 TUNABLE_INT("hw.acpi.cpu.pst.force_pkg_domain", &acpi_pst_force_pkg_domain);
173 static device_method_t acpi_pst_methods[] = {
174 /* Device interface */
175 DEVMETHOD(device_probe, acpi_pst_probe),
176 DEVMETHOD(device_attach, acpi_pst_attach),
177 DEVMETHOD(device_detach, bus_generic_detach),
178 DEVMETHOD(device_shutdown, bus_generic_shutdown),
179 DEVMETHOD(device_suspend, bus_generic_suspend),
180 DEVMETHOD(device_resume, bus_generic_resume),
183 DEVMETHOD(bus_add_child, bus_generic_add_child),
184 DEVMETHOD(bus_print_child, bus_generic_print_child),
185 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar),
186 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar),
187 DEVMETHOD(bus_get_resource_list, bus_generic_get_resource_list),
188 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
189 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
190 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
191 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
192 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
193 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
194 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
195 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
196 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
201 static driver_t acpi_pst_driver = {
204 sizeof(struct acpi_pst_softc)
207 static devclass_t acpi_pst_devclass;
208 DRIVER_MODULE(cpu_pst, cpu, acpi_pst_driver, acpi_pst_devclass, NULL, NULL);
209 MODULE_DEPEND(cpu_pst, acpi, 1, 1, 1);
212 acpi_pst_freq2index(int freq)
216 for (i = 0; i < acpi_npstates; ++i) {
217 if (acpi_pstates[i].st_freq == freq)
224 acpi_pst_probe(device_t dev)
231 if (acpi_disabled("cpu_pst") ||
232 acpi_get_type(dev) != ACPI_TYPE_PROCESSOR)
235 if (acpi_pst_md == NULL)
236 acpi_pst_md = acpi_pst_md_probe();
238 handle = acpi_get_handle(dev);
244 * Some BIOSes do not expose _PCT for the second thread of
245 * CPU cores. In this case, _PSD should be enough to get the
246 * P-state of the second thread working, since it must have
247 * the same _PCT and _PSS as the first thread in the same
251 buf.Length = ACPI_ALLOCATE_BUFFER;
252 status = AcpiEvaluateObject(handle, "_PSD", NULL, &buf);
253 if (!ACPI_FAILURE(status)) {
254 AcpiOsFree((ACPI_OBJECT *)buf.Pointer);
262 buf.Length = ACPI_ALLOCATE_BUFFER;
263 status = AcpiEvaluateObject(handle, "_PCT", NULL, &buf);
264 if (ACPI_FAILURE(status)) {
266 device_printf(dev, "Can't get _PCT package - %s\n",
267 AcpiFormatException(status));
272 obj = (ACPI_OBJECT *)buf.Pointer;
273 if (!ACPI_PKG_VALID_EQ(obj, 2)) {
274 device_printf(dev, "Invalid _PCT package\n");
284 buf.Length = ACPI_ALLOCATE_BUFFER;
285 status = AcpiEvaluateObject(handle, "_PSS", NULL, &buf);
286 if (ACPI_FAILURE(status)) {
287 device_printf(dev, "Can't get _PSS package - %s\n",
288 AcpiFormatException(status));
292 obj = (ACPI_OBJECT *)buf.Pointer;
293 if (!ACPI_PKG_VALID(obj, 1)) {
294 device_printf(dev, "Invalid _PSS package\n");
301 device_set_desc(dev, "ACPI CPU P-State");
306 acpi_pst_attach(device_t dev)
308 struct acpi_pst_softc *sc = device_get_softc(dev);
309 struct acpi_pst_domain *dom = NULL;
313 struct acpi_pstate *pstate, *p;
314 int i, npstate, error, sstart, scount;
317 sc->pst_parent = device_get_softc(device_get_parent(dev));
318 sc->pst_handle = acpi_get_handle(dev);
319 sc->pst_cpuid = acpi_get_magic(dev);
322 * If there is a _PSD, then we create procossor domain
323 * accordingly. If there is no _PSD, we just fake a
324 * default processor domain0.
327 buf.Length = ACPI_ALLOCATE_BUFFER;
328 status = AcpiEvaluateObject(sc->pst_handle, "_PSD", NULL, &buf);
329 if (!ACPI_FAILURE(status)) {
330 obj = (ACPI_OBJECT *)buf.Pointer;
332 if (acpi_pst_domain_id > 0) {
333 device_printf(dev, "Missing _PSD for certain CPUs\n");
337 acpi_pst_domain_id = -1;
339 if (ACPI_PKG_VALID_EQ(obj, 1)) {
340 dom = acpi_pst_domain_create_pkg(dev,
341 &obj->Package.Elements[0]);
347 if (obj->Type != ACPI_TYPE_INTEGER) {
349 "Invalid _PSD package, Type 0x%x\n",
354 device_printf(dev, "Integer _PSD %ju\n",
355 (uintmax_t)obj->Integer.Value);
356 dom = acpi_pst_domain_create_int(dev,
366 AcpiOsFree(buf.Pointer);
368 if (acpi_pst_domain_id < 0) {
369 device_printf(dev, "Missing _PSD for cpu%d\n",
375 * Create a stub one processor domain for each processor
377 dom = acpi_pst_domain_alloc(acpi_pst_domain_id,
378 ACPI_PSD_COORD_SWANY, 1);
379 dom->pd_flags |= ACPI_PSTDOM_FLAG_STUB;
381 ++acpi_pst_domain_id;
384 /* Make sure that adding us will not overflow our domain */
385 acpi_pst_domain_check_nproc(dev, dom);
388 * Get control/status registers from _PCT
391 buf.Length = ACPI_ALLOCATE_BUFFER;
392 status = AcpiEvaluateObject(sc->pst_handle, "_PCT", NULL, &buf);
393 if (ACPI_FAILURE(status)) {
394 struct acpi_pst_softc *pst;
397 * No _PCT. See the comment in acpi_pst_probe() near
400 * Use control/status registers of another CPU in the
401 * same domain, or in the same core, if the type of
402 * these registers are "Fixed Hardware", e.g. on most
403 * of the model Intel CPUs.
405 pst = LIST_FIRST(&dom->pd_pstlist);
409 mask = get_cpumask_from_level(sc->pst_cpuid,
411 if (CPUMASK_TESTNZERO(mask)) {
412 struct acpi_pst_domain *dom1;
414 LIST_FOREACH(dom1, &acpi_pst_domains, pd_link) {
415 LIST_FOREACH(pst, &dom1->pd_pstlist,
417 if (CPUMASK_TESTBIT(mask,
424 if (pst != NULL && acpi_pst_ht_reuse_domain) {
426 * Use the same domain for CPUs in the
429 device_printf(dev, "Destroy domain%u, "
431 dom->pd_dom, dom1->pd_dom);
432 LIST_REMOVE(dom, pd_link);
433 kfree(dom, M_DEVBUF);
436 * Make sure that adding us will not
437 * overflow the domain containing
438 * siblings in the same core.
440 acpi_pst_domain_check_nproc(dev, dom);
445 pst->pst_creg.pr_res == NULL &&
446 pst->pst_creg.pr_rid == 0 &&
447 pst->pst_creg.pr_gas.SpaceId ==
448 ACPI_ADR_SPACE_FIXED_HARDWARE &&
449 pst->pst_sreg.pr_res == NULL &&
450 pst->pst_sreg.pr_rid == 0 &&
451 pst->pst_sreg.pr_gas.SpaceId ==
452 ACPI_ADR_SPACE_FIXED_HARDWARE) {
453 sc->pst_creg = pst->pst_creg;
454 sc->pst_sreg = pst->pst_sreg;
456 "No _PCT; reuse %s control/status regs\n",
457 device_get_nameunit(pst->pst_dev));
460 device_printf(dev, "Can't get _PCT package - %s\n",
461 AcpiFormatException(status));
465 obj = (ACPI_OBJECT *)buf.Pointer;
466 if (!ACPI_PKG_VALID_EQ(obj, 2)) {
467 device_printf(dev, "Invalid _PCT package\n");
472 /* Save and try allocating control register */
473 error = acpi_pst_alloc_resource(dev, obj, 0, &sc->pst_creg);
479 device_printf(dev, "control reg %d %jx\n",
480 sc->pst_creg.pr_gas.SpaceId,
481 (uintmax_t)sc->pst_creg.pr_gas.Address);
484 /* Save and try allocating status register */
485 error = acpi_pst_alloc_resource(dev, obj, 1, &sc->pst_sreg);
491 device_printf(dev, "status reg %d %jx\n",
492 sc->pst_sreg.pr_gas.SpaceId,
493 (uintmax_t)sc->pst_sreg.pr_gas.Address);
501 * Create P-State table according to _PSS
504 buf.Length = ACPI_ALLOCATE_BUFFER;
505 status = AcpiEvaluateObject(sc->pst_handle, "_PSS", NULL, &buf);
506 if (ACPI_FAILURE(status)) {
508 * No _PSS. See the comment in acpi_pst_probe() near
511 * Assume _PSS are same across all CPUs; well, they
512 * should/have to be so.
514 if (acpi_npstates > 0 && acpi_pstates != NULL) {
515 device_printf(dev, "No _PSS\n");
518 device_printf(dev, "Can't get _PSS package - %s\n",
519 AcpiFormatException(status));
523 obj = (ACPI_OBJECT *)buf.Pointer;
524 if (!ACPI_PKG_VALID(obj, 1)) {
525 device_printf(dev, "Invalid _PSS package\n");
530 /* Don't create too many P-States */
531 npstate = obj->Package.Count;
532 if (npstate > ACPI_NPSTATE_MAX) {
533 device_printf(dev, "Too many P-States, %d->%d\n",
534 npstate, ACPI_NPSTATE_MAX);
535 npstate = ACPI_NPSTATE_MAX;
539 * If we have already created P-State table,
540 * we must make sure that number of entries
543 if (acpi_pstates != NULL && acpi_npstates != npstate) {
544 device_printf(dev, "Inconsistent # of P-States "
545 "cross Processor objects\n");
551 * Create a temporary P-State table
553 pstate = kmalloc(sizeof(*pstate) * npstate, M_TEMP, M_WAITOK);
554 for (i = 0, p = pstate; i < npstate; ++i, ++p) {
556 uint32_t *ptr[ACPI_PSS_PX_NENTRY] = {
557 &p->st_freq, &p->st_power, &p->st_xsit_lat,
558 &p->st_bm_lat, &p->st_cval, &p->st_sval
562 pkg = &obj->Package.Elements[i];
563 if (!ACPI_PKG_VALID(pkg, ACPI_PSS_PX_NENTRY)) {
564 device_printf(dev, "Invalud _PSS P%d\n", i);
566 kfree(pstate, M_TEMP);
569 for (j = 0; j < ACPI_PSS_PX_NENTRY; ++j) {
570 if (acpi_PkgInt32(pkg, j, ptr[j]) != 0) {
571 device_printf(dev, "Can't extract "
572 "_PSS P%d %dth entry\n", i, j);
574 kfree(pstate, M_TEMP);
583 if (acpi_pstates == NULL) {
585 * If no P-State table is created yet,
586 * save the temporary one we just created.
588 acpi_pstates = pstate;
589 acpi_npstates = npstate;
593 for (i = 0; i < acpi_npstates; ++i) {
595 "freq %u, pwr %u, xlat %u, blat %u, "
596 "cv %08x, sv %08x\n",
597 acpi_pstates[i].st_freq,
598 acpi_pstates[i].st_power,
599 acpi_pstates[i].st_xsit_lat,
600 acpi_pstates[i].st_bm_lat,
601 acpi_pstates[i].st_cval,
602 acpi_pstates[i].st_sval);
607 * Make sure that P-State tables are same
608 * for all processors.
610 if (memcmp(pstate, acpi_pstates,
611 sizeof(*pstate) * npstate) != 0) {
612 device_printf(dev, "Inconsistent _PSS "
613 "cross Processor objects\n");
616 * Some BIOSes create different P-State tables;
617 * just trust the one from the BSP and move on.
619 kfree(pstate, M_TEMP);
623 kfree(pstate, M_TEMP);
627 /* By default, we start from P-State table's first entry */
631 * Adjust the usable first entry of P-State table,
632 * if there is _PPC object.
635 buf.Length = ACPI_ALLOCATE_BUFFER;
636 status = AcpiEvaluateObject(sc->pst_handle, "_PPC", NULL, &buf);
637 if (!ACPI_FAILURE(status)) {
638 ACPI_OBJECT_LIST arglist;
641 obj = (ACPI_OBJECT *)buf.Pointer;
642 if (obj->Type == ACPI_TYPE_INTEGER) {
643 if (obj->Integer.Value >= acpi_npstates) {
644 device_printf(dev, "Invalid _PPC value\n");
648 sstart = obj->Integer.Value;
650 device_printf(dev, "_PPC %d\n", sstart);
652 /* TODO: Install notifiy handler */
654 device_printf(dev, "Invalid _PPC object\n");
662 /* _PPC has been successfully processed */
663 arglist.Pointer = arg;
665 arg[0].Type = ACPI_TYPE_INTEGER;
666 arg[0].Integer.Value = 0x80;
667 arg[1].Type = ACPI_TYPE_INTEGER;
668 arg[1].Integer.Value = 0;
669 AcpiEvaluateObject(sc->pst_handle, "_OST", &arglist, NULL);
671 if (acpi_pstate_start < 0) {
672 acpi_pstate_start = sstart;
673 } else if (acpi_pstate_start != sstart) {
674 device_printf(dev, "_PPC mismatch, was %d, now %d\n",
675 acpi_pstate_start, sstart);
676 if (acpi_pstate_start < sstart) {
677 device_printf(dev, "_PPC %d -> %d\n",
678 acpi_pstate_start, sstart);
679 acpi_pstate_start = sstart;
684 * By default, we assume number of usable P-States is same as
685 * number of P-States.
687 scount = acpi_npstates;
690 * Allow users to override or set _PDL
692 if (acpi_pst_pdl >= 0) {
693 if (acpi_pst_pdl < acpi_npstates) {
695 device_printf(dev, "_PDL override %d\n",
698 scount = acpi_pst_pdl + 1;
701 device_printf(dev, "Invalid _PDL override %d, "
702 "must be less than %d\n", acpi_pst_pdl,
708 * Adjust the number of usable entries in P-State table,
709 * if there is _PDL object.
712 buf.Length = ACPI_ALLOCATE_BUFFER;
713 status = AcpiEvaluateObject(sc->pst_handle, "_PDL", NULL, &buf);
714 if (!ACPI_FAILURE(status)) {
715 obj = (ACPI_OBJECT *)buf.Pointer;
716 if (obj->Type == ACPI_TYPE_INTEGER) {
717 if (obj->Integer.Value >= acpi_npstates) {
718 device_printf(dev, "Invalid _PDL value\n");
722 if (obj->Integer.Value >= acpi_pstate_start) {
723 scount = obj->Integer.Value + 1;
725 device_printf(dev, "_PDL %d\n", scount);
727 /* Prefer _PPC as stated in ACPI 5.1 8.4.4.6 */
728 device_printf(dev, "conflict _PDL %ju and "
730 (uintmax_t)obj->Integer.Value,
734 /* TODO: Install notifiy handler */
736 device_printf(dev, "Invalid _PDL object\n");
745 if (acpi_pstate_count == 0) {
746 acpi_pstate_count = scount;
747 } else if (acpi_pstate_count != scount) {
748 device_printf(dev, "_PDL mismatch, was %d, now %d\n",
749 acpi_pstate_count, scount);
750 if (acpi_pstate_count > scount) {
751 device_printf(dev, "_PDL %d -> %d\n",
752 acpi_pstate_count, scount);
753 acpi_pstate_count = scount;
758 * Some CPUs only have package P-states, but some BIOSes put each
759 * hyperthread to its own P-state domain; allow user to override.
761 if (LIST_EMPTY(&dom->pd_pstlist) && acpi_pst_force_pkg_domain) {
764 mask = get_cpumask_from_level(sc->pst_cpuid, CHIP_LEVEL);
765 if (CPUMASK_TESTNZERO(mask)) {
766 struct acpi_pst_softc *pst = NULL;
767 struct acpi_pst_domain *dom1;
769 LIST_FOREACH(dom1, &acpi_pst_domains, pd_link) {
770 LIST_FOREACH(pst, &dom1->pd_pstlist,
772 if (CPUMASK_TESTBIT(mask,
780 memcmp(&pst->pst_creg, &sc->pst_creg,
781 sizeof(sc->pst_creg)) == 0 &&
782 memcmp(&pst->pst_sreg, &sc->pst_sreg,
783 sizeof(sc->pst_sreg)) == 0) {
785 * Use the same domain for CPUs in the
788 device_printf(dev, "Destroy domain%u, "
789 "force pkg domain%u\n",
790 dom->pd_dom, dom1->pd_dom);
791 LIST_REMOVE(dom, pd_link);
792 kfree(dom, M_DEVBUF);
795 * Make sure that adding us will not
796 * overflow the domain containing
797 * siblings in the same package.
799 acpi_pst_domain_check_nproc(dev, dom);
804 /* Link us with the domain */
805 sc->pst_domain = dom;
806 LIST_INSERT_HEAD(&dom->pd_pstlist, sc, pst_link);
808 if (device_get_unit(dev) == 0)
809 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_pst_postattach, NULL);
814 static struct acpi_pst_domain *
815 acpi_pst_domain_create_pkg(device_t dev, ACPI_OBJECT *obj)
817 struct acpi_pst_domain *dom;
818 uint32_t val, domain, coord, nproc;
820 if (!ACPI_PKG_VALID_EQ(obj, 5)) {
821 device_printf(dev, "Invalid _PSD package\n");
825 /* NumberOfEntries */
826 if (acpi_PkgInt32(obj, 0, &val) != 0 || val != 5) {
827 device_printf(dev, "Invalid _PSD NumberOfEntries\n");
832 if (acpi_PkgInt32(obj, 1, &val) != 0 || val != 0) {
833 device_printf(dev, "Invalid _PSD Revision\n");
837 if (acpi_PkgInt32(obj, 2, &domain) != 0 ||
838 acpi_PkgInt32(obj, 3, &coord) != 0 ||
839 acpi_PkgInt32(obj, 4, &nproc) != 0) {
840 device_printf(dev, "Can't extract _PSD package\n");
844 if (!ACPI_PSD_COORD_VALID(coord)) {
845 device_printf(dev, "Invalid _PSD CoordType (%#x)\n", coord);
849 if (nproc > MAXCPU) {
851 * If NumProcessors is greater than MAXCPU
852 * and domain's coordination is SWALL, then
853 * we will never be able to start all CPUs
854 * within this domain, and power state
855 * transition will never be completed, so we
856 * just bail out here.
858 if (coord == ACPI_PSD_COORD_SWALL) {
859 device_printf(dev, "Unsupported _PSD NumProcessors "
863 } else if (nproc == 0) {
864 device_printf(dev, "_PSD NumProcessors are zero\n");
868 dom = acpi_pst_domain_find(domain);
870 if (dom->pd_flags & ACPI_PSTDOM_FLAG_INT) {
871 device_printf(dev, "Mixed Integer _PSD and "
875 if (dom->pd_coord != coord) {
876 device_printf(dev, "Inconsistent _PSD coord "
877 "information cross Processor objects\n");
880 if (dom->pd_nproc != nproc) {
881 device_printf(dev, "Inconsistent _PSD nproc "
882 "information cross Processor objects\n");
884 * Some stupid BIOSes will set wrong "# of processors",
885 * e.g. 1 for CPU w/ hyperthreading; Be lenient here.
891 dom = acpi_pst_domain_alloc(domain, coord, nproc);
893 device_printf(dev, "create pkg domain%u, coord %#x\n",
894 dom->pd_dom, dom->pd_coord);
900 static struct acpi_pst_domain *
901 acpi_pst_domain_create_int(device_t dev, uint32_t domain)
903 struct acpi_pst_domain *dom;
905 dom = acpi_pst_domain_find(domain);
907 if ((dom->pd_flags & ACPI_PSTDOM_FLAG_INT) == 0) {
908 device_printf(dev, "Mixed Package _PSD and "
912 KKASSERT(dom->pd_coord == ACPI_PSD_COORD_SWALL);
918 dom = acpi_pst_domain_alloc(domain, ACPI_PSD_COORD_SWALL, 1);
919 dom->pd_flags |= ACPI_PSTDOM_FLAG_INT;
922 device_printf(dev, "create int domain%u\n", dom->pd_dom);
927 static struct acpi_pst_domain *
928 acpi_pst_domain_find(uint32_t domain)
930 struct acpi_pst_domain *dom;
932 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
933 if (dom->pd_flags & ACPI_PSTDOM_FLAG_STUB)
935 if (dom->pd_dom == domain)
941 static struct acpi_pst_domain *
942 acpi_pst_domain_alloc(uint32_t domain, uint32_t coord, uint32_t nproc)
944 struct acpi_pst_domain *dom;
946 dom = kmalloc(sizeof(*dom), M_DEVBUF, M_WAITOK | M_ZERO);
947 dom->pd_dom = domain;
948 dom->pd_coord = coord;
949 dom->pd_nproc = nproc;
950 LIST_INIT(&dom->pd_pstlist);
952 LIST_INSERT_HEAD(&acpi_pst_domains, dom, pd_link);
958 acpi_pst_domain_set_pstate(struct acpi_pst_domain *dom, int i)
960 const struct acpi_pstate *pstate;
961 struct acpi_pst_softc *sc;
964 KKASSERT(i >= 0 && i < acpi_npstates);
965 pstate = &acpi_pstates[i];
968 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
970 error = acpi_pst_set_pstate(sc, pstate);
972 device_printf(sc->pst_dev, "can't set "
973 "freq %d\n", pstate->st_freq);
974 /* XXX error cleanup? */
976 if (dom->pd_coord == ACPI_PSD_COORD_SWANY)
987 acpi_pst_global_set_pstate(int i)
989 struct acpi_pst_domain *dom;
991 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
992 /* Skip dead domain */
993 if (dom->pd_flags & ACPI_PSTDOM_FLAG_DEAD)
995 acpi_pst_domain_set_pstate(dom, i);
997 acpi_pst_global_state = i;
1003 acpi_pst_postattach(void *arg __unused)
1005 struct acpi_pst_domain *dom;
1006 struct acpi_cpu_softc *cpu;
1008 int i, ndevices, error, has_domain;
1012 error = devclass_get_devices(acpi_pst_devclass, &devices, &ndevices);
1020 for (i = 0; i < ndevices; ++i) {
1021 cpu = device_get_softc(device_get_parent(devices[i]));
1022 if (cpu->glob_sysctl_tree != NULL)
1025 kfree(devices, M_TEMP);
1026 KKASSERT(cpu != NULL);
1028 if (acpi_pst_md == NULL)
1029 kprintf("ACPI: no P-State CPU driver\n");
1032 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
1033 struct acpi_pst_softc *sc;
1036 dom->pd_state = acpi_pstate_start;
1039 * Make sure that all processors belonging to this
1040 * domain are located.
1043 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
1044 sc->pst_state = acpi_pstate_start;
1047 if (i != dom->pd_nproc) {
1048 KKASSERT(i < dom->pd_nproc);
1050 kprintf("ACPI: domain%u misses processors, "
1051 "should be %d, got %d\n", dom->pd_dom,
1053 if (dom->pd_coord == ACPI_PSD_COORD_SWALL) {
1055 * If this domain's coordination is
1056 * SWALL and we don't see all of the
1057 * member CPUs of this domain, then
1058 * the P-State transition will never
1059 * be completed, so just leave this
1062 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
1069 * Validate P-State configurations for this domain
1071 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
1072 error = acpi_pst_check_csr(sc);
1076 error = acpi_pst_check_pstates(sc);
1081 kprintf("ACPI: domain%u P-State configuration "
1082 "check failed\n", dom->pd_dom);
1083 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
1088 * Do necssary P-State initialization
1090 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
1091 error = acpi_pst_init(sc);
1096 kprintf("ACPI: domain%u P-State initialization "
1097 "check failed\n", dom->pd_dom);
1098 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
1104 ksnprintf(buf, sizeof(buf), "px_dom%u", dom->pd_dom);
1106 sysctl_ctx_init(&dom->pd_sysctl_ctx);
1107 dom->pd_sysctl_tree =
1108 SYSCTL_ADD_NODE(&dom->pd_sysctl_ctx,
1109 SYSCTL_CHILDREN(cpu->glob_sysctl_tree),
1110 OID_AUTO, buf, CTLFLAG_RD, 0,
1112 if (dom->pd_sysctl_tree == NULL) {
1113 kprintf("ACPI: Can't create sysctl tree for domain%u",
1118 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
1119 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
1120 OID_AUTO, "available",
1121 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP,
1122 dom, 0, acpi_pst_sysctl_freqs, "A",
1123 "available frequencies");
1125 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
1126 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
1128 CTLTYPE_OPAQUE | CTLFLAG_RD,
1129 dom, 0, acpi_pst_sysctl_freqs_bin, "IU",
1130 "available frequencies");
1132 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
1133 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
1135 CTLTYPE_OPAQUE | CTLFLAG_RD,
1136 dom, 0, acpi_pst_sysctl_power, "IU",
1137 "power of available frequencies");
1139 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
1140 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
1141 OID_AUTO, "members",
1142 CTLTYPE_STRING | CTLFLAG_RD,
1143 dom, 0, acpi_pst_sysctl_members, "A",
1146 if (acpi_pst_md != NULL &&
1147 acpi_pst_md->pmd_set_pstate != NULL) {
1148 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
1149 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
1151 CTLTYPE_UINT | CTLFLAG_RW,
1152 dom, 0, acpi_pst_sysctl_select,
1153 "IU", "select freq");
1157 if (has_domain && acpi_pst_md != NULL &&
1158 acpi_pst_md->pmd_set_pstate != NULL) {
1159 SYSCTL_ADD_PROC(&cpu->glob_sysctl_ctx,
1160 SYSCTL_CHILDREN(cpu->glob_sysctl_tree),
1161 OID_AUTO, "px_global",
1162 CTLTYPE_UINT | CTLFLAG_RW,
1163 NULL, 0, acpi_pst_sysctl_global,
1164 "IU", "select freq for all domains");
1166 acpi_pst_global_set_pstate(acpi_pstate_start);
1171 acpi_pst_sysctl_freqs(SYSCTL_HANDLER_ARGS)
1176 for (i = 0; i < acpi_npstates; ++i) {
1177 if (error == 0 && i)
1178 error = SYSCTL_OUT(req, " ", 1);
1183 if (i < acpi_pstate_start || i >= acpi_pstate_count)
1188 ksnprintf(buf, sizeof(buf), pat,
1189 acpi_pstates[i].st_freq);
1190 error = SYSCTL_OUT(req, buf, strlen(buf));
1197 acpi_pst_sysctl_freqs_bin(SYSCTL_HANDLER_ARGS)
1199 uint32_t freqs[ACPI_NPSTATE_MAX];
1202 cnt = acpi_pstate_count - acpi_pstate_start;
1203 for (i = 0; i < cnt; ++i)
1204 freqs[i] = acpi_pstates[acpi_pstate_start + i].st_freq;
1206 return sysctl_handle_opaque(oidp, freqs, cnt * sizeof(freqs[0]), req);
1210 acpi_pst_sysctl_power(SYSCTL_HANDLER_ARGS)
1212 uint32_t power[ACPI_NPSTATE_MAX];
1215 cnt = acpi_pstate_count - acpi_pstate_start;
1216 for (i = 0; i < cnt; ++i)
1217 power[i] = acpi_pstates[acpi_pstate_start + i].st_power;
1219 return sysctl_handle_opaque(oidp, power, cnt * sizeof(power[0]), req);
1223 acpi_pst_sysctl_members(SYSCTL_HANDLER_ARGS)
1225 struct acpi_pst_domain *dom = arg1;
1226 struct acpi_pst_softc *sc;
1230 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
1233 if (error == 0 && loop)
1234 error = SYSCTL_OUT(req, " ", 1);
1236 ksnprintf(buf, sizeof(buf), "cpu%d", sc->pst_cpuid);
1237 error = SYSCTL_OUT(req, buf, strlen(buf));
1240 if (error == 0 && acpi_pst_md && acpi_pst_md->pmd_get_pstate) {
1241 const struct acpi_pstate *pstate;
1244 pstate = acpi_pst_get_pstate(sc);
1245 if (pstate == NULL) {
1248 ksnprintf(buf, sizeof(buf), "(%d)",
1252 error = SYSCTL_OUT(req, str, strlen(str));
1260 acpi_pst_sysctl_select(SYSCTL_HANDLER_ARGS)
1262 struct acpi_pst_domain *dom = arg1;
1265 KKASSERT(dom->pd_state >= 0 && dom->pd_state < acpi_npstates);
1267 freq = acpi_pstates[dom->pd_state].st_freq;
1269 error = sysctl_handle_int(oidp, &freq, 0, req);
1270 if (error || req->newptr == NULL)
1273 i = acpi_pst_freq2index(freq);
1277 acpi_pst_domain_set_pstate(dom, i);
1282 acpi_pst_sysctl_global(SYSCTL_HANDLER_ARGS)
1286 KKASSERT(acpi_pst_global_state >= 0 &&
1287 acpi_pst_global_state < acpi_npstates);
1289 freq = acpi_pstates[acpi_pst_global_state].st_freq;
1291 error = sysctl_handle_int(oidp, &freq, 0, req);
1292 if (error || req->newptr == NULL)
1295 i = acpi_pst_freq2index(freq);
1299 acpi_pst_global_set_pstate(i);
1305 acpi_pst_check_csr_handler(netmsg_t msg)
1307 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1310 error = acpi_pst_md->pmd_check_csr(rmsg->ctrl, rmsg->status);
1311 lwkt_replymsg(&rmsg->base.lmsg, error);
1315 acpi_pst_check_csr(struct acpi_pst_softc *sc)
1317 struct netmsg_acpi_pst msg;
1319 if (acpi_pst_md == NULL)
1322 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1323 MSGF_PRIORITY, acpi_pst_check_csr_handler);
1324 msg.ctrl = &sc->pst_creg;
1325 msg.status = &sc->pst_sreg;
1327 return lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.base.lmsg, 0);
1331 acpi_pst_check_pstates_handler(netmsg_t msg)
1335 error = acpi_pst_md->pmd_check_pstates(acpi_pstates, acpi_npstates);
1336 lwkt_replymsg(&msg->lmsg, error);
1340 acpi_pst_check_pstates(struct acpi_pst_softc *sc)
1342 struct netmsg_base msg;
1344 if (acpi_pst_md == NULL)
1347 netmsg_init(&msg, NULL, &curthread->td_msgport,
1348 MSGF_PRIORITY, acpi_pst_check_pstates_handler);
1350 return lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.lmsg, 0);
1354 acpi_pst_init_handler(netmsg_t msg)
1356 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1359 error = acpi_pst_md->pmd_init(rmsg->ctrl, rmsg->status);
1360 lwkt_replymsg(&rmsg->base.lmsg, error);
1364 acpi_pst_init(struct acpi_pst_softc *sc)
1366 struct netmsg_acpi_pst msg;
1368 if (acpi_pst_md == NULL)
1371 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1372 MSGF_PRIORITY, acpi_pst_init_handler);
1373 msg.ctrl = &sc->pst_creg;
1374 msg.status = &sc->pst_sreg;
1376 return lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.base.lmsg, 0);
1380 acpi_pst_set_pstate_handler(netmsg_t msg)
1382 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1385 error = acpi_pst_md->pmd_set_pstate(rmsg->ctrl, rmsg->status,
1386 rmsg->base.lmsg.u.ms_resultp);
1387 lwkt_replymsg(&rmsg->base.lmsg, error);
1391 acpi_pst_set_pstate(struct acpi_pst_softc *sc, const struct acpi_pstate *pstate)
1393 struct netmsg_acpi_pst msg;
1395 KKASSERT(acpi_pst_md != NULL);
1397 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1398 MSGF_PRIORITY, acpi_pst_set_pstate_handler);
1399 msg.base.lmsg.u.ms_resultp = __DECONST(void *, pstate);
1400 msg.ctrl = &sc->pst_creg;
1401 msg.status = &sc->pst_sreg;
1403 return lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.base.lmsg, 0);
1407 acpi_pst_get_pstate_handler(netmsg_t msg)
1409 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1410 const struct acpi_pstate *pstate;
1412 pstate = acpi_pst_md->pmd_get_pstate(rmsg->status, acpi_pstates,
1414 rmsg->base.lmsg.u.ms_resultp = __DECONST(void *, pstate);
1415 lwkt_replymsg(&rmsg->base.lmsg, 0);
1418 static const struct acpi_pstate *
1419 acpi_pst_get_pstate(struct acpi_pst_softc *sc)
1421 struct netmsg_acpi_pst msg;
1423 if (acpi_pst_md == NULL)
1426 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1427 MSGF_PRIORITY, acpi_pst_get_pstate_handler);
1428 msg.status = &sc->pst_sreg;
1430 lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.base.lmsg, 0);
1431 return msg.base.lmsg.u.ms_resultp;
1435 acpi_pst_alloc_resource(device_t dev, ACPI_OBJECT *obj, int idx,
1436 struct acpi_pst_res *res)
1438 struct acpi_pst_softc *sc = device_get_softc(dev);
1442 error = acpi_PkgRawGas(obj, idx, &res->pr_gas);
1446 /* Allocate resource, if possible */
1447 res->pr_rid = sc->pst_parent->cpu_next_rid;
1448 acpi_bus_alloc_gas(dev, &type, &res->pr_rid, &res->pr_gas, &res->pr_res, 0);
1449 if (res->pr_res != NULL) {
1450 sc->pst_parent->cpu_next_rid++;
1451 res->pr_bt = rman_get_bustag(res->pr_res);
1452 res->pr_bh = rman_get_bushandle(res->pr_res);
1460 acpi_pst_domain_check_nproc(device_t dev, struct acpi_pst_domain *dom)
1462 struct acpi_pst_softc *pst;
1466 LIST_FOREACH(pst, &dom->pd_pstlist, pst_link)
1468 if (i == dom->pd_nproc) {
1470 * Some stupid BIOSes will set wrong "# of processors",
1471 * e.g. 1 for CPU w/ hyperthreading; Be lenient here.
1474 device_printf(dev, "domain%u already contains %d "
1475 "P-States\n", dom->pd_dom, dom->pd_nproc);
1479 KKASSERT(i < dom->pd_nproc);