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>
46 #include <net/netisr.h>
47 #include <net/netmsg2.h>
48 #include <net/if_var.h>
53 #include "acpi_cpu_pstate.h"
55 #define ACPI_NPSTATE_MAX 16
57 #define ACPI_PSS_PX_NENTRY 6
59 #define ACPI_PSD_COORD_SWALL 0xfc
60 #define ACPI_PSD_COORD_SWANY 0xfd
61 #define ACPI_PSD_COORD_HWALL 0xfe
62 #define ACPI_PSD_COORD_VALID(coord) \
63 ((coord) == ACPI_PSD_COORD_SWALL || \
64 (coord) == ACPI_PSD_COORD_SWANY || \
65 (coord) == ACPI_PSD_COORD_HWALL)
67 struct acpi_pst_softc;
68 LIST_HEAD(acpi_pst_list, acpi_pst_softc);
70 struct netmsg_acpi_pst {
71 struct netmsg_base base;
72 const struct acpi_pst_res *ctrl;
73 const struct acpi_pst_res *status;
76 struct acpi_pst_domain {
80 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_cpux_softc *pst_parent;
100 struct acpi_pst_domain *pst_domain;
101 struct acpi_pst_res pst_creg;
102 struct acpi_pst_res pst_sreg;
108 ACPI_HANDLE pst_handle;
110 LIST_ENTRY(acpi_pst_softc) pst_link;
113 static int acpi_pst_probe(device_t dev);
114 static int acpi_pst_attach(device_t dev);
116 static void acpi_pst_postattach(void *);
117 static struct acpi_pst_domain *
118 acpi_pst_domain_create_int(device_t, uint32_t);
119 static struct acpi_pst_domain *
120 acpi_pst_domain_create_pkg(device_t, ACPI_OBJECT *);
121 static struct acpi_pst_domain *
122 acpi_pst_domain_find(uint32_t);
123 static struct acpi_pst_domain *
124 acpi_pst_domain_alloc(uint32_t, uint32_t, uint32_t);
125 static int acpi_pst_domain_set_pstate(struct acpi_pst_domain *, int);
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_members(SYSCTL_HANDLER_ARGS);
146 static int acpi_pst_sysctl_select(SYSCTL_HANDLER_ARGS);
147 static int acpi_pst_sysctl_global(SYSCTL_HANDLER_ARGS);
149 static struct acpi_pst_domlist acpi_pst_domains =
150 LIST_HEAD_INITIALIZER(acpi_pst_domains);
151 static int acpi_pst_domain_id;
153 static int acpi_pst_global_state;
155 static int acpi_npstates;
156 static struct acpi_pstate *acpi_pstates;
158 static const struct acpi_pst_md *acpi_pst_md;
160 static device_method_t acpi_pst_methods[] = {
161 /* Device interface */
162 DEVMETHOD(device_probe, acpi_pst_probe),
163 DEVMETHOD(device_attach, acpi_pst_attach),
164 DEVMETHOD(device_detach, bus_generic_detach),
165 DEVMETHOD(device_shutdown, bus_generic_shutdown),
166 DEVMETHOD(device_suspend, bus_generic_suspend),
167 DEVMETHOD(device_resume, bus_generic_resume),
170 DEVMETHOD(bus_add_child, bus_generic_add_child),
171 DEVMETHOD(bus_print_child, bus_generic_print_child),
172 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar),
173 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar),
174 DEVMETHOD(bus_get_resource_list, bus_generic_get_resource_list),
175 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
176 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
177 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
178 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
179 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
180 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
181 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
182 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
183 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
188 static driver_t acpi_pst_driver = {
191 sizeof(struct acpi_pst_softc)
194 static devclass_t acpi_pst_devclass;
195 DRIVER_MODULE(cpu_pst, cpu, acpi_pst_driver, acpi_pst_devclass, 0, 0);
196 MODULE_DEPEND(cpu_pst, acpi, 1, 1, 1);
199 acpi_pst_freq2index(int freq)
203 for (i = 0; i < acpi_npstates; ++i) {
204 if (acpi_pstates[i].st_freq == freq)
211 acpi_pst_probe(device_t dev)
218 if (acpi_disabled("cpu_pst") ||
219 acpi_get_type(dev) != ACPI_TYPE_PROCESSOR)
222 if (acpi_pst_md == NULL)
223 acpi_pst_md = acpi_pst_md_probe();
225 handle = acpi_get_handle(dev);
231 buf.Length = ACPI_ALLOCATE_BUFFER;
232 status = AcpiEvaluateObject(handle, "_PCT", NULL, &buf);
233 if (ACPI_FAILURE(status)) {
235 device_printf(dev, "Can't get _PCT package - %s\n",
236 AcpiFormatException(status));
241 obj = (ACPI_OBJECT *)buf.Pointer;
242 if (!ACPI_PKG_VALID_EQ(obj, 2)) {
243 device_printf(dev, "Invalid _PCT package\n");
253 buf.Length = ACPI_ALLOCATE_BUFFER;
254 status = AcpiEvaluateObject(handle, "_PSS", NULL, &buf);
255 if (ACPI_FAILURE(status)) {
256 device_printf(dev, "Can't get _PSS package - %s\n",
257 AcpiFormatException(status));
261 obj = (ACPI_OBJECT *)buf.Pointer;
262 if (!ACPI_PKG_VALID(obj, 1)) {
263 device_printf(dev, "Invalid _PSS package\n");
269 device_set_desc(dev, "ACPI CPU P-State");
274 acpi_pst_attach(device_t dev)
276 struct acpi_pst_softc *sc = device_get_softc(dev), *pst;
277 struct acpi_pst_domain *dom = NULL;
281 struct acpi_pstate *pstate, *p;
282 int i, npstate, error;
285 sc->pst_parent = device_get_softc(device_get_parent(dev));
286 sc->pst_handle = acpi_get_handle(dev);
287 sc->pst_cpuid = acpi_get_magic(dev);
290 * If there is a _PSD, then we create procossor domain
291 * accordingly. If there is no _PSD, we just fake a
292 * default processor domain0.
295 buf.Length = ACPI_ALLOCATE_BUFFER;
296 status = AcpiEvaluateObject(sc->pst_handle, "_PSD", NULL, &buf);
297 if (!ACPI_FAILURE(status)) {
298 obj = (ACPI_OBJECT *)buf.Pointer;
300 if (acpi_pst_domain_id > 0) {
301 device_printf(dev, "Missing _PSD for certain CPUs\n");
305 acpi_pst_domain_id = -1;
307 if (ACPI_PKG_VALID_EQ(obj, 1)) {
308 dom = acpi_pst_domain_create_pkg(dev,
309 &obj->Package.Elements[0]);
315 if (obj->Type != ACPI_TYPE_INTEGER) {
317 "Invalid _PSD package, Type 0x%x\n",
322 device_printf(dev, "Integer _PSD %ju\n",
323 (uintmax_t)obj->Integer.Value);
324 dom = acpi_pst_domain_create_int(dev,
334 AcpiOsFree(buf.Pointer);
336 if (acpi_pst_domain_id < 0) {
337 device_printf(dev, "Missing _PSD for cpu%d\n",
343 * Create a stub one processor domain for each processor
345 dom = acpi_pst_domain_alloc(acpi_pst_domain_id,
346 ACPI_PSD_COORD_SWANY, 1);
347 dom->pd_flags |= ACPI_PSTDOM_FLAG_STUB;
349 ++acpi_pst_domain_id;
352 /* Make sure that adding us will not overflow our domain */
354 LIST_FOREACH(pst, &dom->pd_pstlist, pst_link)
356 if (i == dom->pd_nproc) {
357 device_printf(dev, "Domain%u already contains %d P-States, "
358 "invalid _PSD package\n",
359 dom->pd_dom, dom->pd_nproc);
362 KKASSERT(i < dom->pd_nproc);
365 * Get control/status registers from _PCT
368 buf.Length = ACPI_ALLOCATE_BUFFER;
369 status = AcpiEvaluateObject(sc->pst_handle, "_PCT", NULL, &buf);
370 if (ACPI_FAILURE(status)) {
371 device_printf(dev, "Can't get _PCT package - %s\n",
372 AcpiFormatException(status));
376 obj = (ACPI_OBJECT *)buf.Pointer;
377 if (!ACPI_PKG_VALID_EQ(obj, 2)) {
378 device_printf(dev, "Invalid _PCT package\n");
383 /* Save and try allocating control register */
384 error = acpi_pst_alloc_resource(dev, obj, 0, &sc->pst_creg);
390 device_printf(dev, "control reg %d %jx\n",
391 sc->pst_creg.pr_gas.SpaceId,
392 (uintmax_t)sc->pst_creg.pr_gas.Address);
395 /* Save and try allocating status register */
396 error = acpi_pst_alloc_resource(dev, obj, 1, &sc->pst_sreg);
402 device_printf(dev, "status reg %d %jx\n",
403 sc->pst_sreg.pr_gas.SpaceId,
404 (uintmax_t)sc->pst_sreg.pr_gas.Address);
411 * Create P-State table according to _PSS
414 buf.Length = ACPI_ALLOCATE_BUFFER;
415 status = AcpiEvaluateObject(sc->pst_handle, "_PSS", NULL, &buf);
416 if (ACPI_FAILURE(status)) {
417 device_printf(dev, "Can't get _PSS package - %s\n",
418 AcpiFormatException(status));
422 obj = (ACPI_OBJECT *)buf.Pointer;
423 if (!ACPI_PKG_VALID(obj, 1)) {
424 device_printf(dev, "Invalid _PSS package\n");
429 /* Don't create too many P-States */
430 npstate = obj->Package.Count;
431 if (npstate > ACPI_NPSTATE_MAX) {
432 device_printf(dev, "Too many P-States, %d->%d\n",
433 npstate, ACPI_NPSTATE_MAX);
434 npstate = ACPI_NPSTATE_MAX;
438 * If we have already created P-State table,
439 * we must make sure that number of entries
442 if (acpi_pstates != NULL && acpi_npstates != npstate) {
443 device_printf(dev, "Inconsistent # of P-States "
444 "cross Processor objects\n");
450 * Create a temporary P-State table
452 pstate = kmalloc(sizeof(*pstate) * npstate, M_TEMP, M_WAITOK);
453 for (i = 0, p = pstate; i < npstate; ++i, ++p) {
455 uint32_t *ptr[ACPI_PSS_PX_NENTRY] = {
456 &p->st_freq, &p->st_power, &p->st_xsit_lat,
457 &p->st_bm_lat, &p->st_cval, &p->st_sval
461 pkg = &obj->Package.Elements[i];
462 if (!ACPI_PKG_VALID(pkg, ACPI_PSS_PX_NENTRY)) {
463 device_printf(dev, "Invalud _PSS P%d\n", i);
465 kfree(pstate, M_TEMP);
468 for (j = 0; j < ACPI_PSS_PX_NENTRY; ++j) {
469 if (acpi_PkgInt32(pkg, j, ptr[j]) != 0) {
470 device_printf(dev, "Can't extract "
471 "_PSS P%d %dth entry\n", i, j);
473 kfree(pstate, M_TEMP);
482 if (acpi_pstates == NULL) {
484 * If no P-State table is created yet,
485 * save the temporary one we just created.
487 acpi_pstates = pstate;
488 acpi_npstates = npstate;
492 for (i = 0; i < acpi_npstates; ++i) {
494 "freq %u, pwr %u, xlat %u, blat %u, "
495 "cv %08x, sv %08x\n",
496 acpi_pstates[i].st_freq,
497 acpi_pstates[i].st_power,
498 acpi_pstates[i].st_xsit_lat,
499 acpi_pstates[i].st_bm_lat,
500 acpi_pstates[i].st_cval,
501 acpi_pstates[i].st_sval);
506 * Make sure that P-State tables are same
507 * for all processors.
509 if (memcmp(pstate, acpi_pstates,
510 sizeof(*pstate) * npstate) != 0) {
511 device_printf(dev, "Inconsistent _PSS "
512 "cross Processor objects\n");
513 kfree(pstate, M_TEMP);
516 kfree(pstate, M_TEMP);
519 /* By default, we start from P-State table's first entry */
523 * Adjust the usable first entry of P-State table,
524 * if there is _PPC object.
527 buf.Length = ACPI_ALLOCATE_BUFFER;
528 status = AcpiEvaluateObject(sc->pst_handle, "_PPC", NULL, &buf);
529 if (!ACPI_FAILURE(status)) {
530 obj = (ACPI_OBJECT *)buf.Pointer;
531 if (obj->Type == ACPI_TYPE_INTEGER) {
532 if (obj->Integer.Value >= acpi_npstates) {
533 device_printf(dev, "Invalid _PPC value\n");
537 sc->pst_sstart = obj->Integer.Value;
539 device_printf(dev, "_PPC %d\n", sc->pst_sstart);
541 /* TODO: Install notifiy handler */
543 device_printf(dev, "Invalid _PPC object\n");
552 sc->pst_state = sc->pst_sstart;
554 /* Link us with the domain */
555 sc->pst_domain = dom;
556 LIST_INSERT_HEAD(&dom->pd_pstlist, sc, pst_link);
558 if (device_get_unit(dev) == 0)
559 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_pst_postattach, NULL);
564 static struct acpi_pst_domain *
565 acpi_pst_domain_create_pkg(device_t dev, ACPI_OBJECT *obj)
567 struct acpi_pst_domain *dom;
568 uint32_t val, domain, coord, nproc;
570 if (!ACPI_PKG_VALID_EQ(obj, 5)) {
571 device_printf(dev, "Invalid _PSD package\n");
575 /* NumberOfEntries */
576 if (acpi_PkgInt32(obj, 0, &val) != 0 || val != 5) {
577 device_printf(dev, "Invalid _PSD NumberOfEntries\n");
582 if (acpi_PkgInt32(obj, 1, &val) != 0 || val != 0) {
583 device_printf(dev, "Invalid _PSD Revision\n");
587 if (acpi_PkgInt32(obj, 2, &domain) != 0 ||
588 acpi_PkgInt32(obj, 3, &coord) != 0 ||
589 acpi_PkgInt32(obj, 4, &nproc) != 0) {
590 device_printf(dev, "Can't extract _PSD package\n");
594 if (!ACPI_PSD_COORD_VALID(coord)) {
595 device_printf(dev, "Invalid _PSD CoordType (%#x)\n", coord);
599 if (nproc > MAXCPU) {
601 * If NumProcessors is greater than MAXCPU
602 * and domain's coordination is SWALL, then
603 * we will never be able to start all CPUs
604 * within this domain, and power state
605 * transition will never be completed, so we
606 * just bail out here.
608 if (coord == ACPI_PSD_COORD_SWALL) {
609 device_printf(dev, "Unsupported _PSD NumProcessors "
613 } else if (nproc == 0) {
614 device_printf(dev, "_PSD NumProcessors are zero\n");
618 dom = acpi_pst_domain_find(domain);
620 if (dom->pd_flags & ACPI_PSTDOM_FLAG_INT) {
621 device_printf(dev, "Mixed Integer _PSD and "
625 if (dom->pd_coord != coord || dom->pd_nproc != nproc) {
626 device_printf(dev, "Inconsistent _PSD information "
627 "cross Processor objects\n");
633 dom = acpi_pst_domain_alloc(domain, coord, nproc);
635 device_printf(dev, "create pkg domain%u\n", dom->pd_dom);
640 static struct acpi_pst_domain *
641 acpi_pst_domain_create_int(device_t dev, uint32_t domain)
643 struct acpi_pst_domain *dom;
645 dom = acpi_pst_domain_find(domain);
647 if ((dom->pd_flags & ACPI_PSTDOM_FLAG_INT) == 0) {
648 device_printf(dev, "Mixed Package _PSD and "
652 KKASSERT(dom->pd_coord == ACPI_PSD_COORD_SWALL);
658 dom = acpi_pst_domain_alloc(domain, ACPI_PSD_COORD_SWALL, 1);
659 dom->pd_flags |= ACPI_PSTDOM_FLAG_INT;
662 device_printf(dev, "create int domain%u\n", dom->pd_dom);
667 static struct acpi_pst_domain *
668 acpi_pst_domain_find(uint32_t domain)
670 struct acpi_pst_domain *dom;
672 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
673 if (dom->pd_flags & ACPI_PSTDOM_FLAG_STUB)
675 if (dom->pd_dom == domain)
681 static struct acpi_pst_domain *
682 acpi_pst_domain_alloc(uint32_t domain, uint32_t coord, uint32_t nproc)
684 struct acpi_pst_domain *dom;
686 dom = kmalloc(sizeof(*dom), M_DEVBUF, M_WAITOK | M_ZERO);
687 dom->pd_dom = domain;
688 dom->pd_coord = coord;
689 dom->pd_nproc = nproc;
690 dom->pd_state = 0; /* XXX */
691 dom->pd_sstart = 0; /* XXX */
692 LIST_INIT(&dom->pd_pstlist);
694 LIST_INSERT_HEAD(&acpi_pst_domains, dom, pd_link);
700 acpi_pst_domain_set_pstate(struct acpi_pst_domain *dom, int i)
702 const struct acpi_pstate *pstate;
703 struct acpi_pst_softc *sc;
706 KKASSERT(i >= 0 && i < acpi_npstates);
707 pstate = &acpi_pstates[i];
710 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
712 error = acpi_pst_set_pstate(sc, pstate);
714 device_printf(sc->pst_dev, "can't set "
715 "freq %d\n", pstate->st_freq);
716 /* XXX error cleanup? */
718 if (dom->pd_coord == ACPI_PSD_COORD_SWANY)
729 acpi_pst_global_set_pstate(int i)
731 struct acpi_pst_domain *dom;
733 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
734 /* Skip dead domain */
735 if (dom->pd_flags & ACPI_PSTDOM_FLAG_DEAD)
737 acpi_pst_domain_set_pstate(dom, i);
739 acpi_pst_global_state = i;
745 acpi_pst_postattach(void *arg __unused)
747 struct acpi_pst_domain *dom;
748 struct acpi_cpux_softc *cpux;
750 int i, ndevices, error, has_domain, sstate;
754 error = devclass_get_devices(acpi_pst_devclass, &devices, &ndevices);
762 for (i = 0; i < ndevices; ++i) {
763 cpux = device_get_softc(device_get_parent(devices[i]));
764 if (cpux->glob_sysctl_tree != NULL)
767 kfree(devices, M_TEMP);
768 KKASSERT(cpux != NULL);
770 if (acpi_pst_md == NULL)
771 kprintf("ACPI: no P-State CPU driver\n");
775 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
776 struct acpi_pst_softc *sc;
780 * Make sure that all processors belonging to this
781 * domain are located.
784 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link)
786 if (i != dom->pd_nproc) {
787 KKASSERT(i < dom->pd_nproc);
789 kprintf("ACPI: domain%u misses processors, "
790 "should be %d, got %d\n", dom->pd_dom,
792 if (dom->pd_coord == ACPI_PSD_COORD_SWALL) {
794 * If this domain's coordination is
795 * SWALL and we don't see all of the
796 * member CPUs of this domain, then
797 * the P-State transition will never
798 * be completed, so just leave this
801 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
808 * Validate P-State configurations for this domain
810 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
811 error = acpi_pst_check_csr(sc);
815 error = acpi_pst_check_pstates(sc);
820 kprintf("ACPI: domain%u P-State configuration "
821 "check failed\n", dom->pd_dom);
822 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
827 * Do necssary P-State initialization
829 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
830 error = acpi_pst_init(sc);
835 kprintf("ACPI: domain%u P-State initialization "
836 "check failed\n", dom->pd_dom);
837 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
843 ksnprintf(buf, sizeof(buf), "px_dom%u", dom->pd_dom);
845 sysctl_ctx_init(&dom->pd_sysctl_ctx);
846 dom->pd_sysctl_tree =
847 SYSCTL_ADD_NODE(&dom->pd_sysctl_ctx,
848 SYSCTL_CHILDREN(cpux->glob_sysctl_tree),
849 OID_AUTO, buf, CTLFLAG_RD, 0,
851 if (dom->pd_sysctl_tree == NULL) {
852 kprintf("ACPI: Can't create sysctl tree for domain%u",
857 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
858 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
859 OID_AUTO, "available",
860 CTLTYPE_STRING | CTLFLAG_RD,
861 dom, 0, acpi_pst_sysctl_freqs, "A",
862 "available frequencies");
864 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
865 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
867 CTLTYPE_STRING | CTLFLAG_RD,
868 dom, 0, acpi_pst_sysctl_members, "A",
871 if (acpi_pst_md != NULL &&
872 acpi_pst_md->pmd_set_pstate != NULL) {
873 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
874 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
876 CTLTYPE_UINT | CTLFLAG_RW,
877 dom, 0, acpi_pst_sysctl_select,
878 "IU", "select freq");
881 if (dom->pd_state < sstate)
882 sstate = dom->pd_state;
885 if (has_domain && acpi_pst_md != NULL &&
886 acpi_pst_md->pmd_set_pstate != NULL) {
887 SYSCTL_ADD_PROC(&cpux->glob_sysctl_ctx,
888 SYSCTL_CHILDREN(cpux->glob_sysctl_tree),
889 OID_AUTO, "px_global",
890 CTLTYPE_UINT | CTLFLAG_RW,
891 NULL, 0, acpi_pst_sysctl_global,
892 "IU", "select freq for all domains");
894 acpi_pst_global_set_pstate(sstate);
899 acpi_pst_sysctl_freqs(SYSCTL_HANDLER_ARGS)
901 struct acpi_pst_domain *dom = arg1;
905 for (i = 0; i < acpi_npstates; ++i) {
907 error = SYSCTL_OUT(req, " ", 1);
912 if (i < dom->pd_sstart)
917 ksnprintf(buf, sizeof(buf), pat,
918 acpi_pstates[i].st_freq);
919 error = SYSCTL_OUT(req, buf, strlen(buf));
926 acpi_pst_sysctl_members(SYSCTL_HANDLER_ARGS)
928 struct acpi_pst_domain *dom = arg1;
929 struct acpi_pst_softc *sc;
933 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
936 if (error == 0 && loop)
937 error = SYSCTL_OUT(req, " ", 1);
939 ksnprintf(buf, sizeof(buf), "cpu%d", sc->pst_cpuid);
940 error = SYSCTL_OUT(req, buf, strlen(buf));
943 if (error == 0 && acpi_pst_md && acpi_pst_md->pmd_get_pstate) {
944 const struct acpi_pstate *pstate;
947 pstate = acpi_pst_get_pstate(sc);
948 if (pstate == NULL) {
951 ksnprintf(buf, sizeof(buf), "(%d)",
955 error = SYSCTL_OUT(req, str, strlen(str));
963 acpi_pst_sysctl_select(SYSCTL_HANDLER_ARGS)
965 struct acpi_pst_domain *dom = arg1;
968 KKASSERT(dom->pd_state >= 0 && dom->pd_state < acpi_npstates);
970 freq = acpi_pstates[dom->pd_state].st_freq;
972 error = sysctl_handle_int(oidp, &freq, 0, req);
973 if (error || req->newptr == NULL)
976 i = acpi_pst_freq2index(freq);
980 acpi_pst_domain_set_pstate(dom, i);
985 acpi_pst_sysctl_global(SYSCTL_HANDLER_ARGS)
989 KKASSERT(acpi_pst_global_state >= 0 &&
990 acpi_pst_global_state < acpi_npstates);
992 freq = acpi_pstates[acpi_pst_global_state].st_freq;
994 error = sysctl_handle_int(oidp, &freq, 0, req);
995 if (error || req->newptr == NULL)
998 i = acpi_pst_freq2index(freq);
1002 acpi_pst_global_set_pstate(i);
1008 acpi_pst_check_csr_handler(netmsg_t msg)
1010 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1013 error = acpi_pst_md->pmd_check_csr(rmsg->ctrl, rmsg->status);
1014 lwkt_replymsg(&rmsg->base.lmsg, error);
1018 acpi_pst_check_csr(struct acpi_pst_softc *sc)
1020 struct netmsg_acpi_pst msg;
1022 if (acpi_pst_md == NULL)
1025 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1026 MSGF_PRIORITY, acpi_pst_check_csr_handler);
1027 msg.ctrl = &sc->pst_creg;
1028 msg.status = &sc->pst_sreg;
1030 return lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.base.lmsg, 0);
1034 acpi_pst_check_pstates_handler(netmsg_t msg)
1038 error = acpi_pst_md->pmd_check_pstates(acpi_pstates, acpi_npstates);
1039 lwkt_replymsg(&msg->lmsg, error);
1043 acpi_pst_check_pstates(struct acpi_pst_softc *sc)
1045 struct netmsg_base msg;
1047 if (acpi_pst_md == NULL)
1050 netmsg_init(&msg, NULL, &curthread->td_msgport,
1051 MSGF_PRIORITY, acpi_pst_check_pstates_handler);
1053 return lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.lmsg, 0);
1057 acpi_pst_init_handler(netmsg_t msg)
1059 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1062 error = acpi_pst_md->pmd_init(rmsg->ctrl, rmsg->status);
1063 lwkt_replymsg(&rmsg->base.lmsg, error);
1067 acpi_pst_init(struct acpi_pst_softc *sc)
1069 struct netmsg_acpi_pst msg;
1071 if (acpi_pst_md == NULL)
1074 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1075 MSGF_PRIORITY, acpi_pst_init_handler);
1076 msg.ctrl = &sc->pst_creg;
1077 msg.status = &sc->pst_sreg;
1079 return lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.base.lmsg, 0);
1083 acpi_pst_set_pstate_handler(netmsg_t msg)
1085 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1088 error = acpi_pst_md->pmd_set_pstate(rmsg->ctrl, rmsg->status,
1089 rmsg->base.lmsg.u.ms_resultp);
1090 lwkt_replymsg(&rmsg->base.lmsg, error);
1094 acpi_pst_set_pstate(struct acpi_pst_softc *sc, const struct acpi_pstate *pstate)
1096 struct netmsg_acpi_pst msg;
1098 KKASSERT(acpi_pst_md != NULL);
1100 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1101 MSGF_PRIORITY, acpi_pst_set_pstate_handler);
1102 msg.base.lmsg.u.ms_resultp = __DECONST(void *, pstate);
1103 msg.ctrl = &sc->pst_creg;
1104 msg.status = &sc->pst_sreg;
1106 return lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.base.lmsg, 0);
1110 acpi_pst_get_pstate_handler(netmsg_t msg)
1112 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1113 const struct acpi_pstate *pstate;
1115 pstate = acpi_pst_md->pmd_get_pstate(rmsg->status, acpi_pstates,
1117 rmsg->base.lmsg.u.ms_resultp = __DECONST(void *, pstate);
1118 lwkt_replymsg(&rmsg->base.lmsg, 0);
1121 static const struct acpi_pstate *
1122 acpi_pst_get_pstate(struct acpi_pst_softc *sc)
1124 struct netmsg_acpi_pst msg;
1126 if (acpi_pst_md == NULL)
1129 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1130 MSGF_PRIORITY, acpi_pst_get_pstate_handler);
1131 msg.status = &sc->pst_sreg;
1133 lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.base.lmsg, 0);
1134 return msg.base.lmsg.u.ms_resultp;
1138 acpi_pst_alloc_resource(device_t dev, ACPI_OBJECT *obj, int idx,
1139 struct acpi_pst_res *res)
1141 struct acpi_pst_softc *sc = device_get_softc(dev);
1145 error = acpi_PkgRawGas(obj, idx, &res->pr_gas);
1149 /* Allocate resource, if possible */
1150 res->pr_rid = sc->pst_parent->cpux_next_rid;
1151 acpi_bus_alloc_gas(dev, &type, &res->pr_rid, &res->pr_gas, &res->pr_res, 0);
1152 if (res->pr_res != NULL) {
1153 sc->pst_parent->cpux_next_rid++;
1154 res->pr_bt = rman_get_bustag(res->pr_res);
1155 res->pr_bh = rman_get_bushandle(res->pr_res);