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;
299 if (ACPI_PKG_VALID_EQ(obj, 1)) {
300 dom = acpi_pst_domain_create_pkg(dev,
301 &obj->Package.Elements[0]);
307 if (obj->Type != ACPI_TYPE_INTEGER) {
309 "Invalid _PSD package, Type 0x%x\n",
314 device_printf(dev, "Integer _PSD %ju\n",
315 (uintmax_t)obj->Integer.Value);
316 dom = acpi_pst_domain_create_int(dev,
326 AcpiOsFree(buf.Pointer);
329 * Create a stub one processor domain for each processor
331 dom = acpi_pst_domain_alloc(acpi_pst_domain_id,
332 ACPI_PSD_COORD_SWANY, 1);
333 dom->pd_flags |= ACPI_PSTDOM_FLAG_STUB;
335 ++acpi_pst_domain_id;
338 /* Make sure that adding us will not overflow our domain */
340 LIST_FOREACH(pst, &dom->pd_pstlist, pst_link)
342 if (i == dom->pd_nproc) {
343 device_printf(dev, "Domain%u already contains %d P-States, "
344 "invalid _PSD package\n",
345 dom->pd_dom, dom->pd_nproc);
348 KKASSERT(i < dom->pd_nproc);
351 * Get control/status registers from _PCT
354 buf.Length = ACPI_ALLOCATE_BUFFER;
355 status = AcpiEvaluateObject(sc->pst_handle, "_PCT", NULL, &buf);
356 if (ACPI_FAILURE(status)) {
357 device_printf(dev, "Can't get _PCT package - %s\n",
358 AcpiFormatException(status));
362 obj = (ACPI_OBJECT *)buf.Pointer;
363 if (!ACPI_PKG_VALID_EQ(obj, 2)) {
364 device_printf(dev, "Invalid _PCT package\n");
369 /* Save and try allocating control register */
370 error = acpi_pst_alloc_resource(dev, obj, 0, &sc->pst_creg);
376 device_printf(dev, "control reg %d %jx\n",
377 sc->pst_creg.pr_gas.SpaceId,
378 (uintmax_t)sc->pst_creg.pr_gas.Address);
381 /* Save and try allocating status register */
382 error = acpi_pst_alloc_resource(dev, obj, 1, &sc->pst_sreg);
388 device_printf(dev, "status reg %d %jx\n",
389 sc->pst_sreg.pr_gas.SpaceId,
390 (uintmax_t)sc->pst_sreg.pr_gas.Address);
397 * Create P-State table according to _PSS
400 buf.Length = ACPI_ALLOCATE_BUFFER;
401 status = AcpiEvaluateObject(sc->pst_handle, "_PSS", NULL, &buf);
402 if (ACPI_FAILURE(status)) {
403 device_printf(dev, "Can't get _PSS package - %s\n",
404 AcpiFormatException(status));
408 obj = (ACPI_OBJECT *)buf.Pointer;
409 if (!ACPI_PKG_VALID(obj, 1)) {
410 device_printf(dev, "Invalid _PSS package\n");
415 /* Don't create too many P-States */
416 npstate = obj->Package.Count;
417 if (npstate > ACPI_NPSTATE_MAX) {
418 device_printf(dev, "Too many P-States, %d->%d\n",
419 npstate, ACPI_NPSTATE_MAX);
420 npstate = ACPI_NPSTATE_MAX;
424 * If we have already created P-State table,
425 * we must make sure that number of entries
428 if (acpi_pstates != NULL && acpi_npstates != npstate) {
429 device_printf(dev, "Inconsistent # of P-States "
430 "cross Processor objects\n");
436 * Create a temporary P-State table
438 pstate = kmalloc(sizeof(*pstate) * npstate, M_TEMP, M_WAITOK);
439 for (i = 0, p = pstate; i < npstate; ++i, ++p) {
441 uint32_t *ptr[ACPI_PSS_PX_NENTRY] = {
442 &p->st_freq, &p->st_power, &p->st_xsit_lat,
443 &p->st_bm_lat, &p->st_cval, &p->st_sval
447 pkg = &obj->Package.Elements[i];
448 if (!ACPI_PKG_VALID(pkg, ACPI_PSS_PX_NENTRY)) {
449 device_printf(dev, "Invalud _PSS P%d\n", i);
451 kfree(pstate, M_TEMP);
454 for (j = 0; j < ACPI_PSS_PX_NENTRY; ++j) {
455 if (acpi_PkgInt32(pkg, j, ptr[j]) != 0) {
456 device_printf(dev, "Can't extract "
457 "_PSS P%d %dth entry\n", i, j);
459 kfree(pstate, M_TEMP);
468 if (acpi_pstates == NULL) {
470 * If no P-State table is created yet,
471 * save the temporary one we just created.
473 acpi_pstates = pstate;
474 acpi_npstates = npstate;
478 for (i = 0; i < acpi_npstates; ++i) {
480 "freq %u, pwr %u, xlat %u, blat %u, "
481 "cv %08x, sv %08x\n",
482 acpi_pstates[i].st_freq,
483 acpi_pstates[i].st_power,
484 acpi_pstates[i].st_xsit_lat,
485 acpi_pstates[i].st_bm_lat,
486 acpi_pstates[i].st_cval,
487 acpi_pstates[i].st_sval);
492 * Make sure that P-State tables are same
493 * for all processors.
495 if (memcmp(pstate, acpi_pstates,
496 sizeof(*pstate) * npstate) != 0) {
497 device_printf(dev, "Inconsistent _PSS "
498 "cross Processor objects\n");
499 kfree(pstate, M_TEMP);
502 kfree(pstate, M_TEMP);
505 /* By default, we start from P-State table's first entry */
509 * Adjust the usable first entry of P-State table,
510 * if there is _PPC object.
513 buf.Length = ACPI_ALLOCATE_BUFFER;
514 status = AcpiEvaluateObject(sc->pst_handle, "_PPC", NULL, &buf);
515 if (!ACPI_FAILURE(status)) {
516 obj = (ACPI_OBJECT *)buf.Pointer;
517 if (obj->Type == ACPI_TYPE_INTEGER) {
518 if (obj->Integer.Value >= acpi_npstates) {
519 device_printf(dev, "Invalid _PPC value\n");
523 sc->pst_sstart = obj->Integer.Value;
525 device_printf(dev, "_PPC %d\n", sc->pst_sstart);
527 /* TODO: Install notifiy handler */
529 device_printf(dev, "Invalid _PPC object\n");
538 sc->pst_state = sc->pst_sstart;
540 /* Link us with the domain */
541 sc->pst_domain = dom;
542 LIST_INSERT_HEAD(&dom->pd_pstlist, sc, pst_link);
544 if (device_get_unit(dev) == 0)
545 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_pst_postattach, NULL);
550 static struct acpi_pst_domain *
551 acpi_pst_domain_create_pkg(device_t dev, ACPI_OBJECT *obj)
553 struct acpi_pst_domain *dom;
554 uint32_t val, domain, coord, nproc;
556 if (!ACPI_PKG_VALID_EQ(obj, 5)) {
557 device_printf(dev, "Invalid _PSD package\n");
561 /* NumberOfEntries */
562 if (acpi_PkgInt32(obj, 0, &val) != 0 || val != 5) {
563 device_printf(dev, "Invalid _PSD NumberOfEntries\n");
568 if (acpi_PkgInt32(obj, 1, &val) != 0 || val != 0) {
569 device_printf(dev, "Invalid _PSD Revision\n");
573 if (acpi_PkgInt32(obj, 2, &domain) != 0 ||
574 acpi_PkgInt32(obj, 3, &coord) != 0 ||
575 acpi_PkgInt32(obj, 4, &nproc) != 0) {
576 device_printf(dev, "Can't extract _PSD package\n");
580 if (!ACPI_PSD_COORD_VALID(coord)) {
581 device_printf(dev, "Invalid _PSD CoordType (%#x)\n", coord);
585 if (nproc > MAXCPU) {
587 * If NumProcessors is greater than MAXCPU
588 * and domain's coordination is SWALL, then
589 * we will never be able to start all CPUs
590 * within this domain, and power state
591 * transition will never be completed, so we
592 * just bail out here.
594 if (coord == ACPI_PSD_COORD_SWALL) {
595 device_printf(dev, "Unsupported _PSD NumProcessors "
599 } else if (nproc == 0) {
600 device_printf(dev, "_PSD NumProcessors are zero\n");
604 dom = acpi_pst_domain_find(domain);
606 if (dom->pd_flags & ACPI_PSTDOM_FLAG_INT) {
607 device_printf(dev, "Mixed Integer _PSD and "
611 if (dom->pd_coord != coord || dom->pd_nproc != nproc) {
612 device_printf(dev, "Inconsistent _PSD information "
613 "cross Processor objects\n");
619 dom = acpi_pst_domain_alloc(domain, coord, nproc);
621 device_printf(dev, "create pkg domain%u\n", dom->pd_dom);
626 static struct acpi_pst_domain *
627 acpi_pst_domain_create_int(device_t dev, uint32_t domain)
629 struct acpi_pst_domain *dom;
631 dom = acpi_pst_domain_find(domain);
633 if ((dom->pd_flags & ACPI_PSTDOM_FLAG_INT) == 0) {
634 device_printf(dev, "Mixed Package _PSD and "
638 KKASSERT(dom->pd_coord == ACPI_PSD_COORD_SWALL);
644 dom = acpi_pst_domain_alloc(domain, ACPI_PSD_COORD_SWALL, 1);
645 dom->pd_flags |= ACPI_PSTDOM_FLAG_INT;
648 device_printf(dev, "create int domain%u\n", dom->pd_dom);
653 static struct acpi_pst_domain *
654 acpi_pst_domain_find(uint32_t domain)
656 struct acpi_pst_domain *dom;
658 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
659 if (dom->pd_flags & ACPI_PSTDOM_FLAG_STUB)
661 if (dom->pd_dom == domain)
667 static struct acpi_pst_domain *
668 acpi_pst_domain_alloc(uint32_t domain, uint32_t coord, uint32_t nproc)
670 struct acpi_pst_domain *dom;
672 dom = kmalloc(sizeof(*dom), M_DEVBUF, M_WAITOK | M_ZERO);
673 dom->pd_dom = domain;
674 dom->pd_coord = coord;
675 dom->pd_nproc = nproc;
676 dom->pd_state = 0; /* XXX */
677 dom->pd_sstart = 0; /* XXX */
678 LIST_INIT(&dom->pd_pstlist);
680 LIST_INSERT_HEAD(&acpi_pst_domains, dom, pd_link);
686 acpi_pst_domain_set_pstate(struct acpi_pst_domain *dom, int i)
688 const struct acpi_pstate *pstate;
689 struct acpi_pst_softc *sc;
692 KKASSERT(i >= 0 && i < acpi_npstates);
693 pstate = &acpi_pstates[i];
696 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
698 error = acpi_pst_set_pstate(sc, pstate);
700 device_printf(sc->pst_dev, "can't set "
701 "freq %d\n", pstate->st_freq);
702 /* XXX error cleanup? */
704 if (dom->pd_coord == ACPI_PSD_COORD_SWANY)
715 acpi_pst_global_set_pstate(int i)
717 struct acpi_pst_domain *dom;
719 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
720 /* Skip dead domain */
721 if (dom->pd_flags & ACPI_PSTDOM_FLAG_DEAD)
723 acpi_pst_domain_set_pstate(dom, i);
725 acpi_pst_global_state = i;
731 acpi_pst_postattach(void *arg __unused)
733 struct acpi_pst_domain *dom;
734 struct acpi_cpux_softc *cpux;
736 int i, ndevices, error, has_domain, sstate;
740 error = devclass_get_devices(acpi_pst_devclass, &devices, &ndevices);
748 for (i = 0; i < ndevices; ++i) {
749 cpux = device_get_softc(device_get_parent(devices[i]));
750 if (cpux->glob_sysctl_tree != NULL)
753 kfree(devices, M_TEMP);
754 KKASSERT(cpux != NULL);
756 if (acpi_pst_md == NULL)
757 kprintf("ACPI: no P-State CPU driver\n");
761 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
762 struct acpi_pst_softc *sc;
766 * Make sure that all processors belonging to this
767 * domain are located.
770 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link)
772 if (i != dom->pd_nproc) {
773 KKASSERT(i < dom->pd_nproc);
775 kprintf("ACPI: domain%u misses processors, "
776 "should be %d, got %d\n", dom->pd_dom,
778 if (dom->pd_coord == ACPI_PSD_COORD_SWALL) {
780 * If this domain's coordination is
781 * SWALL and we don't see all of the
782 * member CPUs of this domain, then
783 * the P-State transition will never
784 * be completed, so just leave this
787 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
794 * Validate P-State configurations for this domain
796 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
797 error = acpi_pst_check_csr(sc);
801 error = acpi_pst_check_pstates(sc);
806 kprintf("ACPI: domain%u P-State configuration "
807 "check failed\n", dom->pd_dom);
808 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
813 * Do necssary P-State initialization
815 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
816 error = acpi_pst_init(sc);
821 kprintf("ACPI: domain%u P-State initialization "
822 "check failed\n", dom->pd_dom);
823 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
829 ksnprintf(buf, sizeof(buf), "px_dom%u", dom->pd_dom);
831 sysctl_ctx_init(&dom->pd_sysctl_ctx);
832 dom->pd_sysctl_tree =
833 SYSCTL_ADD_NODE(&dom->pd_sysctl_ctx,
834 SYSCTL_CHILDREN(cpux->glob_sysctl_tree),
835 OID_AUTO, buf, CTLFLAG_RD, 0,
837 if (dom->pd_sysctl_tree == NULL) {
838 kprintf("ACPI: Can't create sysctl tree for domain%u",
843 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
844 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
845 OID_AUTO, "available",
846 CTLTYPE_STRING | CTLFLAG_RD,
847 dom, 0, acpi_pst_sysctl_freqs, "A",
848 "available frequencies");
850 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
851 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
853 CTLTYPE_STRING | CTLFLAG_RD,
854 dom, 0, acpi_pst_sysctl_members, "A",
857 if (acpi_pst_md != NULL &&
858 acpi_pst_md->pmd_set_pstate != NULL) {
859 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
860 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
862 CTLTYPE_UINT | CTLFLAG_RW,
863 dom, 0, acpi_pst_sysctl_select,
864 "IU", "select freq");
867 if (dom->pd_state < sstate)
868 sstate = dom->pd_state;
871 if (has_domain && acpi_pst_md != NULL &&
872 acpi_pst_md->pmd_set_pstate != NULL) {
873 SYSCTL_ADD_PROC(&cpux->glob_sysctl_ctx,
874 SYSCTL_CHILDREN(cpux->glob_sysctl_tree),
875 OID_AUTO, "px_global",
876 CTLTYPE_UINT | CTLFLAG_RW,
877 NULL, 0, acpi_pst_sysctl_global,
878 "IU", "select freq for all domains");
880 acpi_pst_global_set_pstate(sstate);
885 acpi_pst_sysctl_freqs(SYSCTL_HANDLER_ARGS)
887 struct acpi_pst_domain *dom = arg1;
891 for (i = 0; i < acpi_npstates; ++i) {
893 error = SYSCTL_OUT(req, " ", 1);
898 if (i < dom->pd_sstart)
903 ksnprintf(buf, sizeof(buf), pat,
904 acpi_pstates[i].st_freq);
905 error = SYSCTL_OUT(req, buf, strlen(buf));
912 acpi_pst_sysctl_members(SYSCTL_HANDLER_ARGS)
914 struct acpi_pst_domain *dom = arg1;
915 struct acpi_pst_softc *sc;
919 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
922 if (error == 0 && loop)
923 error = SYSCTL_OUT(req, " ", 1);
925 ksnprintf(buf, sizeof(buf), "cpu%d", sc->pst_cpuid);
926 error = SYSCTL_OUT(req, buf, strlen(buf));
929 if (error == 0 && acpi_pst_md && acpi_pst_md->pmd_get_pstate) {
930 const struct acpi_pstate *pstate;
933 pstate = acpi_pst_get_pstate(sc);
934 if (pstate == NULL) {
937 ksnprintf(buf, sizeof(buf), "(%d)",
941 error = SYSCTL_OUT(req, str, strlen(str));
949 acpi_pst_sysctl_select(SYSCTL_HANDLER_ARGS)
951 struct acpi_pst_domain *dom = arg1;
954 KKASSERT(dom->pd_state >= 0 && dom->pd_state < acpi_npstates);
956 freq = acpi_pstates[dom->pd_state].st_freq;
958 error = sysctl_handle_int(oidp, &freq, 0, req);
959 if (error || req->newptr == NULL)
962 i = acpi_pst_freq2index(freq);
966 acpi_pst_domain_set_pstate(dom, i);
971 acpi_pst_sysctl_global(SYSCTL_HANDLER_ARGS)
975 KKASSERT(acpi_pst_global_state >= 0 &&
976 acpi_pst_global_state < acpi_npstates);
978 freq = acpi_pstates[acpi_pst_global_state].st_freq;
980 error = sysctl_handle_int(oidp, &freq, 0, req);
981 if (error || req->newptr == NULL)
984 i = acpi_pst_freq2index(freq);
988 acpi_pst_global_set_pstate(i);
994 acpi_pst_check_csr_handler(netmsg_t msg)
996 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
999 error = acpi_pst_md->pmd_check_csr(rmsg->ctrl, rmsg->status);
1000 lwkt_replymsg(&rmsg->base.lmsg, error);
1004 acpi_pst_check_csr(struct acpi_pst_softc *sc)
1006 struct netmsg_acpi_pst msg;
1008 if (acpi_pst_md == NULL)
1011 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1012 MSGF_PRIORITY, acpi_pst_check_csr_handler);
1013 msg.ctrl = &sc->pst_creg;
1014 msg.status = &sc->pst_sreg;
1016 return lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.base.lmsg, 0);
1020 acpi_pst_check_pstates_handler(netmsg_t msg)
1024 error = acpi_pst_md->pmd_check_pstates(acpi_pstates, acpi_npstates);
1025 lwkt_replymsg(&msg->lmsg, error);
1029 acpi_pst_check_pstates(struct acpi_pst_softc *sc)
1031 struct netmsg_base msg;
1033 if (acpi_pst_md == NULL)
1036 netmsg_init(&msg, NULL, &curthread->td_msgport,
1037 MSGF_PRIORITY, acpi_pst_check_pstates_handler);
1039 return lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.lmsg, 0);
1043 acpi_pst_init_handler(netmsg_t msg)
1045 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1048 error = acpi_pst_md->pmd_init(rmsg->ctrl, rmsg->status);
1049 lwkt_replymsg(&rmsg->base.lmsg, error);
1053 acpi_pst_init(struct acpi_pst_softc *sc)
1055 struct netmsg_acpi_pst msg;
1057 if (acpi_pst_md == NULL)
1060 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1061 MSGF_PRIORITY, acpi_pst_init_handler);
1062 msg.ctrl = &sc->pst_creg;
1063 msg.status = &sc->pst_sreg;
1065 return lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.base.lmsg, 0);
1069 acpi_pst_set_pstate_handler(netmsg_t msg)
1071 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1074 error = acpi_pst_md->pmd_set_pstate(rmsg->ctrl, rmsg->status,
1075 rmsg->base.lmsg.u.ms_resultp);
1076 lwkt_replymsg(&rmsg->base.lmsg, error);
1080 acpi_pst_set_pstate(struct acpi_pst_softc *sc, const struct acpi_pstate *pstate)
1082 struct netmsg_acpi_pst msg;
1084 KKASSERT(acpi_pst_md != NULL);
1086 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1087 MSGF_PRIORITY, acpi_pst_set_pstate_handler);
1088 msg.base.lmsg.u.ms_resultp = __DECONST(void *, pstate);
1089 msg.ctrl = &sc->pst_creg;
1090 msg.status = &sc->pst_sreg;
1092 return lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.base.lmsg, 0);
1096 acpi_pst_get_pstate_handler(netmsg_t msg)
1098 struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1099 const struct acpi_pstate *pstate;
1101 pstate = acpi_pst_md->pmd_get_pstate(rmsg->status, acpi_pstates,
1103 rmsg->base.lmsg.u.ms_resultp = __DECONST(void *, pstate);
1104 lwkt_replymsg(&rmsg->base.lmsg, 0);
1107 static const struct acpi_pstate *
1108 acpi_pst_get_pstate(struct acpi_pst_softc *sc)
1110 struct netmsg_acpi_pst msg;
1112 if (acpi_pst_md == NULL)
1115 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1116 MSGF_PRIORITY, acpi_pst_get_pstate_handler);
1117 msg.status = &sc->pst_sreg;
1119 lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.base.lmsg, 0);
1120 return msg.base.lmsg.u.ms_resultp;
1124 acpi_pst_alloc_resource(device_t dev, ACPI_OBJECT *obj, int idx,
1125 struct acpi_pst_res *res)
1127 struct acpi_pst_softc *sc = device_get_softc(dev);
1131 error = acpi_PkgRawGas(obj, idx, &res->pr_gas);
1135 /* Allocate resource, if possible */
1136 res->pr_rid = sc->pst_parent->cpux_next_rid;
1137 acpi_bus_alloc_gas(dev, &type, &res->pr_rid, &res->pr_gas, &res->pr_res, 0);
1138 if (res->pr_res != NULL) {
1139 sc->pst_parent->cpux_next_rid++;
1140 res->pr_bt = rman_get_bustag(res->pr_res);
1141 res->pr_bh = rman_get_bushandle(res->pr_res);