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 {
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 */
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(device_t, ACPI_OBJECT *);
119 static struct acpi_pst_domain *
120 acpi_pst_domain_find(uint32_t);
121 static struct acpi_pst_domain *
122 acpi_pst_domain_alloc(uint32_t, uint32_t, uint32_t);
123 static int acpi_pst_domain_set_pstate(struct acpi_pst_domain *, int);
124 static int acpi_pst_global_set_pstate(int);
126 static int acpi_pst_check_csr(struct acpi_pst_softc *);
127 static int acpi_pst_check_pstates(struct acpi_pst_softc *);
128 static int acpi_pst_init(struct acpi_pst_softc *);
129 static int acpi_pst_set_pstate(struct acpi_pst_softc *,
130 const struct acpi_pstate *);
131 static const struct acpi_pstate *
132 acpi_pst_get_pstate(struct acpi_pst_softc *);
133 static int acpi_pst_alloc_resource(device_t, ACPI_OBJECT *, int,
134 struct acpi_pst_res *);
136 static void acpi_pst_check_csr_handler(struct netmsg *);
137 static void acpi_pst_check_pstates_handler(struct netmsg *);
138 static void acpi_pst_init_handler(struct netmsg *);
139 static void acpi_pst_set_pstate_handler(struct netmsg *);
140 static void acpi_pst_get_pstate_handler(struct netmsg *);
142 static int acpi_pst_sysctl_freqs(SYSCTL_HANDLER_ARGS);
143 static int acpi_pst_sysctl_members(SYSCTL_HANDLER_ARGS);
144 static int acpi_pst_sysctl_select(SYSCTL_HANDLER_ARGS);
145 static int acpi_pst_sysctl_global(SYSCTL_HANDLER_ARGS);
147 static struct acpi_pst_domlist acpi_pst_domains =
148 LIST_HEAD_INITIALIZER(acpi_pst_domains);
149 static int acpi_pst_domain_id;
151 static int acpi_pst_global_state;
153 static int acpi_npstates;
154 static struct acpi_pstate *acpi_pstates;
156 static const struct acpi_pst_md *acpi_pst_md;
158 static device_method_t acpi_pst_methods[] = {
159 /* Device interface */
160 DEVMETHOD(device_probe, acpi_pst_probe),
161 DEVMETHOD(device_attach, acpi_pst_attach),
162 DEVMETHOD(device_detach, bus_generic_detach),
163 DEVMETHOD(device_shutdown, bus_generic_shutdown),
164 DEVMETHOD(device_suspend, bus_generic_suspend),
165 DEVMETHOD(device_resume, bus_generic_resume),
168 DEVMETHOD(bus_add_child, bus_generic_add_child),
169 DEVMETHOD(bus_print_child, bus_generic_print_child),
170 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar),
171 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar),
172 DEVMETHOD(bus_get_resource_list, bus_generic_get_resource_list),
173 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
174 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
175 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
176 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
177 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
178 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
179 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
180 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
181 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
186 static driver_t acpi_pst_driver = {
189 sizeof(struct acpi_pst_softc)
192 static devclass_t acpi_pst_devclass;
193 DRIVER_MODULE(cpu_pst, cpu, acpi_pst_driver, acpi_pst_devclass, 0, 0);
194 MODULE_DEPEND(cpu_pst, acpi, 1, 1, 1);
197 acpi_pst_freq2index(int freq)
201 for (i = 0; i < acpi_npstates; ++i) {
202 if (acpi_pstates[i].st_freq == freq)
209 acpi_pst_probe(device_t dev)
216 if (acpi_disabled("cpu_pst") ||
217 acpi_get_type(dev) != ACPI_TYPE_PROCESSOR)
220 if (acpi_pst_md == NULL)
221 acpi_pst_md = acpi_pst_md_probe();
223 handle = acpi_get_handle(dev);
229 buf.Length = ACPI_ALLOCATE_BUFFER;
230 status = AcpiEvaluateObject(handle, "_PCT", NULL, &buf);
231 if (ACPI_FAILURE(status)) {
233 device_printf(dev, "Can't get _PCT package - %s\n",
234 AcpiFormatException(status));
239 obj = (ACPI_OBJECT *)buf.Pointer;
240 if (!ACPI_PKG_VALID_EQ(obj, 2)) {
241 device_printf(dev, "Invalid _PCT package\n");
251 buf.Length = ACPI_ALLOCATE_BUFFER;
252 status = AcpiEvaluateObject(handle, "_PSS", NULL, &buf);
253 if (ACPI_FAILURE(status)) {
254 device_printf(dev, "Can't get _PSS package - %s\n",
255 AcpiFormatException(status));
259 obj = (ACPI_OBJECT *)buf.Pointer;
260 if (!ACPI_PKG_VALID(obj, 1)) {
261 device_printf(dev, "Invalid _PSS package\n");
267 device_set_desc(dev, "ACPI CPU P-State");
272 acpi_pst_attach(device_t dev)
274 struct acpi_pst_softc *sc = device_get_softc(dev), *pst;
275 struct acpi_pst_domain *dom = NULL;
279 struct acpi_pstate *pstate, *p;
280 int i, npstate, error;
283 sc->pst_parent = device_get_softc(device_get_parent(dev));
284 sc->pst_handle = acpi_get_handle(dev);
285 sc->pst_cpuid = acpi_get_magic(dev);
288 * If there is a _PSD, then we create procossor domain
289 * accordingly. If there is no _PSD, we just fake a
290 * default processor domain0.
293 buf.Length = ACPI_ALLOCATE_BUFFER;
294 status = AcpiEvaluateObject(sc->pst_handle, "_PSD", NULL, &buf);
295 if (!ACPI_FAILURE(status)) {
296 obj = (ACPI_OBJECT *)buf.Pointer;
297 if (ACPI_PKG_VALID_EQ(obj, 1)) {
298 dom = acpi_pst_domain_create(dev,
299 &obj->Package.Elements[0]);
305 device_printf(dev, "Invalid _PSD package\n");
311 AcpiOsFree(buf.Pointer);
314 * Create a stub one processor domain for each processor
316 dom = acpi_pst_domain_alloc(acpi_pst_domain_id,
317 ACPI_PSD_COORD_SWANY, 1);
318 dom->pd_flags |= ACPI_PSTDOM_FLAG_STUB;
320 ++acpi_pst_domain_id;
323 /* Make sure that adding us will not overflow our domain */
325 LIST_FOREACH(pst, &dom->pd_pstlist, pst_link)
327 if (i == dom->pd_nproc) {
328 device_printf(dev, "Domain%u already contains %d P-States, "
329 "invalid _PSD package\n",
330 dom->pd_dom, dom->pd_nproc);
333 KKASSERT(i < dom->pd_nproc);
336 * Get control/status registers from _PCT
339 buf.Length = ACPI_ALLOCATE_BUFFER;
340 status = AcpiEvaluateObject(sc->pst_handle, "_PCT", NULL, &buf);
341 if (ACPI_FAILURE(status)) {
342 device_printf(dev, "Can't get _PCT package - %s\n",
343 AcpiFormatException(status));
347 obj = (ACPI_OBJECT *)buf.Pointer;
348 if (!ACPI_PKG_VALID_EQ(obj, 2)) {
349 device_printf(dev, "Invalid _PCT package\n");
354 /* Save and try allocating control register */
355 error = acpi_pst_alloc_resource(dev, obj, 0, &sc->pst_creg);
361 device_printf(dev, "control reg %d %jx\n",
362 sc->pst_creg.pr_gas.SpaceId,
363 (uintmax_t)sc->pst_creg.pr_gas.Address);
366 /* Save and try allocating status register */
367 error = acpi_pst_alloc_resource(dev, obj, 1, &sc->pst_sreg);
373 device_printf(dev, "status reg %d %jx\n",
374 sc->pst_sreg.pr_gas.SpaceId,
375 (uintmax_t)sc->pst_sreg.pr_gas.Address);
382 * Create P-State table according to _PSS
385 buf.Length = ACPI_ALLOCATE_BUFFER;
386 status = AcpiEvaluateObject(sc->pst_handle, "_PSS", NULL, &buf);
387 if (ACPI_FAILURE(status)) {
388 device_printf(dev, "Can't get _PSS package - %s\n",
389 AcpiFormatException(status));
393 obj = (ACPI_OBJECT *)buf.Pointer;
394 if (!ACPI_PKG_VALID(obj, 1)) {
395 device_printf(dev, "Invalid _PSS package\n");
400 /* Don't create too many P-States */
401 npstate = obj->Package.Count;
402 if (npstate > ACPI_NPSTATE_MAX) {
403 device_printf(dev, "Too many P-States, %d->%d\n",
404 npstate, ACPI_NPSTATE_MAX);
405 npstate = ACPI_NPSTATE_MAX;
409 * If we have already created P-State table,
410 * we must make sure that number of entries
413 if (acpi_pstates != NULL && acpi_npstates != npstate) {
414 device_printf(dev, "Inconsistent # of P-States "
415 "cross Processor objects\n");
421 * Create a temporary P-State table
423 pstate = kmalloc(sizeof(*pstate) * npstate, M_TEMP, M_WAITOK);
424 for (i = 0, p = pstate; i < npstate; ++i, ++p) {
426 uint32_t *ptr[ACPI_PSS_PX_NENTRY] = {
427 &p->st_freq, &p->st_power, &p->st_xsit_lat,
428 &p->st_bm_lat, &p->st_cval, &p->st_sval
432 pkg = &obj->Package.Elements[i];
433 if (!ACPI_PKG_VALID(pkg, ACPI_PSS_PX_NENTRY)) {
434 device_printf(dev, "Invalud _PSS P%d\n", i);
436 kfree(pstate, M_TEMP);
439 for (j = 0; j < ACPI_PSS_PX_NENTRY; ++j) {
440 if (acpi_PkgInt32(pkg, j, ptr[j]) != 0) {
441 device_printf(dev, "Can't extract "
442 "_PSS P%d %dth entry\n", i, j);
444 kfree(pstate, M_TEMP);
453 if (acpi_pstates == NULL) {
455 * If no P-State table is created yet,
456 * save the temporary one we just created.
458 acpi_pstates = pstate;
459 acpi_npstates = npstate;
463 for (i = 0; i < acpi_npstates; ++i) {
465 "freq %u, pwr %u, xlat %u, blat %u, "
466 "cv %08x, sv %08x\n",
467 acpi_pstates[i].st_freq,
468 acpi_pstates[i].st_power,
469 acpi_pstates[i].st_xsit_lat,
470 acpi_pstates[i].st_bm_lat,
471 acpi_pstates[i].st_cval,
472 acpi_pstates[i].st_sval);
477 * Make sure that P-State tables are same
478 * for all processors.
480 if (memcmp(pstate, acpi_pstates,
481 sizeof(*pstate) * npstate) != 0) {
482 device_printf(dev, "Inconsistent _PSS "
483 "cross Processor objects\n");
484 kfree(pstate, M_TEMP);
487 kfree(pstate, M_TEMP);
490 /* By default, we start from P-State table's first entry */
494 * Adjust the usable first entry of P-State table,
495 * if there is _PPC object.
498 buf.Length = ACPI_ALLOCATE_BUFFER;
499 status = AcpiEvaluateObject(sc->pst_handle, "_PPC", NULL, &buf);
500 if (!ACPI_FAILURE(status)) {
501 obj = (ACPI_OBJECT *)buf.Pointer;
502 if (obj->Type == ACPI_TYPE_INTEGER) {
503 if (obj->Integer.Value >= acpi_npstates) {
504 device_printf(dev, "Invalid _PPC value\n");
508 sc->pst_sstart = obj->Integer.Value;
510 device_printf(dev, "_PPC %d\n", sc->pst_sstart);
512 /* TODO: Install notifiy handler */
514 device_printf(dev, "Invalid _PPC object\n");
523 sc->pst_state = sc->pst_sstart;
525 /* Link us with the domain */
526 sc->pst_domain = dom;
527 LIST_INSERT_HEAD(&dom->pd_pstlist, sc, pst_link);
529 if (device_get_unit(dev) == 0)
530 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_pst_postattach, NULL);
535 static struct acpi_pst_domain *
536 acpi_pst_domain_create(device_t dev, ACPI_OBJECT *obj)
538 struct acpi_pst_domain *dom;
539 uint32_t val, domain, coord, nproc;
541 if (!ACPI_PKG_VALID_EQ(obj, 5)) {
542 device_printf(dev, "Invalid _PSD package\n");
546 /* NumberOfEntries */
547 if (acpi_PkgInt32(obj, 0, &val) != 0 || val != 5) {
548 device_printf(dev, "Invalid _PSD NumberOfEntries\n");
553 if (acpi_PkgInt32(obj, 1, &val) != 0 || val != 0) {
554 device_printf(dev, "Invalid _PSD Revision\n");
558 if (acpi_PkgInt32(obj, 2, &domain) != 0 ||
559 acpi_PkgInt32(obj, 3, &coord) != 0 ||
560 acpi_PkgInt32(obj, 4, &nproc) != 0) {
561 device_printf(dev, "Can't extract _PSD package\n");
565 if (!ACPI_PSD_COORD_VALID(coord)) {
566 device_printf(dev, "Invalid _PSD CoordType (%#x)\n", coord);
570 if (nproc > MAXCPU) {
572 * If NumProcessors is greater than MAXCPU
573 * and domain's coordination is SWALL, then
574 * we will never be able to start all CPUs
575 * within this domain, and power state
576 * transition will never be completed, so we
577 * just bail out here.
579 if (coord == ACPI_PSD_COORD_SWALL) {
580 device_printf(dev, "Unsupported _PSD NumProcessors "
584 } else if (nproc == 0) {
585 device_printf(dev, "_PSD NumProcessors are zero\n");
589 dom = acpi_pst_domain_find(domain);
591 if (dom->pd_coord != coord || dom->pd_nproc != nproc) {
592 device_printf(dev, "Inconsistent _PSD information "
593 "cross Processor objects\n");
599 dom = acpi_pst_domain_alloc(domain, coord, nproc);
601 device_printf(dev, "create domain%u\n", dom->pd_dom);
606 static struct acpi_pst_domain *
607 acpi_pst_domain_find(uint32_t domain)
609 struct acpi_pst_domain *dom;
611 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
612 if (dom->pd_flags & ACPI_PSTDOM_FLAG_STUB)
614 if (dom->pd_dom == domain)
620 static struct acpi_pst_domain *
621 acpi_pst_domain_alloc(uint32_t domain, uint32_t coord, uint32_t nproc)
623 struct acpi_pst_domain *dom;
625 dom = kmalloc(sizeof(*dom), M_DEVBUF, M_WAITOK | M_ZERO);
626 dom->pd_dom = domain;
627 dom->pd_coord = coord;
628 dom->pd_nproc = nproc;
629 dom->pd_state = 0; /* XXX */
630 dom->pd_sstart = 0; /* XXX */
631 LIST_INIT(&dom->pd_pstlist);
633 LIST_INSERT_HEAD(&acpi_pst_domains, dom, pd_link);
639 acpi_pst_domain_set_pstate(struct acpi_pst_domain *dom, int i)
641 const struct acpi_pstate *pstate;
642 struct acpi_pst_softc *sc;
645 KKASSERT(i >= 0 && i < acpi_npstates);
646 pstate = &acpi_pstates[i];
649 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
651 error = acpi_pst_set_pstate(sc, pstate);
653 device_printf(sc->pst_dev, "can't set "
654 "freq %d\n", pstate->st_freq);
655 /* XXX error cleanup? */
657 if (dom->pd_coord == ACPI_PSD_COORD_SWANY)
668 acpi_pst_global_set_pstate(int i)
670 struct acpi_pst_domain *dom;
672 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
673 /* Skip dead domain */
674 if (dom->pd_flags & ACPI_PSTDOM_FLAG_DEAD)
676 acpi_pst_domain_set_pstate(dom, i);
678 acpi_pst_global_state = i;
684 acpi_pst_postattach(void *arg __unused)
686 struct acpi_pst_domain *dom;
687 struct acpi_cpux_softc *cpux;
689 int i, ndevices, error, has_domain, sstate;
693 error = devclass_get_devices(acpi_pst_devclass, &devices, &ndevices);
701 for (i = 0; i < ndevices; ++i) {
702 cpux = device_get_softc(device_get_parent(devices[i]));
703 if (cpux->glob_sysctl_tree != NULL)
706 kfree(devices, M_TEMP);
707 KKASSERT(cpux != NULL);
709 if (acpi_pst_md == NULL)
710 kprintf("ACPI: no P-State CPU driver\n");
714 LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
715 struct acpi_pst_softc *sc;
719 * Make sure that all processors belonging to this
720 * domain are located.
723 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link)
725 if (i != dom->pd_nproc) {
726 KKASSERT(i < dom->pd_nproc);
728 kprintf("ACPI: domain%u misses processors, "
729 "should be %d, got %d\n", dom->pd_dom,
731 if (dom->pd_coord == ACPI_PSD_COORD_SWALL) {
733 * If this domain's coordination is
734 * SWALL and we don't see all of the
735 * member CPUs of this domain, then
736 * the P-State transition will never
737 * be completed, so just leave this
740 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
747 * Validate P-State configurations for this domain
749 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
750 error = acpi_pst_check_csr(sc);
754 error = acpi_pst_check_pstates(sc);
759 kprintf("ACPI: domain%u P-State configuration "
760 "check failed\n", dom->pd_dom);
761 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
766 * Do necssary P-State initialization
768 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
769 error = acpi_pst_init(sc);
774 kprintf("ACPI: domain%u P-State initialization "
775 "check failed\n", dom->pd_dom);
776 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
782 ksnprintf(buf, sizeof(buf), "px_dom%u", dom->pd_dom);
784 sysctl_ctx_init(&dom->pd_sysctl_ctx);
785 dom->pd_sysctl_tree =
786 SYSCTL_ADD_NODE(&dom->pd_sysctl_ctx,
787 SYSCTL_CHILDREN(cpux->glob_sysctl_tree),
788 OID_AUTO, buf, CTLFLAG_RD, 0,
790 if (dom->pd_sysctl_tree == NULL) {
791 kprintf("ACPI: Can't create sysctl tree for domain%u",
796 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
797 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
798 OID_AUTO, "available",
799 CTLTYPE_STRING | CTLFLAG_RD,
800 dom, 0, acpi_pst_sysctl_freqs, "A",
801 "available frequencies");
803 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
804 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
806 CTLTYPE_STRING | CTLFLAG_RD,
807 dom, 0, acpi_pst_sysctl_members, "A",
810 if (acpi_pst_md != NULL &&
811 acpi_pst_md->pmd_set_pstate != NULL) {
812 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
813 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
815 CTLTYPE_UINT | CTLFLAG_RW,
816 dom, 0, acpi_pst_sysctl_select,
817 "IU", "select freq");
820 if (dom->pd_state < sstate)
821 sstate = dom->pd_state;
824 if (has_domain && acpi_pst_md != NULL &&
825 acpi_pst_md->pmd_set_pstate != NULL) {
826 SYSCTL_ADD_PROC(&cpux->glob_sysctl_ctx,
827 SYSCTL_CHILDREN(cpux->glob_sysctl_tree),
828 OID_AUTO, "px_global",
829 CTLTYPE_UINT | CTLFLAG_RW,
830 NULL, 0, acpi_pst_sysctl_global,
831 "IU", "select freq for all domains");
833 acpi_pst_global_set_pstate(sstate);
838 acpi_pst_sysctl_freqs(SYSCTL_HANDLER_ARGS)
840 struct acpi_pst_domain *dom = arg1;
844 for (i = 0; i < acpi_npstates; ++i) {
846 error = SYSCTL_OUT(req, " ", 1);
851 if (i < dom->pd_sstart)
856 ksnprintf(buf, sizeof(buf), pat,
857 acpi_pstates[i].st_freq);
858 error = SYSCTL_OUT(req, buf, strlen(buf));
865 acpi_pst_sysctl_members(SYSCTL_HANDLER_ARGS)
867 struct acpi_pst_domain *dom = arg1;
868 struct acpi_pst_softc *sc;
872 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
875 if (error == 0 && loop)
876 error = SYSCTL_OUT(req, " ", 1);
878 ksnprintf(buf, sizeof(buf), "cpu%d", sc->pst_cpuid);
879 error = SYSCTL_OUT(req, buf, strlen(buf));
882 if (error == 0 && acpi_pst_md && acpi_pst_md->pmd_get_pstate) {
883 const struct acpi_pstate *pstate;
886 pstate = acpi_pst_get_pstate(sc);
887 if (pstate == NULL) {
890 ksnprintf(buf, sizeof(buf), "(%d)",
894 error = SYSCTL_OUT(req, str, strlen(str));
902 acpi_pst_sysctl_select(SYSCTL_HANDLER_ARGS)
904 struct acpi_pst_domain *dom = arg1;
907 KKASSERT(dom->pd_state >= 0 && dom->pd_state < acpi_npstates);
909 freq = acpi_pstates[dom->pd_state].st_freq;
911 error = sysctl_handle_int(oidp, &freq, 0, req);
912 if (error || req->newptr == NULL)
915 i = acpi_pst_freq2index(freq);
919 acpi_pst_domain_set_pstate(dom, i);
924 acpi_pst_sysctl_global(SYSCTL_HANDLER_ARGS)
928 KKASSERT(acpi_pst_global_state >= 0 &&
929 acpi_pst_global_state < acpi_npstates);
931 freq = acpi_pstates[acpi_pst_global_state].st_freq;
933 error = sysctl_handle_int(oidp, &freq, 0, req);
934 if (error || req->newptr == NULL)
937 i = acpi_pst_freq2index(freq);
941 acpi_pst_global_set_pstate(i);
947 acpi_pst_check_csr_handler(struct netmsg *nmsg)
949 struct netmsg_acpi_pst *msg = (struct netmsg_acpi_pst *)nmsg;
952 error = acpi_pst_md->pmd_check_csr(msg->ctrl, msg->status);
953 lwkt_replymsg(&nmsg->nm_lmsg, error);
957 acpi_pst_check_csr(struct acpi_pst_softc *sc)
959 struct netmsg_acpi_pst msg;
961 if (acpi_pst_md == NULL)
964 netmsg_init(&msg.nmsg, NULL, &curthread->td_msgport,
965 MSGF_PRIORITY, acpi_pst_check_csr_handler);
966 msg.ctrl = &sc->pst_creg;
967 msg.status = &sc->pst_sreg;
969 return lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.nmsg.nm_lmsg, 0);
973 acpi_pst_check_pstates_handler(struct netmsg *nmsg)
977 error = acpi_pst_md->pmd_check_pstates(acpi_pstates, acpi_npstates);
978 lwkt_replymsg(&nmsg->nm_lmsg, error);
982 acpi_pst_check_pstates(struct acpi_pst_softc *sc)
986 if (acpi_pst_md == NULL)
989 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
990 MSGF_PRIORITY, acpi_pst_check_pstates_handler);
992 return lwkt_domsg(cpu_portfn(sc->pst_cpuid), &nmsg.nm_lmsg, 0);
996 acpi_pst_init_handler(struct netmsg *nmsg)
998 struct netmsg_acpi_pst *msg = (struct netmsg_acpi_pst *)nmsg;
1001 error = acpi_pst_md->pmd_init(msg->ctrl, msg->status);
1002 lwkt_replymsg(&nmsg->nm_lmsg, error);
1006 acpi_pst_init(struct acpi_pst_softc *sc)
1008 struct netmsg_acpi_pst msg;
1010 if (acpi_pst_md == NULL)
1013 netmsg_init(&msg.nmsg, NULL, &curthread->td_msgport,
1014 MSGF_PRIORITY, acpi_pst_init_handler);
1015 msg.ctrl = &sc->pst_creg;
1016 msg.status = &sc->pst_sreg;
1018 return lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.nmsg.nm_lmsg, 0);
1022 acpi_pst_set_pstate_handler(struct netmsg *nmsg)
1024 struct netmsg_acpi_pst *msg = (struct netmsg_acpi_pst *)nmsg;
1027 error = acpi_pst_md->pmd_set_pstate(msg->ctrl, msg->status,
1028 nmsg->nm_lmsg.u.ms_resultp);
1029 lwkt_replymsg(&nmsg->nm_lmsg, error);
1033 acpi_pst_set_pstate(struct acpi_pst_softc *sc, const struct acpi_pstate *pstate)
1035 struct netmsg_acpi_pst msg;
1037 KKASSERT(acpi_pst_md != NULL);
1039 netmsg_init(&msg.nmsg, NULL, &curthread->td_msgport,
1040 MSGF_PRIORITY, acpi_pst_set_pstate_handler);
1041 msg.nmsg.nm_lmsg.u.ms_resultp = __DECONST(void *, pstate);
1042 msg.ctrl = &sc->pst_creg;
1043 msg.status = &sc->pst_sreg;
1045 return lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.nmsg.nm_lmsg, 0);
1049 acpi_pst_get_pstate_handler(struct netmsg *nmsg)
1051 struct netmsg_acpi_pst *msg = (struct netmsg_acpi_pst *)nmsg;
1052 const struct acpi_pstate *pstate;
1054 pstate = acpi_pst_md->pmd_get_pstate(msg->status, acpi_pstates,
1056 nmsg->nm_lmsg.u.ms_resultp = __DECONST(void *, pstate);
1057 lwkt_replymsg(&nmsg->nm_lmsg, 0);
1060 static const struct acpi_pstate *
1061 acpi_pst_get_pstate(struct acpi_pst_softc *sc)
1063 struct netmsg_acpi_pst msg;
1065 if (acpi_pst_md == NULL)
1068 netmsg_init(&msg.nmsg, NULL, &curthread->td_msgport,
1069 MSGF_PRIORITY, acpi_pst_get_pstate_handler);
1070 msg.status = &sc->pst_sreg;
1072 lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.nmsg.nm_lmsg, 0);
1073 return msg.nmsg.nm_lmsg.u.ms_resultp;
1077 acpi_pst_alloc_resource(device_t dev, ACPI_OBJECT *obj, int idx,
1078 struct acpi_pst_res *res)
1080 struct acpi_pst_softc *sc = device_get_softc(dev);
1084 error = acpi_PkgRawGas(obj, idx, &res->pr_gas);
1088 /* Allocate resource, if possible */
1089 res->pr_rid = sc->pst_parent->cpux_next_rid;
1090 acpi_bus_alloc_gas(dev, &type, &res->pr_rid, &res->pr_gas, &res->pr_res, 0);
1091 if (res->pr_res != NULL) {
1092 sc->pst_parent->cpux_next_rid++;
1093 res->pr_bt = rman_get_bustag(res->pr_res);
1094 res->pr_bh = rman_get_bushandle(res->pr_res);