acpi/pstate: Support Integer _PSD
[dragonfly.git] / sys / dev / acpica5 / 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
46 #include <net/netisr.h>
47 #include <net/netmsg2.h>
48 #include <net/if_var.h>
49
50 #include "acpi.h"
51 #include "acpivar.h"
52 #include "acpi_cpu.h"
53 #include "acpi_cpu_pstate.h"
54
55 #define ACPI_NPSTATE_MAX        16
56
57 #define ACPI_PSS_PX_NENTRY      6
58
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)
66
67 struct acpi_pst_softc;
68 LIST_HEAD(acpi_pst_list, acpi_pst_softc);
69
70 struct netmsg_acpi_pst {
71         struct netmsg_base base;
72         const struct acpi_pst_res *ctrl;
73         const struct acpi_pst_res *status;
74 };
75
76 struct acpi_pst_domain {
77         uint32_t                pd_dom;
78         uint32_t                pd_coord;
79         uint32_t                pd_nproc;
80         LIST_ENTRY(acpi_pst_domain) pd_link;
81
82         uint32_t                pd_flags;
83
84         int                     pd_state;
85         int                     pd_sstart;
86         struct acpi_pst_list    pd_pstlist;
87
88         struct sysctl_ctx_list  pd_sysctl_ctx;
89         struct sysctl_oid       *pd_sysctl_tree;
90 };
91 LIST_HEAD(acpi_pst_domlist, acpi_pst_domain);
92
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 */
96
97 struct acpi_pst_softc {
98         device_t                pst_dev;
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;
103
104         int                     pst_state;
105         int                     pst_sstart;
106         int                     pst_cpuid;
107
108         ACPI_HANDLE             pst_handle;
109
110         LIST_ENTRY(acpi_pst_softc) pst_link;
111 };
112
113 static int      acpi_pst_probe(device_t dev);
114 static int      acpi_pst_attach(device_t dev);
115
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);
127
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 *);
137
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);
143
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);
148
149 static struct acpi_pst_domlist  acpi_pst_domains =
150         LIST_HEAD_INITIALIZER(acpi_pst_domains);
151 static int                      acpi_pst_domain_id;
152
153 static int                      acpi_pst_global_state;
154
155 static int                      acpi_npstates;
156 static struct acpi_pstate       *acpi_pstates;
157
158 static const struct acpi_pst_md *acpi_pst_md;
159
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),
168
169         /* Bus interface */
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),
184
185         { 0, 0 }
186 };
187
188 static driver_t acpi_pst_driver = {
189         "cpu_pst",
190         acpi_pst_methods,
191         sizeof(struct acpi_pst_softc)
192 };
193
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);
197
198 static __inline int
199 acpi_pst_freq2index(int freq)
200 {
201         int i;
202
203         for (i = 0; i < acpi_npstates; ++i) {
204                 if (acpi_pstates[i].st_freq == freq)
205                         return i;
206         }
207         return -1;
208 }
209
210 static int
211 acpi_pst_probe(device_t dev)
212 {
213         ACPI_BUFFER buf;
214         ACPI_HANDLE handle;
215         ACPI_STATUS status;
216         ACPI_OBJECT *obj;
217
218         if (acpi_disabled("cpu_pst") ||
219             acpi_get_type(dev) != ACPI_TYPE_PROCESSOR)
220                 return ENXIO;
221
222         if (acpi_pst_md == NULL)
223                 acpi_pst_md = acpi_pst_md_probe();
224
225         handle = acpi_get_handle(dev);
226
227         /*
228          * Check _PCT package
229          */
230         buf.Pointer = NULL;
231         buf.Length = ACPI_ALLOCATE_BUFFER;
232         status = AcpiEvaluateObject(handle, "_PCT", NULL, &buf);
233         if (ACPI_FAILURE(status)) {
234                 if (bootverbose) {
235                         device_printf(dev, "Can't get _PCT package - %s\n",
236                                       AcpiFormatException(status));
237                 }
238                 return ENXIO;
239         }
240
241         obj = (ACPI_OBJECT *)buf.Pointer;
242         if (!ACPI_PKG_VALID_EQ(obj, 2)) {
243                 device_printf(dev, "Invalid _PCT package\n");
244                 AcpiOsFree(obj);
245                 return ENXIO;
246         }
247         AcpiOsFree(obj);
248
249         /*
250          * Check _PSS package
251          */
252         buf.Pointer = NULL;
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));
258                 return ENXIO;
259         }
260
261         obj = (ACPI_OBJECT *)buf.Pointer;
262         if (!ACPI_PKG_VALID(obj, 1)) {
263                 device_printf(dev, "Invalid _PSS package\n");
264                 AcpiOsFree(obj);
265                 return ENXIO;
266         }
267         AcpiOsFree(obj);
268
269         device_set_desc(dev, "ACPI CPU P-State");
270         return 0;
271 }
272
273 static int
274 acpi_pst_attach(device_t dev)
275 {
276         struct acpi_pst_softc *sc = device_get_softc(dev), *pst;
277         struct acpi_pst_domain *dom = NULL;
278         ACPI_BUFFER buf;
279         ACPI_STATUS status;
280         ACPI_OBJECT *obj;
281         struct acpi_pstate *pstate, *p;
282         int i, npstate, error;
283
284         sc->pst_dev = dev;
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);
288
289         /*
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.
293          */
294         buf.Pointer = NULL;
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]);
302                         if (dom == NULL) {
303                                 AcpiOsFree(obj);
304                                 return ENXIO;
305                         }
306                 } else {
307                         if (obj->Type != ACPI_TYPE_INTEGER) {
308                                 device_printf(dev,
309                                     "Invalid _PSD package, Type 0x%x\n",
310                                     obj->Type);
311                                 AcpiOsFree(obj);
312                                 return ENXIO;
313                         } else {
314                                 device_printf(dev, "Integer _PSD %ju\n",
315                                     (uintmax_t)obj->Integer.Value);
316                                 dom = acpi_pst_domain_create_int(dev,
317                                     obj->Integer.Value);
318                                 if (dom == NULL) {
319                                         AcpiOsFree(obj);
320                                         return ENXIO;
321                                 }
322                         }
323                 }
324
325                 /* Free _PSD */
326                 AcpiOsFree(buf.Pointer);
327         } else {
328                 /*
329                  * Create a stub one processor domain for each processor
330                  */
331                 dom = acpi_pst_domain_alloc(acpi_pst_domain_id,
332                         ACPI_PSD_COORD_SWANY, 1);
333                 dom->pd_flags |= ACPI_PSTDOM_FLAG_STUB;
334
335                 ++acpi_pst_domain_id;
336         }
337
338         /* Make sure that adding us will not overflow our domain */
339         i = 0;
340         LIST_FOREACH(pst, &dom->pd_pstlist, pst_link)
341                 ++i;
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);
346                 return ENXIO;
347         }
348         KKASSERT(i < dom->pd_nproc);
349
350         /*
351          * Get control/status registers from _PCT
352          */
353         buf.Pointer = NULL;
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));
359                 return ENXIO;
360         }
361
362         obj = (ACPI_OBJECT *)buf.Pointer;
363         if (!ACPI_PKG_VALID_EQ(obj, 2)) {
364                 device_printf(dev, "Invalid _PCT package\n");
365                 AcpiOsFree(obj);
366                 return ENXIO;
367         }
368
369         /* Save and try allocating control register */
370         error = acpi_pst_alloc_resource(dev, obj, 0, &sc->pst_creg);
371         if (error) {
372                 AcpiOsFree(obj);
373                 return error;
374         }
375         if (bootverbose) {
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);
379         }
380
381         /* Save and try allocating status register */
382         error = acpi_pst_alloc_resource(dev, obj, 1, &sc->pst_sreg);
383         if (error) {
384                 AcpiOsFree(obj);
385                 return error;
386         }
387         if (bootverbose) {
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);
391         }
392
393         /* Free _PCT */
394         AcpiOsFree(obj);
395
396         /*
397          * Create P-State table according to _PSS
398          */
399         buf.Pointer = NULL;
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));
405                 return ENXIO;
406         }
407
408         obj = (ACPI_OBJECT *)buf.Pointer;
409         if (!ACPI_PKG_VALID(obj, 1)) {
410                 device_printf(dev, "Invalid _PSS package\n");
411                 AcpiOsFree(obj);
412                 return ENXIO;
413         }
414
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;
421         }
422
423         /*
424          * If we have already created P-State table,
425          * we must make sure that number of entries
426          * is consistent.
427          */
428         if (acpi_pstates != NULL && acpi_npstates != npstate) {
429                 device_printf(dev, "Inconsistent # of P-States "
430                               "cross Processor objects\n");
431                 AcpiOsFree(obj);
432                 return ENXIO;
433         }
434
435         /*
436          * Create a temporary P-State table
437          */
438         pstate = kmalloc(sizeof(*pstate) * npstate, M_TEMP, M_WAITOK);
439         for (i = 0, p = pstate; i < npstate; ++i, ++p) {
440                 ACPI_OBJECT *pkg;
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
444                 };
445                 int j;
446
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);
450                         AcpiOsFree(obj);
451                         kfree(pstate, M_TEMP);
452                         return ENXIO;
453                 }
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);
458                                 AcpiOsFree(obj);
459                                 kfree(pstate, M_TEMP);
460                                 return ENXIO;
461                         }
462                 }
463         }
464
465         /* Free _PSS */
466         AcpiOsFree(obj);
467
468         if (acpi_pstates == NULL) {
469                 /*
470                  * If no P-State table is created yet,
471                  * save the temporary one we just created.
472                  */
473                 acpi_pstates = pstate;
474                 acpi_npstates = npstate;
475                 pstate = NULL;
476
477                 if (bootverbose) {
478                         for (i = 0; i < acpi_npstates; ++i) {
479                                 device_printf(dev,
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);
488                         }
489                 }
490         } else {
491                 /*
492                  * Make sure that P-State tables are same
493                  * for all processors.
494                  */
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);
500                         return ENXIO;
501                 }
502                 kfree(pstate, M_TEMP);
503         }
504
505         /* By default, we start from P-State table's first entry */
506         sc->pst_sstart = 0;
507
508         /*
509          * Adjust the usable first entry of P-State table,
510          * if there is _PPC object.
511          */
512         buf.Pointer = NULL;
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");
520                                 AcpiOsFree(obj);
521                                 return ENXIO;
522                         }
523                         sc->pst_sstart = obj->Integer.Value;
524                         if (bootverbose)
525                                 device_printf(dev, "_PPC %d\n", sc->pst_sstart);
526
527                         /* TODO: Install notifiy handler */
528                 } else {
529                         device_printf(dev, "Invalid _PPC object\n");
530                         AcpiOsFree(obj);
531                         return ENXIO;
532                 }
533
534                 /* Free _PPC */
535                 AcpiOsFree(obj);
536         }
537
538         sc->pst_state = sc->pst_sstart;
539
540         /* Link us with the domain */
541         sc->pst_domain = dom;
542         LIST_INSERT_HEAD(&dom->pd_pstlist, sc, pst_link);
543
544         if (device_get_unit(dev) == 0)
545                 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_pst_postattach, NULL);
546
547         return 0;
548 }
549
550 static struct acpi_pst_domain *
551 acpi_pst_domain_create_pkg(device_t dev, ACPI_OBJECT *obj)
552 {
553         struct acpi_pst_domain *dom;
554         uint32_t val, domain, coord, nproc;
555
556         if (!ACPI_PKG_VALID_EQ(obj, 5)) {
557                 device_printf(dev, "Invalid _PSD package\n");
558                 return NULL;
559         }
560
561         /* NumberOfEntries */
562         if (acpi_PkgInt32(obj, 0, &val) != 0 || val != 5) {
563                 device_printf(dev, "Invalid _PSD NumberOfEntries\n");
564                 return NULL;
565         }
566
567         /* Revision */
568         if (acpi_PkgInt32(obj, 1, &val) != 0 || val != 0) {
569                 device_printf(dev, "Invalid _PSD Revision\n");
570                 return NULL;
571         }
572
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");
577                 return NULL;
578         }
579
580         if (!ACPI_PSD_COORD_VALID(coord)) {
581                 device_printf(dev, "Invalid _PSD CoordType (%#x)\n", coord);
582                 return NULL;
583         }
584
585         if (nproc > MAXCPU) {
586                 /*
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.
593                  */
594                 if (coord == ACPI_PSD_COORD_SWALL) {
595                         device_printf(dev, "Unsupported _PSD NumProcessors "
596                                       "(%d)\n", nproc);
597                         return NULL;
598                 }
599         } else if (nproc == 0) {
600                 device_printf(dev, "_PSD NumProcessors are zero\n");
601                 return NULL;
602         }
603
604         dom = acpi_pst_domain_find(domain);
605         if (dom != NULL) {
606                 if (dom->pd_flags & ACPI_PSTDOM_FLAG_INT) {
607                         device_printf(dev, "Mixed Integer _PSD and "
608                             "Package _PSD\n");
609                         return NULL;
610                 }
611                 if (dom->pd_coord != coord || dom->pd_nproc != nproc) {
612                         device_printf(dev, "Inconsistent _PSD information "
613                                       "cross Processor objects\n");
614                         return NULL;
615                 }
616                 return dom;
617         }
618
619         dom = acpi_pst_domain_alloc(domain, coord, nproc);
620         if (bootverbose)
621                 device_printf(dev, "create pkg domain%u\n", dom->pd_dom);
622
623         return dom;
624 }
625
626 static struct acpi_pst_domain *
627 acpi_pst_domain_create_int(device_t dev, uint32_t domain)
628 {
629         struct acpi_pst_domain *dom;
630
631         dom = acpi_pst_domain_find(domain);
632         if (dom != NULL) {
633                 if ((dom->pd_flags & ACPI_PSTDOM_FLAG_INT) == 0) {
634                         device_printf(dev, "Mixed Package _PSD and "
635                             "Integer _PSD\n");
636                         return NULL;
637                 }
638                 KKASSERT(dom->pd_coord == ACPI_PSD_COORD_SWALL);
639
640                 dom->pd_nproc++;
641                 return dom;
642         }
643
644         dom = acpi_pst_domain_alloc(domain, ACPI_PSD_COORD_SWALL, 1);
645         dom->pd_flags |= ACPI_PSTDOM_FLAG_INT;
646
647         if (bootverbose)
648                 device_printf(dev, "create int domain%u\n", dom->pd_dom);
649
650         return dom;
651 }
652
653 static struct acpi_pst_domain *
654 acpi_pst_domain_find(uint32_t domain)
655 {
656         struct acpi_pst_domain *dom;
657
658         LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
659                 if (dom->pd_flags & ACPI_PSTDOM_FLAG_STUB)
660                         continue;
661                 if (dom->pd_dom == domain)
662                         return dom;
663         }
664         return NULL;
665 }
666
667 static struct acpi_pst_domain *
668 acpi_pst_domain_alloc(uint32_t domain, uint32_t coord, uint32_t nproc)
669 {
670         struct acpi_pst_domain *dom;
671
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);
679
680         LIST_INSERT_HEAD(&acpi_pst_domains, dom, pd_link);
681
682         return dom;
683 }
684
685 static int
686 acpi_pst_domain_set_pstate(struct acpi_pst_domain *dom, int i)
687 {
688         const struct acpi_pstate *pstate;
689         struct acpi_pst_softc *sc;
690         int done, error;
691
692         KKASSERT(i >= 0 && i < acpi_npstates);
693         pstate = &acpi_pstates[i];
694
695         done = 0;
696         LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
697                 if (!done) {
698                         error = acpi_pst_set_pstate(sc, pstate);
699                         if (error) {
700                                 device_printf(sc->pst_dev, "can't set "
701                                               "freq %d\n", pstate->st_freq);
702                                 /* XXX error cleanup? */
703                         }
704                         if (dom->pd_coord == ACPI_PSD_COORD_SWANY)
705                                 done = 1;
706                 }
707                 sc->pst_state = i;
708         }
709         dom->pd_state = i;
710
711         return 0;
712 }
713
714 static int
715 acpi_pst_global_set_pstate(int i)
716 {
717         struct acpi_pst_domain *dom;
718
719         LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
720                 /* Skip dead domain */
721                 if (dom->pd_flags & ACPI_PSTDOM_FLAG_DEAD)
722                         continue;
723                 acpi_pst_domain_set_pstate(dom, i);
724         }
725         acpi_pst_global_state = i;
726
727         return 0;
728 }
729
730 static void
731 acpi_pst_postattach(void *arg __unused)
732 {
733         struct acpi_pst_domain *dom;
734         struct acpi_cpux_softc *cpux;
735         device_t *devices;
736         int i, ndevices, error, has_domain, sstate;
737
738         devices = NULL;
739         ndevices = 0;
740         error = devclass_get_devices(acpi_pst_devclass, &devices, &ndevices);
741         if (error)
742                 return;
743
744         if (ndevices == 0)
745                 return;
746
747         cpux = NULL;
748         for (i = 0; i < ndevices; ++i) {
749                 cpux = device_get_softc(device_get_parent(devices[i]));
750                 if (cpux->glob_sysctl_tree != NULL)
751                         break;
752         }
753         kfree(devices, M_TEMP);
754         KKASSERT(cpux != NULL);
755
756         if (acpi_pst_md == NULL)
757                 kprintf("ACPI: no P-State CPU driver\n");
758
759         sstate = 0x7fffffff;
760         has_domain = 0;
761         LIST_FOREACH(dom, &acpi_pst_domains, pd_link) {
762                 struct acpi_pst_softc *sc;
763                 char buf[32];
764
765                 /*
766                  * Make sure that all processors belonging to this
767                  * domain are located.
768                  */
769                 i = 0;
770                 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link)
771                         ++i;
772                 if (i != dom->pd_nproc) {
773                         KKASSERT(i < dom->pd_nproc);
774
775                         kprintf("ACPI: domain%u misses processors, "
776                                 "should be %d, got %d\n", dom->pd_dom,
777                                 dom->pd_nproc, i);
778                         if (dom->pd_coord == ACPI_PSD_COORD_SWALL) {
779                                 /*
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
785                                  * domain out.
786                                  */
787                                 dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
788                                 continue;
789                         }
790                         dom->pd_nproc = i;
791                 }
792
793                 /*
794                  * Validate P-State configurations for this domain
795                  */
796                 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
797                         error = acpi_pst_check_csr(sc);
798                         if (error)
799                                 break;
800
801                         error = acpi_pst_check_pstates(sc);
802                         if (error)
803                                 break;
804                 }
805                 if (sc != NULL) {
806                         kprintf("ACPI: domain%u P-State configuration "
807                                 "check failed\n", dom->pd_dom);
808                         dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
809                         continue;
810                 }
811
812                 /*
813                  * Do necssary P-State initialization
814                  */
815                 LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
816                         error = acpi_pst_init(sc);
817                         if (error)
818                                 break;
819                 }
820                 if (sc != NULL) {
821                         kprintf("ACPI: domain%u P-State initialization "
822                                 "check failed\n", dom->pd_dom);
823                         dom->pd_flags |= ACPI_PSTDOM_FLAG_DEAD;
824                         continue;
825                 }
826
827                 has_domain = 1;
828
829                 ksnprintf(buf, sizeof(buf), "px_dom%u", dom->pd_dom);
830
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,
836                         "P-State domain");
837                 if (dom->pd_sysctl_tree == NULL) {
838                         kprintf("ACPI: Can't create sysctl tree for domain%u",
839                                 dom->pd_dom);
840                         continue;
841                 }
842
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");
849
850                 SYSCTL_ADD_PROC(&dom->pd_sysctl_ctx,
851                                 SYSCTL_CHILDREN(dom->pd_sysctl_tree),
852                                 OID_AUTO, "members",
853                                 CTLTYPE_STRING | CTLFLAG_RD,
854                                 dom, 0, acpi_pst_sysctl_members, "A",
855                                 "member cpus");
856
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),
861                                         OID_AUTO, "select",
862                                         CTLTYPE_UINT | CTLFLAG_RW,
863                                         dom, 0, acpi_pst_sysctl_select,
864                                         "IU", "select freq");
865                 }
866
867                 if (dom->pd_state < sstate)
868                         sstate = dom->pd_state;
869         }
870
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");
879
880                 acpi_pst_global_set_pstate(sstate);
881         }
882 }
883
884 static int
885 acpi_pst_sysctl_freqs(SYSCTL_HANDLER_ARGS)
886 {
887         struct acpi_pst_domain *dom = arg1;
888         int i, error;
889
890         error = 0;
891         for (i = 0; i < acpi_npstates; ++i) {
892                 if (error == 0 && i)
893                         error = SYSCTL_OUT(req, " ", 1);
894                 if (error == 0) {
895                         const char *pat;
896                         char buf[32];
897
898                         if (i < dom->pd_sstart)
899                                 pat = "(%u)";
900                         else
901                                 pat = "%u";
902
903                         ksnprintf(buf, sizeof(buf), pat,
904                                   acpi_pstates[i].st_freq);
905                         error = SYSCTL_OUT(req, buf, strlen(buf));
906                 }
907         }
908         return error;
909 }
910
911 static int
912 acpi_pst_sysctl_members(SYSCTL_HANDLER_ARGS)
913 {
914         struct acpi_pst_domain *dom = arg1;
915         struct acpi_pst_softc *sc;
916         int loop, error;
917
918         loop = error = 0;
919         LIST_FOREACH(sc, &dom->pd_pstlist, pst_link) {
920                 char buf[32];
921
922                 if (error == 0 && loop)
923                         error = SYSCTL_OUT(req, " ", 1);
924                 if (error == 0) {
925                         ksnprintf(buf, sizeof(buf), "cpu%d", sc->pst_cpuid);
926                         error = SYSCTL_OUT(req, buf, strlen(buf));
927                 }
928
929                 if (error == 0 && acpi_pst_md && acpi_pst_md->pmd_get_pstate) {
930                         const struct acpi_pstate *pstate;
931                         const char *str;
932
933                         pstate = acpi_pst_get_pstate(sc);
934                         if (pstate == NULL) {
935                                 str = "(*)";
936                         } else {
937                                 ksnprintf(buf, sizeof(buf), "(%d)",
938                                           pstate->st_freq);
939                                 str = buf;
940                         }
941                         error = SYSCTL_OUT(req, str, strlen(str));
942                 }
943                 ++loop;
944         }
945         return error;
946 }
947
948 static int
949 acpi_pst_sysctl_select(SYSCTL_HANDLER_ARGS)
950 {
951         struct acpi_pst_domain *dom = arg1;
952         int error, i, freq;
953
954         KKASSERT(dom->pd_state >= 0 && dom->pd_state < acpi_npstates);
955
956         freq = acpi_pstates[dom->pd_state].st_freq;
957
958         error = sysctl_handle_int(oidp, &freq, 0, req);
959         if (error || req->newptr == NULL)
960                 return error;
961
962         i = acpi_pst_freq2index(freq);
963         if (i < 0)
964                 return EINVAL;
965
966         acpi_pst_domain_set_pstate(dom, i);
967         return 0;
968 }
969
970 static int
971 acpi_pst_sysctl_global(SYSCTL_HANDLER_ARGS)
972 {
973         int error, i, freq;
974
975         KKASSERT(acpi_pst_global_state >= 0 &&
976                  acpi_pst_global_state < acpi_npstates);
977
978         freq = acpi_pstates[acpi_pst_global_state].st_freq;
979
980         error = sysctl_handle_int(oidp, &freq, 0, req);
981         if (error || req->newptr == NULL)
982                 return error;
983
984         i = acpi_pst_freq2index(freq);
985         if (i < 0)
986                 return EINVAL;
987
988         acpi_pst_global_set_pstate(i);
989
990         return 0;
991 }
992
993 static void
994 acpi_pst_check_csr_handler(netmsg_t msg)
995 {
996         struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
997         int error;
998
999         error = acpi_pst_md->pmd_check_csr(rmsg->ctrl, rmsg->status);
1000         lwkt_replymsg(&rmsg->base.lmsg, error);
1001 }
1002
1003 static int
1004 acpi_pst_check_csr(struct acpi_pst_softc *sc)
1005 {
1006         struct netmsg_acpi_pst msg;
1007
1008         if (acpi_pst_md == NULL)
1009                 return 0;
1010
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;
1015
1016         return lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.base.lmsg, 0);
1017 }
1018
1019 static void
1020 acpi_pst_check_pstates_handler(netmsg_t msg)
1021 {
1022         int error;
1023
1024         error = acpi_pst_md->pmd_check_pstates(acpi_pstates, acpi_npstates);
1025         lwkt_replymsg(&msg->lmsg, error);
1026 }
1027
1028 static int
1029 acpi_pst_check_pstates(struct acpi_pst_softc *sc)
1030 {
1031         struct netmsg_base msg;
1032
1033         if (acpi_pst_md == NULL)
1034                 return 0;
1035
1036         netmsg_init(&msg, NULL, &curthread->td_msgport,
1037                     MSGF_PRIORITY, acpi_pst_check_pstates_handler);
1038
1039         return lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.lmsg, 0);
1040 }
1041
1042 static void
1043 acpi_pst_init_handler(netmsg_t msg)
1044 {
1045         struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1046         int error;
1047
1048         error = acpi_pst_md->pmd_init(rmsg->ctrl, rmsg->status);
1049         lwkt_replymsg(&rmsg->base.lmsg, error);
1050 }
1051
1052 static int
1053 acpi_pst_init(struct acpi_pst_softc *sc)
1054 {
1055         struct netmsg_acpi_pst msg;
1056
1057         if (acpi_pst_md == NULL)
1058                 return 0;
1059
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;
1064
1065         return lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.base.lmsg, 0);
1066 }
1067
1068 static void
1069 acpi_pst_set_pstate_handler(netmsg_t msg)
1070 {
1071         struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1072         int error;
1073
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);
1077 }
1078
1079 static int
1080 acpi_pst_set_pstate(struct acpi_pst_softc *sc, const struct acpi_pstate *pstate)
1081 {
1082         struct netmsg_acpi_pst msg;
1083
1084         KKASSERT(acpi_pst_md != NULL);
1085
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;
1091
1092         return lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.base.lmsg, 0);
1093 }
1094
1095 static void
1096 acpi_pst_get_pstate_handler(netmsg_t msg)
1097 {
1098         struct netmsg_acpi_pst *rmsg = (struct netmsg_acpi_pst *)msg;
1099         const struct acpi_pstate *pstate;
1100
1101         pstate = acpi_pst_md->pmd_get_pstate(rmsg->status, acpi_pstates,
1102                                              acpi_npstates);
1103         rmsg->base.lmsg.u.ms_resultp = __DECONST(void *, pstate);
1104         lwkt_replymsg(&rmsg->base.lmsg, 0);
1105 }
1106
1107 static const struct acpi_pstate *
1108 acpi_pst_get_pstate(struct acpi_pst_softc *sc)
1109 {
1110         struct netmsg_acpi_pst msg;
1111
1112         if (acpi_pst_md == NULL)
1113                 return 0;
1114
1115         netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1116                     MSGF_PRIORITY, acpi_pst_get_pstate_handler);
1117         msg.status = &sc->pst_sreg;
1118
1119         lwkt_domsg(cpu_portfn(sc->pst_cpuid), &msg.base.lmsg, 0);
1120         return msg.base.lmsg.u.ms_resultp;
1121 }
1122
1123 static int
1124 acpi_pst_alloc_resource(device_t dev, ACPI_OBJECT *obj, int idx,
1125                         struct acpi_pst_res *res)
1126 {
1127         struct acpi_pst_softc *sc = device_get_softc(dev);
1128         int error, type;
1129
1130         /* Save GAS */
1131         error = acpi_PkgRawGas(obj, idx, &res->pr_gas);
1132         if (error)
1133                 return error;
1134
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);
1142         } else {
1143                 res->pr_rid = 0;
1144         }
1145         return 0;
1146 }