acpi/pstate: Allow user to force package level P-state domain
[dragonfly.git] / sys / dev / acpica / acpi_cpu_pstate.c
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Sepherosa Ziehau <sepherosa@gmail.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
16  *    distribution.
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.
20  * 
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
32  * SUCH DAMAGE.
33  */
34
35 #include "opt_acpi.h"
36
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/queue.h>
42 #include <sys/rman.h>
43 #include <sys/sysctl.h>
44 #include <sys/msgport2.h>
45 #include <sys/cpu_topology.h>
46
47 #include <net/netisr2.h>
48 #include <net/netmsg2.h>
49 #include <net/if_var.h>
50
51 #include "acpi.h"
52 #include "acpivar.h"
53 #include "acpi_cpu.h"
54 #include "acpi_cpu_pstate.h"
55
56 #define ACPI_NPSTATE_MAX        16
57
58 #define ACPI_PSS_PX_NENTRY      6
59
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)
67
68 struct acpi_pst_softc;
69 LIST_HEAD(acpi_pst_list, acpi_pst_softc);
70
71 struct netmsg_acpi_pst {
72         struct netmsg_base base;
73         const struct acpi_pst_res *ctrl;
74         const struct acpi_pst_res *status;
75 };
76
77 struct acpi_pst_domain {
78         uint32_t                pd_dom;
79         uint32_t                pd_coord;
80         uint32_t                pd_nproc;
81         LIST_ENTRY(acpi_pst_domain) pd_link;
82
83         uint32_t                pd_flags;
84
85         int                     pd_state;
86         int                     pd_sstart;
87         struct acpi_pst_list    pd_pstlist;
88
89         struct sysctl_ctx_list  pd_sysctl_ctx;
90         struct sysctl_oid       *pd_sysctl_tree;
91 };
92 LIST_HEAD(acpi_pst_domlist, acpi_pst_domain);
93
94 #define ACPI_PSTDOM_FLAG_STUB   0x1     /* stub domain, no _PSD */
95 #define ACPI_PSTDOM_FLAG_DEAD   0x2     /* domain can't be started */
96 #define ACPI_PSTDOM_FLAG_INT    0x4     /* domain created from Integer _PSD */
97
98 struct acpi_pst_softc {
99         device_t                pst_dev;
100         struct acpi_cpu_softc   *pst_parent;
101         struct acpi_pst_domain  *pst_domain;
102         struct acpi_pst_res     pst_creg;
103         struct acpi_pst_res     pst_sreg;
104
105         int                     pst_state;
106         int                     pst_sstart;
107         int                     pst_cpuid;
108
109         ACPI_HANDLE             pst_handle;
110
111         LIST_ENTRY(acpi_pst_softc) pst_link;
112 };
113
114 static int      acpi_pst_probe(device_t dev);
115 static int      acpi_pst_attach(device_t dev);
116
117 static void     acpi_pst_postattach(void *);
118 static struct acpi_pst_domain *
119                 acpi_pst_domain_create_int(device_t, uint32_t);
120 static struct acpi_pst_domain *
121                 acpi_pst_domain_create_pkg(device_t, ACPI_OBJECT *);
122 static struct acpi_pst_domain *
123                 acpi_pst_domain_find(uint32_t);
124 static struct acpi_pst_domain *
125                 acpi_pst_domain_alloc(uint32_t, uint32_t, uint32_t);
126 static int      acpi_pst_domain_set_pstate(struct acpi_pst_domain *, int);
127 static void     acpi_pst_domain_check_nproc(device_t, struct acpi_pst_domain *);
128 static int      acpi_pst_global_set_pstate(int);
129
130 static int      acpi_pst_check_csr(struct acpi_pst_softc *);
131 static int      acpi_pst_check_pstates(struct acpi_pst_softc *);
132 static int      acpi_pst_init(struct acpi_pst_softc *);
133 static int      acpi_pst_set_pstate(struct acpi_pst_softc *,
134                     const struct acpi_pstate *);
135 static const struct acpi_pstate *
136                 acpi_pst_get_pstate(struct acpi_pst_softc *);
137 static int      acpi_pst_alloc_resource(device_t, ACPI_OBJECT *, int,
138                     struct acpi_pst_res *);
139
140 static void     acpi_pst_check_csr_handler(netmsg_t);
141 static void     acpi_pst_check_pstates_handler(netmsg_t);
142 static void     acpi_pst_init_handler(netmsg_t);
143 static void     acpi_pst_set_pstate_handler(netmsg_t);
144 static void     acpi_pst_get_pstate_handler(netmsg_t);
145
146 static int      acpi_pst_sysctl_freqs(SYSCTL_HANDLER_ARGS);
147 static int      acpi_pst_sysctl_members(SYSCTL_HANDLER_ARGS);
148 static int      acpi_pst_sysctl_select(SYSCTL_HANDLER_ARGS);
149 static int      acpi_pst_sysctl_global(SYSCTL_HANDLER_ARGS);
150
151 static struct acpi_pst_domlist  acpi_pst_domains =
152         LIST_HEAD_INITIALIZER(acpi_pst_domains);
153 static int                      acpi_pst_domain_id;
154
155 static int                      acpi_pst_global_state;
156
157 static int                      acpi_npstates;
158 static struct acpi_pstate       *acpi_pstates;
159
160 static const struct acpi_pst_md *acpi_pst_md;
161
162 static int                      acpi_pst_ht_reuse_domain = 1;
163 TUNABLE_INT("hw.acpi.cpu.pst.ht_reuse_domain", &acpi_pst_ht_reuse_domain);
164
165 static int                      acpi_pst_force_pkg_domain = 0;
166 TUNABLE_INT("hw.acpi.cpu.pst.force_pkg_domain", &acpi_pst_force_pkg_domain);
167
168 static device_method_t acpi_pst_methods[] = {
169         /* Device interface */
170         DEVMETHOD(device_probe,                 acpi_pst_probe),
171         DEVMETHOD(device_attach,                acpi_pst_attach),
172         DEVMETHOD(device_detach,                bus_generic_detach),
173         DEVMETHOD(device_shutdown,              bus_generic_shutdown),
174         DEVMETHOD(device_suspend,               bus_generic_suspend),
175         DEVMETHOD(device_resume,                bus_generic_resume),
176
177         /* Bus interface */
178         DEVMETHOD(bus_add_child,                bus_generic_add_child),
179         DEVMETHOD(bus_print_child,              bus_generic_print_child),
180         DEVMETHOD(bus_read_ivar,                bus_generic_read_ivar),
181         DEVMETHOD(bus_write_ivar,               bus_generic_write_ivar),
182         DEVMETHOD(bus_get_resource_list,        bus_generic_get_resource_list),
183         DEVMETHOD(bus_set_resource,             bus_generic_rl_set_resource),
184         DEVMETHOD(bus_get_resource,             bus_generic_rl_get_resource),
185         DEVMETHOD(bus_alloc_resource,           bus_generic_alloc_resource),
186         DEVMETHOD(bus_release_resource,         bus_generic_release_resource),
187         DEVMETHOD(bus_driver_added,             bus_generic_driver_added),
188         DEVMETHOD(bus_activate_resource,        bus_generic_activate_resource),
189         DEVMETHOD(bus_deactivate_resource,      bus_generic_deactivate_resource),
190         DEVMETHOD(bus_setup_intr,               bus_generic_setup_intr),
191         DEVMETHOD(bus_teardown_intr,            bus_generic_teardown_intr),
192
193         DEVMETHOD_END
194 };
195
196 static driver_t acpi_pst_driver = {
197         "cpu_pst",
198         acpi_pst_methods,
199         sizeof(struct acpi_pst_softc)
200 };
201
202 static devclass_t acpi_pst_devclass;
203 DRIVER_MODULE(cpu_pst, cpu, acpi_pst_driver, acpi_pst_devclass, NULL, NULL);
204 MODULE_DEPEND(cpu_pst, acpi, 1, 1, 1);
205
206 static __inline int
207 acpi_pst_freq2index(int freq)
208 {
209         int i;
210
211         for (i = 0; i < acpi_npstates; ++i) {
212                 if (acpi_pstates[i].st_freq == freq)
213                         return i;
214         }
215         return -1;
216 }
217
218 static int
219 acpi_pst_probe(device_t dev)
220 {
221         ACPI_BUFFER buf;
222         ACPI_HANDLE handle;
223         ACPI_STATUS status;
224         ACPI_OBJECT *obj;
225
226         if (acpi_disabled("cpu_pst") ||
227             acpi_get_type(dev) != ACPI_TYPE_PROCESSOR)
228                 return ENXIO;
229
230         if (acpi_pst_md == NULL)
231                 acpi_pst_md = acpi_pst_md_probe();
232
233         handle = acpi_get_handle(dev);
234
235         /*
236          * Check _PSD package
237          *
238          * NOTE:
239          * Some BIOSes do not expose _PCT for the second thread of
240          * CPU cores.  In this case, _PSD should be enough to get the
241          * P-state of the second thread working, since it must have
242          * the same _PCT and _PSS as the first thread in the same
243          * core.
244          */
245         buf.Pointer = NULL;
246         buf.Length = ACPI_ALLOCATE_BUFFER;
247         status = AcpiEvaluateObject(handle, "_PSD", NULL, &buf);
248         if (!ACPI_FAILURE(status)) {
249                 AcpiOsFree((ACPI_OBJECT *)buf.Pointer);
250                 goto done;
251         }
252
253         /*
254          * Check _PCT package
255          */
256         buf.Pointer = NULL;
257         buf.Length = ACPI_ALLOCATE_BUFFER;
258         status = AcpiEvaluateObject(handle, "_PCT", NULL, &buf);
259         if (ACPI_FAILURE(status)) {
260                 if (bootverbose) {
261                         device_printf(dev, "Can't get _PCT package - %s\n",
262                                       AcpiFormatException(status));
263                 }
264                 return ENXIO;
265         }
266
267         obj = (ACPI_OBJECT *)buf.Pointer;
268         if (!ACPI_PKG_VALID_EQ(obj, 2)) {
269                 device_printf(dev, "Invalid _PCT package\n");
270                 AcpiOsFree(obj);
271                 return ENXIO;
272         }
273         AcpiOsFree(obj);
274
275         /*
276          * Check _PSS package
277          */
278         buf.Pointer = NULL;
279         buf.Length = ACPI_ALLOCATE_BUFFER;
280         status = AcpiEvaluateObject(handle, "_PSS", NULL, &buf);
281         if (ACPI_FAILURE(status)) {
282                 device_printf(dev, "Can't get _PSS package - %s\n",
283                               AcpiFormatException(status));
284                 return ENXIO;
285         }
286
287         obj = (ACPI_OBJECT *)buf.Pointer;
288         if (!ACPI_PKG_VALID(obj, 1)) {
289                 device_printf(dev, "Invalid _PSS package\n");
290                 AcpiOsFree(obj);
291                 return ENXIO;
292         }
293         AcpiOsFree(obj);
294
295 done:
296         device_set_desc(dev, "ACPI CPU P-State");
297         return 0;
298 }
299
300 static int
301 acpi_pst_attach(device_t dev)
302 {
303         struct acpi_pst_softc *sc = device_get_softc(dev);
304         struct acpi_pst_domain *dom = NULL;
305         ACPI_BUFFER buf;
306         ACPI_STATUS status;
307         ACPI_OBJECT *obj;
308         struct acpi_pstate *pstate, *p;
309         int i, npstate, error;
310
311         sc->pst_dev = dev;
312         sc->pst_parent = device_get_softc(device_get_parent(dev));
313         sc->pst_handle = acpi_get_handle(dev);
314         sc->pst_cpuid = acpi_get_magic(dev);
315
316         /*
317          * If there is a _PSD, then we create procossor domain
318          * accordingly.  If there is no _PSD, we just fake a
319          * default processor domain0.
320          */
321         buf.Pointer = NULL;
322         buf.Length = ACPI_ALLOCATE_BUFFER;
323         status = AcpiEvaluateObject(sc->pst_handle, "_PSD", NULL, &buf);
324         if (!ACPI_FAILURE(status)) {
325                 obj = (ACPI_OBJECT *)buf.Pointer;
326
327                 if (acpi_pst_domain_id > 0) {
328                         device_printf(dev, "Missing _PSD for certain CPUs\n");
329                         AcpiOsFree(obj);
330                         return ENXIO;
331                 }
332                 acpi_pst_domain_id = -1;
333
334                 if (ACPI_PKG_VALID_EQ(obj, 1)) {
335                         dom = acpi_pst_domain_create_pkg(dev,
336                                 &obj->Package.Elements[0]);
337                         if (dom == NULL) {
338                                 AcpiOsFree(obj);
339                                 return ENXIO;
340                         }
341                 } else {
342                         if (obj->Type != ACPI_TYPE_INTEGER) {
343                                 device_printf(dev,
344                                     "Invalid _PSD package, Type 0x%x\n",
345                                     obj->Type);
346                                 AcpiOsFree(obj);
347                                 return ENXIO;
348                         } else {
349                                 device_printf(dev, "Integer _PSD %ju\n",
350                                     (uintmax_t)obj->Integer.Value);
351                                 dom = acpi_pst_domain_create_int(dev,
352                                     obj->Integer.Value);
353                                 if (dom == NULL) {
354                                         AcpiOsFree(obj);
355                                         return ENXIO;
356                                 }
357                         }
358                 }
359
360                 /* Free _PSD */
361                 AcpiOsFree(buf.Pointer);
362         } else {
363                 if (acpi_pst_domain_id < 0) {
364                         device_printf(dev, "Missing _PSD for cpu%d\n",
365                             sc->pst_cpuid);
366                         return ENXIO;
367                 }
368
369                 /*
370                  * Create a stub one processor domain for each processor
371                  */
372                 dom = acpi_pst_domain_alloc(acpi_pst_domain_id,
373                         ACPI_PSD_COORD_SWANY, 1);
374                 dom->pd_flags |= ACPI_PSTDOM_FLAG_STUB;
375
376                 ++acpi_pst_domain_id;
377         }
378
379         /* Make sure that adding us will not overflow our domain */
380         acpi_pst_domain_check_nproc(dev, dom);
381
382         /*
383          * Get control/status registers from _PCT
384          */
385         buf.Pointer = NULL;
386         buf.Length = ACPI_ALLOCATE_BUFFER;
387         status = AcpiEvaluateObject(sc->pst_handle, "_PCT", NULL, &buf);
388         if (ACPI_FAILURE(status)) {
389                 struct acpi_pst_softc *pst;
390
391                 /*
392                  * No _PCT.  See the comment in acpi_pst_probe() near
393                  * _PSD check.
394                  *
395                  * Use control/status registers of another CPU in the
396                  * same domain, or in the same core, if the type of
397                  * these registers are "Fixed Hardware", e.g. on most
398                  * of the model Intel CPUs.
399                  */
400                 pst = LIST_FIRST(&dom->pd_pstlist);
401                 if (pst == NULL) {
402                         cpumask_t mask;
403
404                         mask = get_cpumask_from_level(sc->pst_cpuid,
405                             CORE_LEVEL);
406                         if (CPUMASK_TESTNZERO(mask)) {
407                                 struct acpi_pst_domain *dom1;
408
409                                 LIST_FOREACH(dom1, &acpi_pst_domains, pd_link) {
410                                         LIST_FOREACH(pst, &dom1->pd_pstlist,
411                                             pst_link) {
412                                                 if (CPUMASK_TESTBIT(mask,
413                                                     pst->pst_cpuid))
414                                                         break;
415                                         }
416                                         if (pst != NULL)
417                                                 break;
418                                 }
419                                 if (pst != NULL && acpi_pst_ht_reuse_domain) {
420                                         /*
421                                          * Use the same domain for CPUs in the
422                                          * same core.
423                                          */
424                                         device_printf(dev, "Destroy domain%u, "
425                                             "reuse domain%u\n",
426                                             dom->pd_dom, dom1->pd_dom);
427                                         LIST_REMOVE(dom, pd_link);
428                                         kfree(dom, M_DEVBUF);
429                                         dom = dom1;
430                                         /*
431                                          * Make sure that adding us will not
432                                          * overflow the domain containing
433                                          * siblings in the same core.
434                                          */
435                                         acpi_pst_domain_check_nproc(dev, dom);
436                                 }
437                         }
438                 }
439                 if (pst != NULL &&
440                     pst->pst_creg.pr_res == NULL &&
441                     pst->pst_creg.pr_rid == 0 &&
442                     pst->pst_creg.pr_gas.SpaceId ==
443                     ACPI_ADR_SPACE_FIXED_HARDWARE &&
444                     pst->pst_sreg.pr_res == NULL &&
445                     pst->pst_sreg.pr_rid == 0 &&
446                     pst->pst_sreg.pr_gas.SpaceId ==
447                     ACPI_ADR_SPACE_FIXED_HARDWARE) {
448                         sc->pst_creg = pst->pst_creg;
449                         sc->pst_sreg = pst->pst_sreg;
450                         device_printf(dev,
451                             "No _PCT; reuse %s control/status regs\n",
452                             device_get_nameunit(pst->pst_dev));
453                         goto fetch_pss;
454                 }
455                 device_printf(dev, "Can't get _PCT package - %s\n",
456                               AcpiFormatException(status));
457                 return ENXIO;
458         }
459
460         obj = (ACPI_OBJECT *)buf.Pointer;
461         if (!ACPI_PKG_VALID_EQ(obj, 2)) {
462                 device_printf(dev, "Invalid _PCT package\n");
463                 AcpiOsFree(obj);
464                 return ENXIO;
465         }
466
467         /* Save and try allocating control register */
468         error = acpi_pst_alloc_resource(dev, obj, 0, &sc->pst_creg);
469         if (error) {
470                 AcpiOsFree(obj);
471                 return error;
472         }
473         if (bootverbose) {
474                 device_printf(dev, "control reg %d %jx\n",
475                               sc->pst_creg.pr_gas.SpaceId,
476                               (uintmax_t)sc->pst_creg.pr_gas.Address);
477         }
478
479         /* Save and try allocating status register */
480         error = acpi_pst_alloc_resource(dev, obj, 1, &sc->pst_sreg);
481         if (error) {
482                 AcpiOsFree(obj);
483                 return error;
484         }
485         if (bootverbose) {
486                 device_printf(dev, "status reg %d %jx\n",
487                               sc->pst_sreg.pr_gas.SpaceId,
488                               (uintmax_t)sc->pst_sreg.pr_gas.Address);
489         }
490
491         /* Free _PCT */
492         AcpiOsFree(obj);
493
494 fetch_pss:
495         /*
496          * Create P-State table according to _PSS
497          */
498         buf.Pointer = NULL;
499         buf.Length = ACPI_ALLOCATE_BUFFER;
500         status = AcpiEvaluateObject(sc->pst_handle, "_PSS", NULL, &buf);
501         if (ACPI_FAILURE(status)) {
502                 /*
503                  * No _PSS.  See the comment in acpi_pst_probe() near
504                  * _PSD check.
505                  *
506                  * Assume _PSS are same across all CPUs; well, they
507                  * should/have to be so.
508                  */
509                 if (acpi_npstates > 0 && acpi_pstates != NULL) {
510                         device_printf(dev, "No _PSS\n");
511                         goto fetch_ppc;
512                 }
513                 device_printf(dev, "Can't get _PSS package - %s\n",
514                               AcpiFormatException(status));
515                 return ENXIO;
516         }
517
518         obj = (ACPI_OBJECT *)buf.Pointer;
519         if (!ACPI_PKG_VALID(obj, 1)) {
520                 device_printf(dev, "Invalid _PSS package\n");
521                 AcpiOsFree(obj);
522                 return ENXIO;
523         }
524
525         /* Don't create too many P-States */
526         npstate = obj->Package.Count;
527         if (npstate > ACPI_NPSTATE_MAX) {
528                 device_printf(dev, "Too many P-States, %d->%d\n",
529                               npstate, ACPI_NPSTATE_MAX);
530                 npstate = ACPI_NPSTATE_MAX;
531         }
532
533         /*
534          * If we have already created P-State table,
535          * we must make sure that number of entries
536          * is consistent.
537          */
538         if (acpi_pstates != NULL && acpi_npstates != npstate) {
539                 device_printf(dev, "Inconsistent # of P-States "
540                               "cross Processor objects\n");
541                 AcpiOsFree(obj);
542                 return ENXIO;
543         }
544
545         /*
546          * Create a temporary P-State table
547          */
548         pstate = kmalloc(sizeof(*pstate) * npstate, M_TEMP, M_WAITOK);
549         for (i = 0, p = pstate; i < npstate; ++i, ++p) {
550                 ACPI_OBJECT *pkg;
551                 uint32_t *ptr[ACPI_PSS_PX_NENTRY] = {
552                         &p->st_freq, &p->st_power, &p->st_xsit_lat,
553                         &p->st_bm_lat, &p->st_cval, &p->st_sval
554                 };
555                 int j;
556
557                 pkg = &obj->Package.Elements[i];
558                 if (!ACPI_PKG_VALID(pkg, ACPI_PSS_PX_NENTRY)) {
559                         device_printf(dev, "Invalud _PSS P%d\n", i);
560                         AcpiOsFree(obj);
561                         kfree(pstate, M_TEMP);
562                         return ENXIO;
563                 }
564                 for (j = 0; j < ACPI_PSS_PX_NENTRY; ++j) {
565                         if (acpi_PkgInt32(pkg, j, ptr[j]) != 0) {
566                                 device_printf(dev, "Can't extract "
567                                               "_PSS P%d %dth entry\n", i, j);
568                                 AcpiOsFree(obj);
569                                 kfree(pstate, M_TEMP);
570                                 return ENXIO;
571                         }
572                 }
573         }
574
575         /* Free _PSS */
576         AcpiOsFree(obj);
577
578         if (acpi_pstates == NULL) {
579                 /*
580                  * If no P-State table is created yet,
581                  * save the temporary one we just created.
582                  */
583                 acpi_pstates = pstate;
584                 acpi_npstates = npstate;
585                 pstate = NULL;
586
587                 if (bootverbose) {
588                         for (i = 0; i < acpi_npstates; ++i) {
589                                 device_printf(dev,
590                                 "freq %u, pwr %u, xlat %u, blat %u, "
591                                 "cv %08x, sv %08x\n",
592                                 acpi_pstates[i].st_freq,
593                                 acpi_pstates[i].st_power,
594                                 acpi_pstates[i].st_xsit_lat,
595                                 acpi_pstates[i].st_bm_lat,
596                                 acpi_pstates[i].st_cval,
597                                 acpi_pstates[i].st_sval);
598                         }
599                 }
600         } else {
601                 /*
602                  * Make sure that P-State tables are same
603                  * for all processors.
604                  */
605                 if (memcmp(pstate, acpi_pstates,
606                            sizeof(*pstate) * npstate) != 0) {
607                         device_printf(dev, "Inconsistent _PSS "
608                                       "cross Processor objects\n");
609                         kfree(pstate, M_TEMP);
610                         return ENXIO;
611                 }
612                 kfree(pstate, M_TEMP);
613         }
614
615 fetch_ppc:
616         /* By default, we start from P-State table's first entry */
617         sc->pst_sstart = 0;
618
619         /*
620          * Adjust the usable first entry of P-State table,
621          * if there is _PPC object.
622          */
623         buf.Pointer = NULL;
624         buf.Length = ACPI_ALLOCATE_BUFFER;
625         status = AcpiEvaluateObject(sc->pst_handle, "_PPC", NULL, &buf);
626         if (!ACPI_FAILURE(status)) {
627                 obj = (ACPI_OBJECT *)buf.Pointer;
628                 if (obj->Type == ACPI_TYPE_INTEGER) {
629                         if (obj->Integer.Value >= acpi_npstates) {
630                                 device_printf(dev, "Invalid _PPC value\n");
631                                 AcpiOsFree(obj);
632                                 return ENXIO;
633                         }
634                         sc->pst_sstart = obj->Integer.Value;
635                         if (bootverbose)
636                                 device_printf(dev, "_PPC %d\n", sc->pst_sstart);
637
638                         /* TODO: Install notifiy handler */
639                 } else {
640                         device_printf(dev, "Invalid _PPC object\n");
641                         AcpiOsFree(obj);
642                         return ENXIO;
643                 }
644
645                 /* Free _PPC */
646                 AcpiOsFree(obj);
647         }
648
649         sc->pst_state = sc->pst_sstart;
650
651         /*
652          * Some CPUs only have package P-states, but some BIOSes put each
653          * hyperthread to its own P-state domain; allow user to override.
654          */
655         if (LIST_EMPTY(&dom->pd_pstlist) && acpi_pst_force_pkg_domain) {
656                 cpumask_t mask;
657
658                 mask = get_cpumask_from_level(sc->pst_cpuid, CHIP_LEVEL);
659                 if (CPUMASK_TESTNZERO(mask)) {
660                         struct acpi_pst_softc *pst = NULL;
661                         struct acpi_pst_domain *dom1;
662
663                         LIST_FOREACH(dom1, &acpi_pst_domains, pd_link) {
664                                 LIST_FOREACH(pst, &dom1->pd_pstlist,
665                                     pst_link) {
666                                         if (CPUMASK_TESTBIT(mask,
667                                             pst->pst_cpuid))
668                                                 break;
669                                 }
670                                 if (pst != NULL)
671                                         break;
672                         }
673                         if (pst != NULL &&
674                             memcmp(&pst->pst_creg, &sc->pst_creg,
675                                 sizeof(sc->pst_creg)) == 0 &&
676                             memcmp(&pst->pst_sreg, &sc->pst_sreg,
677                                 sizeof(sc->pst_sreg)) == 0) {
678                                 /*
679                                  * Use the same domain for CPUs in the
680                                  * same package.
681                                  */
682                                 device_printf(dev, "Destroy domain%u, "
683                                     "force pkg domain%u\n",
684                                     dom->pd_dom, dom1->pd_dom);
685                                 LIST_REMOVE(dom, pd_link);
686                                 kfree(dom, M_DEVBUF);
687                                 dom = dom1;
688                                 /*
689                                  * Make sure that adding us will not
690                                  * overflow the domain containing
691                                  * siblings in the same package.
692                                  */
693                                 acpi_pst_domain_check_nproc(dev, dom);
694                         }
695                 }
696         }
697
698         /* Link us with the domain */
699         sc->pst_domain = dom;
700         LIST_INSERT_HEAD(&dom->pd_pstlist, sc, pst_link);
701
702         if (device_get_unit(dev) == 0)
703                 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_pst_postattach, NULL);
704
705         return 0;
706 }
707
708 static struct acpi_pst_domain *
709 acpi_pst_domain_create_pkg(device_t dev, ACPI_OBJECT *obj)
710 {
711         struct acpi_pst_domain *dom;
712         uint32_t val, domain, coord, nproc;
713
714         if (!ACPI_PKG_VALID_EQ(obj, 5)) {
715                 device_printf(dev, "Invalid _PSD package\n");
716                 return NULL;
717         }
718
719         /* NumberOfEntries */
720         if (acpi_PkgInt32(obj, 0, &val) != 0 || val != 5) {
721                 device_printf(dev, "Invalid _PSD NumberOfEntries\n");
722                 return NULL;
723         }
724
725         /* Revision */
726         if (acpi_PkgInt32(obj, 1, &val) != 0 || val != 0) {
727                 device_printf(dev, "Invalid _PSD Revision\n");
728                 return NULL;
729         }
730
731         if (acpi_PkgInt32(obj, 2, &domain) != 0 ||
732             acpi_PkgInt32(obj, 3, &coord) != 0 ||
733             acpi_PkgInt32(obj, 4, &nproc) != 0) {
734                 device_printf(dev, "Can't extract _PSD package\n");
735                 return NULL;
736         }
737
738         if (!ACPI_PSD_COORD_VALID(coord)) {
739                 device_printf(dev, "Invalid _PSD CoordType (%#x)\n", coord);
740                 return NULL;
741         }
742
743         if (nproc > MAXCPU) {
744                 /*
745                  * If NumProcessors is greater than MAXCPU
746                  * and domain's coordination is SWALL, then
747                  * we will never be able to start all CPUs
748                  * within this domain, and power state
749                  * transition will never be completed, so we
750                  * just bail out here.
751                  */
752                 if (coord == ACPI_PSD_COORD_SWALL) {
753                         device_printf(dev, "Unsupported _PSD NumProcessors "
754                                       "(%d)\n", nproc);
755                         return NULL;
756                 }
757         } else if (nproc == 0) {
758                 device_printf(dev, "_PSD NumProcessors are zero\n");
759                 return NULL;
760         }
761
762         dom = acpi_pst_domain_find(domain);
763         if (dom != NULL) {
764                 if (dom->pd_flags & ACPI_PSTDOM_FLAG_INT) {
765                         device_printf(dev, "Mixed Integer _PSD and "
766                             "Package _PSD\n");
767                         return NULL;
768                 }
769                 if (dom->pd_coord != coord) {
770                         device_printf(dev, "Inconsistent _PSD coord "
771                             "information cross Processor objects\n");
772                         return NULL;
773                 }
774                 if (dom->pd_nproc != nproc) {
775                         device_printf(dev, "Inconsistent _PSD nproc "
776                             "information cross Processor objects\n");
777                         /*
778                          * Some stupid BIOSes will set wrong "# of processors",
779                          * e.g. 1 for CPU w/ hyperthreading; Be lenient here.
780                          */
781                 }
782                 return dom;
783         }
784
785         dom = acpi_pst_domain_alloc(domain, coord, nproc);
786         if (bootverbose) {
787                 device_printf(dev, "create pkg domain%u, coord %#x\n",
788                     dom->pd_dom, dom->pd_coord);
789         }
790
791         return dom;
792 }
793
794 static struct acpi_pst_domain *
795 acpi_pst_domain_create_int(device_t dev, uint32_t domain)
796 {
797         struct acpi_pst_domain *dom;
798
799         dom = acpi_pst_domain_find(domain);
800         if (dom != NULL) {
801                 if ((dom->pd_flags & ACPI_PSTDOM_FLAG_INT) == 0) {
802                         device_printf(dev, "Mixed Package _PSD and "
803                             "Integer _PSD\n");
804                         return NULL;
805                 }
806                 KKASSERT(dom->pd_coord == ACPI_PSD_COORD_SWALL);
807
808                 dom->pd_nproc++;
809                 return dom;
810         }
811
812         dom = acpi_pst_domain_alloc(domain, ACPI_PSD_COORD_SWALL, 1);
813         dom->pd_flags |= ACPI_PSTDOM_FLAG_INT;
814
815         if (bootverbose)
816                 device_printf(dev, "create int domain%u\n", dom->pd_dom);
817
818         return dom;
819 }
820
821 static struct acpi_pst_domain *
822 acpi_pst_domain_find(uint32_t domain)
823 {
824         struct acpi_pst_domain *dom;
825
826         LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
827                 if (dom->pd_flags & ACPI_PSTDOM_FLAG_STUB)
828                         continue;
829                 if (dom->pd_dom == domain)
830                         return dom;
831         }
832         return NULL;
833 }
834
835 static struct acpi_pst_domain *
836 acpi_pst_domain_alloc(uint32_t domain, uint32_t coord, uint32_t nproc)
837 {
838         struct acpi_pst_domain *dom;
839
840         dom = kmalloc(sizeof(*dom), M_DEVBUF, M_WAITOK | M_ZERO);
841         dom->pd_dom = domain;
842         dom->pd_coord = coord;
843         dom->pd_nproc = nproc;
844         dom->pd_state = 0; /* XXX */
845         dom->pd_sstart = 0; /* XXX */
846         LIST_INIT(&dom->pd_pstlist);
847
848         LIST_INSERT_HEAD(&acpi_pst_domains, dom, pd_link);
849
850         return dom;
851 }
852
853 static int
854 acpi_pst_domain_set_pstate(struct acpi_pst_domain *dom, int i)
855 {
856         const struct acpi_pstate *pstate;
857         struct acpi_pst_softc *sc;
858         int done, error;
859
860         KKASSERT(i >= 0 && i < acpi_npstates);
861         pstate = &acpi_pstates[i];
862
863         done = 0;
864         LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
865                 if (!done) {
866                         error = acpi_pst_set_pstate(sc, pstate);
867                         if (error) {
868                                 device_printf(sc->pst_dev, "can't set "
869                                               "freq %d\n", pstate->st_freq);
870                                 /* XXX error cleanup? */
871                         }
872                         if (dom->pd_coord == ACPI_PSD_COORD_SWANY)
873                                 done = 1;
874                 }
875                 sc->pst_state = i;
876         }
877         dom->pd_state = i;
878
879         return 0;
880 }
881
882 static int
883 acpi_pst_global_set_pstate(int i)
884 {
885         struct acpi_pst_domain *dom;
886
887         LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
888                 /* Skip dead domain */
889                 if (dom->pd_flags & ACPI_PSTDOM_FLAG_DEAD)
890                         continue;
891                 acpi_pst_domain_set_pstate(dom, i);
892         }
893         acpi_pst_global_state = i;
894
895         return 0;
896 }
897
898 static void
899 acpi_pst_postattach(void *arg __unused)
900 {
901         struct acpi_pst_domain *dom;
902         struct acpi_cpu_softc *cpu;
903         device_t *devices;
904         int i, ndevices, error, has_domain, sstate;
905
906         devices = NULL;
907         ndevices = 0;
908         error = devclass_get_devices(acpi_pst_devclass, &devices, &ndevices);
909         if (error)
910                 return;
911
912         if (ndevices == 0)
913                 return;
914
915         cpu = NULL;
916         for (i = 0; i < ndevices; ++i) {
917                 cpu = device_get_softc(device_get_parent(devices[i]));
918                 if (cpu->glob_sysctl_tree != NULL)
919                         break;
920         }
921         kfree(devices, M_TEMP);
922         KKASSERT(cpu != NULL);
923
924         if (acpi_pst_md == NULL)
925                 kprintf("ACPI: no P-State CPU driver\n");
926
927         sstate = 0x7fffffff;
928         has_domain = 0;
929         LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
930                 struct acpi_pst_softc *sc;
931                 char buf[32];
932
933                 /*
934                  * Make sure that all processors belonging to this
935                  * domain are located.
936                  */
937                 i = 0;
938                 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link)
939                         ++i;
940                 if (i != dom->pd_nproc) {
941                         KKASSERT(i < dom->pd_nproc);
942
943                         kprintf("ACPI: domain%u misses processors, "
944                                 "should be %d, got %d\n", dom->pd_dom,
945                                 dom->pd_nproc, i);
946                         if (dom->pd_coord == ACPI_PSD_COORD_SWALL) {
947                                 /*
948                                  * If this domain's coordination is
949                                  * SWALL and we don't see all of the
950                                  * member CPUs of this domain, then
951                                  * the P-State transition will never
952                                  * be completed, so just leave this
953                                  * domain out.
954                                  */
955                                 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
956                                 continue;
957                         }
958                         dom->pd_nproc = i;
959                 }
960
961                 /*
962                  * Validate P-State configurations for this domain
963                  */
964                 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
965                         error = acpi_pst_check_csr(sc);
966                         if (error)
967                                 break;
968
969                         error = acpi_pst_check_pstates(sc);
970                         if (error)
971                                 break;
972                 }
973                 if (sc != NULL) {
974                         kprintf("ACPI: domain%u P-State configuration "
975                                 "check failed\n", dom->pd_dom);
976                         dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
977                         continue;
978                 }
979
980                 /*
981                  * Do necssary P-State initialization
982                  */
983                 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
984                         error = acpi_pst_init(sc);
985                         if (error)
986                                 break;
987                 }
988                 if (sc != NULL) {
989                         kprintf("ACPI: domain%u P-State initialization "
990                                 "check failed\n", dom->pd_dom);
991                         dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
992                         continue;
993                 }
994
995                 has_domain = 1;
996
997                 ksnprintf(buf, sizeof(buf), "px_dom%u", dom->pd_dom);
998
999                 sysctl_ctx_init(&dom->pd_sysctl_ctx);
1000                 dom->pd_sysctl_tree =
1001                 SYSCTL_ADD_NODE(&dom->pd_sysctl_ctx,
1002                         SYSCTL_CHILDREN(cpu->glob_sysctl_tree),
1003                         OID_AUTO, buf, CTLFLAG_RD, 0,
1004                         "P-State domain");
1005                 if (dom->pd_sysctl_tree == NULL) {
1006                         kprintf("ACPI: Can't create sysctl tree for domain%u",
1007                                 dom->pd_dom);
1008                         continue;
1009                 }
1010
1011                 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
1012                                 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
1013                                 OID_AUTO, "available",
1014                                 CTLTYPE_STRING | CTLFLAG_RD,
1015                                 dom, 0, acpi_pst_sysctl_freqs, "A",
1016                                 "available frequencies");
1017
1018                 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
1019                                 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
1020                                 OID_AUTO, "members",
1021                                 CTLTYPE_STRING | CTLFLAG_RD,
1022                                 dom, 0, acpi_pst_sysctl_members, "A",
1023                                 "member cpus");
1024
1025                 if (acpi_pst_md != NULL &&
1026                     acpi_pst_md->pmd_set_pstate != NULL) {
1027                         SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
1028                                         SYSCTL_CHILDREN(dom->pd_sysctl_tree),
1029                                         OID_AUTO, "select",
1030                                         CTLTYPE_UINT | CTLFLAG_RW,
1031                                         dom, 0, acpi_pst_sysctl_select,
1032                                         "IU", "select freq");
1033                 }
1034
1035                 if (dom->pd_state < sstate)
1036                         sstate = dom->pd_state;
1037         }
1038
1039         if (has_domain && acpi_pst_md != NULL &&
1040             acpi_pst_md->pmd_set_pstate != NULL) {
1041                 SYSCTL_ADD_PROC(&cpu->glob_sysctl_ctx,
1042                                 SYSCTL_CHILDREN(cpu->glob_sysctl_tree),
1043                                 OID_AUTO, "px_global",
1044                                 CTLTYPE_UINT | CTLFLAG_RW,
1045                                 NULL, 0, acpi_pst_sysctl_global,
1046                                 "IU", "select freq for all domains");
1047
1048                 acpi_pst_global_set_pstate(sstate);
1049         }
1050 }
1051
1052 static int
1053 acpi_pst_sysctl_freqs(SYSCTL_HANDLER_ARGS)
1054 {
1055         struct acpi_pst_domain *dom = arg1;
1056         int i, error;
1057
1058         error = 0;
1059         for (i = 0; i < acpi_npstates; ++i) {
1060                 if (error == 0 && i)
1061                         error = SYSCTL_OUT(req, " ", 1);
1062                 if (error == 0) {
1063                         const char *pat;
1064                         char buf[32];
1065
1066                         if (i < dom->pd_sstart)
1067                                 pat = "(%u)";
1068                         else
1069                                 pat = "%u";
1070
1071                         ksnprintf(buf, sizeof(buf), pat,
1072                                   acpi_pstates[i].st_freq);
1073                         error = SYSCTL_OUT(req, buf, strlen(buf));
1074                 }
1075         }
1076         return error;
1077 }
1078
1079 static int
1080 acpi_pst_sysctl_members(SYSCTL_HANDLER_ARGS)
1081 {
1082         struct acpi_pst_domain *dom = arg1;
1083         struct acpi_pst_softc *sc;
1084         int loop, error;
1085
1086         loop = error = 0;
1087         LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
1088                 char buf[32];
1089
1090                 if (error == 0 && loop)
1091                         error = SYSCTL_OUT(req, " ", 1);
1092                 if (error == 0) {
1093                         ksnprintf(buf, sizeof(buf), "cpu%d", sc->pst_cpuid);
1094                         error = SYSCTL_OUT(req, buf, strlen(buf));
1095                 }
1096
1097                 if (error == 0 && acpi_pst_md && acpi_pst_md->pmd_get_pstate) {
1098                         const struct acpi_pstate *pstate;
1099                         const char *str;
1100
1101                         pstate = acpi_pst_get_pstate(sc);
1102                         if (pstate == NULL) {
1103                                 str = "(*)";
1104                         } else {
1105                                 ksnprintf(buf, sizeof(buf), "(%d)",
1106                                           pstate->st_freq);
1107                                 str = buf;
1108                         }
1109                         error = SYSCTL_OUT(req, str, strlen(str));
1110                 }
1111                 ++loop;
1112         }
1113         return error;
1114 }
1115
1116 static int
1117 acpi_pst_sysctl_select(SYSCTL_HANDLER_ARGS)
1118 {
1119         struct acpi_pst_domain *dom = arg1;
1120         int error, i, freq;
1121
1122         KKASSERT(dom->pd_state >= 0 && dom->pd_state < acpi_npstates);
1123
1124         freq = acpi_pstates[dom->pd_state].st_freq;
1125
1126         error = sysctl_handle_int(oidp, &freq, 0, req);
1127         if (error || req->newptr == NULL)
1128                 return error;
1129
1130         i = acpi_pst_freq2index(freq);
1131         if (i < 0)
1132                 return EINVAL;
1133
1134         acpi_pst_domain_set_pstate(dom, i);
1135         return 0;
1136 }
1137
1138 static int
1139 acpi_pst_sysctl_global(SYSCTL_HANDLER_ARGS)
1140 {
1141         int error, i, freq;
1142
1143         KKASSERT(acpi_pst_global_state >= 0 &&
1144                  acpi_pst_global_state < acpi_npstates);
1145
1146         freq = acpi_pstates[acpi_pst_global_state].st_freq;
1147
1148         error = sysctl_handle_int(oidp, &freq, 0, req);
1149         if (error || req->newptr == NULL)
1150                 return error;
1151
1152         i = acpi_pst_freq2index(freq);
1153         if (i < 0)
1154                 return EINVAL;
1155
1156         acpi_pst_global_set_pstate(i);
1157
1158         return 0;
1159 }
1160
1161 static void
1162 acpi_pst_check_csr_handler(netmsg_t msg)
1163 {
1164         struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1165         int error;
1166
1167         error = acpi_pst_md->pmd_check_csr(rmsg->ctrl, rmsg->status);
1168         lwkt_replymsg(&rmsg->base.lmsg, error);
1169 }
1170
1171 static int
1172 acpi_pst_check_csr(struct acpi_pst_softc *sc)
1173 {
1174         struct netmsg_acpi_pst msg;
1175
1176         if (acpi_pst_md == NULL)
1177                 return 0;
1178
1179         netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1180                     MSGF_PRIORITY, acpi_pst_check_csr_handler);
1181         msg.ctrl = &sc->pst_creg;
1182         msg.status = &sc->pst_sreg;
1183
1184         return lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.base.lmsg, 0);
1185 }
1186
1187 static void
1188 acpi_pst_check_pstates_handler(netmsg_t msg)
1189 {
1190         int error;
1191
1192         error = acpi_pst_md->pmd_check_pstates(acpi_pstates, acpi_npstates);
1193         lwkt_replymsg(&msg->lmsg, error);
1194 }
1195
1196 static int
1197 acpi_pst_check_pstates(struct acpi_pst_softc *sc)
1198 {
1199         struct netmsg_base msg;
1200
1201         if (acpi_pst_md == NULL)
1202                 return 0;
1203
1204         netmsg_init(&msg, NULL, &curthread->td_msgport,
1205                     MSGF_PRIORITY, acpi_pst_check_pstates_handler);
1206
1207         return lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.lmsg, 0);
1208 }
1209
1210 static void
1211 acpi_pst_init_handler(netmsg_t msg)
1212 {
1213         struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1214         int error;
1215
1216         error = acpi_pst_md->pmd_init(rmsg->ctrl, rmsg->status);
1217         lwkt_replymsg(&rmsg->base.lmsg, error);
1218 }
1219
1220 static int
1221 acpi_pst_init(struct acpi_pst_softc *sc)
1222 {
1223         struct netmsg_acpi_pst msg;
1224
1225         if (acpi_pst_md == NULL)
1226                 return 0;
1227
1228         netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1229                     MSGF_PRIORITY, acpi_pst_init_handler);
1230         msg.ctrl = &sc->pst_creg;
1231         msg.status = &sc->pst_sreg;
1232
1233         return lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.base.lmsg, 0);
1234 }
1235
1236 static void
1237 acpi_pst_set_pstate_handler(netmsg_t msg)
1238 {
1239         struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1240         int error;
1241
1242         error = acpi_pst_md->pmd_set_pstate(rmsg->ctrl, rmsg->status,
1243                                             rmsg->base.lmsg.u.ms_resultp);
1244         lwkt_replymsg(&rmsg->base.lmsg, error);
1245 }
1246
1247 static int
1248 acpi_pst_set_pstate(struct acpi_pst_softc *sc, const struct acpi_pstate *pstate)
1249 {
1250         struct netmsg_acpi_pst msg;
1251
1252         KKASSERT(acpi_pst_md != NULL);
1253
1254         netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1255                     MSGF_PRIORITY, acpi_pst_set_pstate_handler);
1256         msg.base.lmsg.u.ms_resultp = __DECONST(void *, pstate);
1257         msg.ctrl = &sc->pst_creg;
1258         msg.status = &sc->pst_sreg;
1259
1260         return lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.base.lmsg, 0);
1261 }
1262
1263 static void
1264 acpi_pst_get_pstate_handler(netmsg_t msg)
1265 {
1266         struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1267         const struct acpi_pstate *pstate;
1268
1269         pstate = acpi_pst_md->pmd_get_pstate(rmsg->status, acpi_pstates,
1270                                              acpi_npstates);
1271         rmsg->base.lmsg.u.ms_resultp = __DECONST(void *, pstate);
1272         lwkt_replymsg(&rmsg->base.lmsg, 0);
1273 }
1274
1275 static const struct acpi_pstate *
1276 acpi_pst_get_pstate(struct acpi_pst_softc *sc)
1277 {
1278         struct netmsg_acpi_pst msg;
1279
1280         if (acpi_pst_md == NULL)
1281                 return 0;
1282
1283         netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1284                     MSGF_PRIORITY, acpi_pst_get_pstate_handler);
1285         msg.status = &sc->pst_sreg;
1286
1287         lwkt_domsg(netisr_cpuport(sc->pst_cpuid), &msg.base.lmsg, 0);
1288         return msg.base.lmsg.u.ms_resultp;
1289 }
1290
1291 static int
1292 acpi_pst_alloc_resource(device_t dev, ACPI_OBJECT *obj, int idx,
1293                         struct acpi_pst_res *res)
1294 {
1295         struct acpi_pst_softc *sc = device_get_softc(dev);
1296         int error, type;
1297
1298         /* Save GAS */
1299         error = acpi_PkgRawGas(obj, idx, &res->pr_gas);
1300         if (error)
1301                 return error;
1302
1303         /* Allocate resource, if possible */
1304         res->pr_rid = sc->pst_parent->cpu_next_rid;
1305         acpi_bus_alloc_gas(dev, &type, &res->pr_rid, &res->pr_gas, &res->pr_res, 0);
1306         if (res->pr_res != NULL) {
1307                 sc->pst_parent->cpu_next_rid++;
1308                 res->pr_bt = rman_get_bustag(res->pr_res);
1309                 res->pr_bh = rman_get_bushandle(res->pr_res);
1310         } else {
1311                 res->pr_rid = 0;
1312         }
1313         return 0;
1314 }
1315
1316 static void
1317 acpi_pst_domain_check_nproc(device_t dev, struct acpi_pst_domain *dom)
1318 {
1319         struct acpi_pst_softc *pst;
1320         int i;
1321
1322         i = 0;
1323         LIST_FOREACH(pst, &dom->pd_pstlist, pst_link)
1324                 ++i;
1325         if (i == dom->pd_nproc) {
1326                 /*
1327                  * Some stupid BIOSes will set wrong "# of processors",
1328                  * e.g. 1 for CPU w/ hyperthreading; Be lenient here.
1329                  */
1330                 if (bootverbose) {
1331                         device_printf(dev, "domain%u already contains %d "
1332                             "P-States\n", dom->pd_dom, dom->pd_nproc);
1333                 }
1334                 dom->pd_nproc++;
1335         }
1336         KKASSERT(i < dom->pd_nproc);
1337 }