acpi_cpu_md.c revision 1.1.2.2 1 1.1.2.2 thorpej /* $NetBSD: acpi_cpu_md.c,v 1.1.2.2 2020/12/14 14:37:47 thorpej Exp $ */
2 1.1.2.2 thorpej
3 1.1.2.2 thorpej /*-
4 1.1.2.2 thorpej * Copyright (c) 2020 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1.2.2 thorpej * All rights reserved.
6 1.1.2.2 thorpej *
7 1.1.2.2 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 thorpej * modification, are permitted provided that the following conditions
9 1.1.2.2 thorpej * are met:
10 1.1.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 thorpej * documentation and/or other materials provided with the distribution.
15 1.1.2.2 thorpej *
16 1.1.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.2.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.2.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.2.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.2.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.2.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.2.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.2.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.2.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.2.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.2.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
27 1.1.2.2 thorpej */
28 1.1.2.2 thorpej
29 1.1.2.2 thorpej #include <sys/cdefs.h>
30 1.1.2.2 thorpej __KERNEL_RCSID(0, "$NetBSD: acpi_cpu_md.c,v 1.1.2.2 2020/12/14 14:37:47 thorpej Exp $");
31 1.1.2.2 thorpej
32 1.1.2.2 thorpej #include <sys/param.h>
33 1.1.2.2 thorpej #include <sys/bus.h>
34 1.1.2.2 thorpej #include <sys/cpu.h>
35 1.1.2.2 thorpej #include <sys/cpufreq.h>
36 1.1.2.2 thorpej #include <sys/device.h>
37 1.1.2.2 thorpej #include <sys/sysctl.h>
38 1.1.2.2 thorpej
39 1.1.2.2 thorpej #include <dev/acpi/acpica.h>
40 1.1.2.2 thorpej #include <dev/acpi/acpivar.h>
41 1.1.2.2 thorpej #include <dev/acpi/acpi_cpu.h>
42 1.1.2.2 thorpej #include <dev/acpi/acpi_util.h>
43 1.1.2.2 thorpej
44 1.1.2.2 thorpej static struct sysctllog *acpicpu_log = NULL;
45 1.1.2.2 thorpej
46 1.1.2.2 thorpej /*
47 1.1.2.2 thorpej * acpicpu_md_match --
48 1.1.2.2 thorpej *
49 1.1.2.2 thorpej * Match against an ACPI processor device node (either a device
50 1.1.2.2 thorpej * with HID "ACPI0007" or a processor node) and return a pointer
51 1.1.2.2 thorpej * to the corresponding CPU device's 'cpu_info' struct.
52 1.1.2.2 thorpej *
53 1.1.2.2 thorpej */
54 1.1.2.2 thorpej struct cpu_info *
55 1.1.2.2 thorpej acpicpu_md_match(device_t parent, cfdata_t cf, void *aux)
56 1.1.2.2 thorpej {
57 1.1.2.2 thorpej struct acpi_attach_args * const aa = aux;
58 1.1.2.2 thorpej
59 1.1.2.2 thorpej return acpi_match_cpu_handle(aa->aa_node->ad_handle);
60 1.1.2.2 thorpej }
61 1.1.2.2 thorpej
62 1.1.2.2 thorpej /*
63 1.1.2.2 thorpej * acpicpu_md_attach --
64 1.1.2.2 thorpej *
65 1.1.2.2 thorpej * Return a pointer to the CPU device's 'cpu_info' struct
66 1.1.2.2 thorpej * corresponding with this device.
67 1.1.2.2 thorpej *
68 1.1.2.2 thorpej */
69 1.1.2.2 thorpej struct cpu_info *
70 1.1.2.2 thorpej acpicpu_md_attach(device_t parent, device_t self, void *aux)
71 1.1.2.2 thorpej {
72 1.1.2.2 thorpej struct acpi_attach_args * const aa = aux;
73 1.1.2.2 thorpej
74 1.1.2.2 thorpej return acpi_match_cpu_handle(aa->aa_node->ad_handle);
75 1.1.2.2 thorpej }
76 1.1.2.2 thorpej
77 1.1.2.2 thorpej /*
78 1.1.2.2 thorpej * acpicpu_md_flags --
79 1.1.2.2 thorpej *
80 1.1.2.2 thorpej * Return a bitmask of ACPICPU_FLAG_* platform specific quirks.
81 1.1.2.2 thorpej *
82 1.1.2.2 thorpej */
83 1.1.2.2 thorpej uint32_t
84 1.1.2.2 thorpej acpicpu_md_flags(void)
85 1.1.2.2 thorpej {
86 1.1.2.2 thorpej return 0;
87 1.1.2.2 thorpej }
88 1.1.2.2 thorpej
89 1.1.2.2 thorpej /*
90 1.1.2.2 thorpej * acpicpu_md_cstate_start --
91 1.1.2.2 thorpej *
92 1.1.2.2 thorpej * Not implemented.
93 1.1.2.2 thorpej *
94 1.1.2.2 thorpej */
95 1.1.2.2 thorpej int
96 1.1.2.2 thorpej acpicpu_md_cstate_start(struct acpicpu_softc *sc)
97 1.1.2.2 thorpej {
98 1.1.2.2 thorpej return EINVAL;
99 1.1.2.2 thorpej }
100 1.1.2.2 thorpej
101 1.1.2.2 thorpej /*
102 1.1.2.2 thorpej * acpicpu_md_cstate_stop --
103 1.1.2.2 thorpej *
104 1.1.2.2 thorpej * Not implemented.
105 1.1.2.2 thorpej *
106 1.1.2.2 thorpej */
107 1.1.2.2 thorpej int
108 1.1.2.2 thorpej acpicpu_md_cstate_stop(void)
109 1.1.2.2 thorpej {
110 1.1.2.2 thorpej return EALREADY;
111 1.1.2.2 thorpej }
112 1.1.2.2 thorpej
113 1.1.2.2 thorpej /*
114 1.1.2.2 thorpej * acpicpu_md_cstate_enter --
115 1.1.2.2 thorpej *
116 1.1.2.2 thorpej * Not implemented.
117 1.1.2.2 thorpej *
118 1.1.2.2 thorpej */
119 1.1.2.2 thorpej void
120 1.1.2.2 thorpej acpicpu_md_cstate_enter(int method, int state)
121 1.1.2.2 thorpej {
122 1.1.2.2 thorpej }
123 1.1.2.2 thorpej
124 1.1.2.2 thorpej /*
125 1.1.2.2 thorpej * acpicpu_md_pstate_init --
126 1.1.2.2 thorpej *
127 1.1.2.2 thorpej * MD initialization for P-state support. Nothing to do here.
128 1.1.2.2 thorpej *
129 1.1.2.2 thorpej */
130 1.1.2.2 thorpej int
131 1.1.2.2 thorpej acpicpu_md_pstate_init(struct acpicpu_softc *sc)
132 1.1.2.2 thorpej {
133 1.1.2.2 thorpej return 0;
134 1.1.2.2 thorpej }
135 1.1.2.2 thorpej
136 1.1.2.2 thorpej /*
137 1.1.2.2 thorpej * acpicpu_md_pstate_sysctl_current --
138 1.1.2.2 thorpej *
139 1.1.2.2 thorpej * sysctl(9) callback for retrieving the current CPU frequency.
140 1.1.2.2 thorpej *
141 1.1.2.2 thorpej */
142 1.1.2.2 thorpej static int
143 1.1.2.2 thorpej acpicpu_md_pstate_sysctl_current(SYSCTLFN_ARGS)
144 1.1.2.2 thorpej {
145 1.1.2.2 thorpej struct sysctlnode node;
146 1.1.2.2 thorpej uint32_t freq;
147 1.1.2.2 thorpej int error;
148 1.1.2.2 thorpej
149 1.1.2.2 thorpej freq = cpufreq_get(curcpu());
150 1.1.2.2 thorpej if (freq == 0)
151 1.1.2.2 thorpej return ENXIO;
152 1.1.2.2 thorpej
153 1.1.2.2 thorpej node = *rnode;
154 1.1.2.2 thorpej node.sysctl_data = &freq;
155 1.1.2.2 thorpej
156 1.1.2.2 thorpej error = sysctl_lookup(SYSCTLFN_CALL(&node));
157 1.1.2.2 thorpej if (error || newp == NULL)
158 1.1.2.2 thorpej return error;
159 1.1.2.2 thorpej
160 1.1.2.2 thorpej return 0;
161 1.1.2.2 thorpej }
162 1.1.2.2 thorpej
163 1.1.2.2 thorpej /*
164 1.1.2.2 thorpej * acpicpu_md_pstate_sysctl_target --
165 1.1.2.2 thorpej *
166 1.1.2.2 thorpej * sysctl(9) callback for setting the target CPU frequency.
167 1.1.2.2 thorpej *
168 1.1.2.2 thorpej */
169 1.1.2.2 thorpej static int
170 1.1.2.2 thorpej acpicpu_md_pstate_sysctl_target(SYSCTLFN_ARGS)
171 1.1.2.2 thorpej {
172 1.1.2.2 thorpej struct sysctlnode node;
173 1.1.2.2 thorpej uint32_t freq;
174 1.1.2.2 thorpej int error;
175 1.1.2.2 thorpej
176 1.1.2.2 thorpej freq = cpufreq_get(curcpu());
177 1.1.2.2 thorpej if (freq == 0)
178 1.1.2.2 thorpej return ENXIO;
179 1.1.2.2 thorpej
180 1.1.2.2 thorpej node = *rnode;
181 1.1.2.2 thorpej node.sysctl_data = &freq;
182 1.1.2.2 thorpej
183 1.1.2.2 thorpej error = sysctl_lookup(SYSCTLFN_CALL(&node));
184 1.1.2.2 thorpej if (error || newp == NULL)
185 1.1.2.2 thorpej return error;
186 1.1.2.2 thorpej
187 1.1.2.2 thorpej cpufreq_set_all(freq);
188 1.1.2.2 thorpej
189 1.1.2.2 thorpej return 0;
190 1.1.2.2 thorpej }
191 1.1.2.2 thorpej
192 1.1.2.2 thorpej /*
193 1.1.2.2 thorpej * acpicpu_md_pstate_sysctl_available --
194 1.1.2.2 thorpej *
195 1.1.2.2 thorpej * sysctl(9) callback for returning a list of supported CPU frequencies.
196 1.1.2.2 thorpej *
197 1.1.2.2 thorpej */
198 1.1.2.2 thorpej static int
199 1.1.2.2 thorpej acpicpu_md_pstate_sysctl_available(SYSCTLFN_ARGS)
200 1.1.2.2 thorpej {
201 1.1.2.2 thorpej struct acpicpu_softc * const sc = rnode->sysctl_data;
202 1.1.2.2 thorpej struct sysctlnode node;
203 1.1.2.2 thorpej char buf[1024];
204 1.1.2.2 thorpej size_t len;
205 1.1.2.2 thorpej uint32_t i;
206 1.1.2.2 thorpej int error;
207 1.1.2.2 thorpej
208 1.1.2.2 thorpej memset(buf, 0, sizeof(buf));
209 1.1.2.2 thorpej
210 1.1.2.2 thorpej mutex_enter(&sc->sc_mtx);
211 1.1.2.2 thorpej for (len = 0, i = sc->sc_pstate_max; i < sc->sc_pstate_count; i++) {
212 1.1.2.2 thorpej if (sc->sc_pstate[i].ps_freq == 0)
213 1.1.2.2 thorpej continue;
214 1.1.2.2 thorpej if (len >= sizeof(buf))
215 1.1.2.2 thorpej break;
216 1.1.2.2 thorpej len += snprintf(buf + len, sizeof(buf) - len, "%u%s",
217 1.1.2.2 thorpej sc->sc_pstate[i].ps_freq,
218 1.1.2.2 thorpej i < (sc->sc_pstate_count - 1) ? " " : "");
219 1.1.2.2 thorpej }
220 1.1.2.2 thorpej mutex_exit(&sc->sc_mtx);
221 1.1.2.2 thorpej
222 1.1.2.2 thorpej node = *rnode;
223 1.1.2.2 thorpej node.sysctl_data = buf;
224 1.1.2.2 thorpej
225 1.1.2.2 thorpej error = sysctl_lookup(SYSCTLFN_CALL(&node));
226 1.1.2.2 thorpej if (error || newp == NULL)
227 1.1.2.2 thorpej return error;
228 1.1.2.2 thorpej
229 1.1.2.2 thorpej return 0;
230 1.1.2.2 thorpej }
231 1.1.2.2 thorpej
232 1.1.2.2 thorpej /*
233 1.1.2.2 thorpej * acpicpu_md_pstate_start --
234 1.1.2.2 thorpej *
235 1.1.2.2 thorpej * MD startup for P-state support. Create sysctls here.
236 1.1.2.2 thorpej *
237 1.1.2.2 thorpej */
238 1.1.2.2 thorpej int
239 1.1.2.2 thorpej acpicpu_md_pstate_start(struct acpicpu_softc *sc)
240 1.1.2.2 thorpej {
241 1.1.2.2 thorpej const struct sysctlnode *mnode, *cnode, *fnode, *node;
242 1.1.2.2 thorpej int error;
243 1.1.2.2 thorpej
244 1.1.2.2 thorpej error = sysctl_createv(&acpicpu_log, 0, NULL, &mnode,
245 1.1.2.2 thorpej CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
246 1.1.2.2 thorpej NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
247 1.1.2.2 thorpej if (error) {
248 1.1.2.2 thorpej goto teardown;
249 1.1.2.2 thorpej }
250 1.1.2.2 thorpej
251 1.1.2.2 thorpej error = sysctl_createv(&acpicpu_log, 0, &mnode, &cnode,
252 1.1.2.2 thorpej 0, CTLTYPE_NODE, "cpu", NULL,
253 1.1.2.2 thorpej NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
254 1.1.2.2 thorpej if (error) {
255 1.1.2.2 thorpej goto teardown;
256 1.1.2.2 thorpej }
257 1.1.2.2 thorpej
258 1.1.2.2 thorpej error = sysctl_createv(&acpicpu_log, 0, &cnode, &fnode,
259 1.1.2.2 thorpej 0, CTLTYPE_NODE, "frequency", NULL,
260 1.1.2.2 thorpej NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
261 1.1.2.2 thorpej if (error) {
262 1.1.2.2 thorpej goto teardown;
263 1.1.2.2 thorpej }
264 1.1.2.2 thorpej
265 1.1.2.2 thorpej error = sysctl_createv(&acpicpu_log, 0, &fnode, &node,
266 1.1.2.2 thorpej CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL,
267 1.1.2.2 thorpej acpicpu_md_pstate_sysctl_target, 0, NULL, 0, CTL_CREATE, CTL_EOL);
268 1.1.2.2 thorpej if (error) {
269 1.1.2.2 thorpej goto teardown;
270 1.1.2.2 thorpej }
271 1.1.2.2 thorpej
272 1.1.2.2 thorpej error = sysctl_createv(&acpicpu_log, 0, &fnode, &node,
273 1.1.2.2 thorpej CTLFLAG_READONLY, CTLTYPE_INT, "current", NULL,
274 1.1.2.2 thorpej acpicpu_md_pstate_sysctl_current, 0, NULL, 0, CTL_CREATE, CTL_EOL);
275 1.1.2.2 thorpej if (error) {
276 1.1.2.2 thorpej goto teardown;
277 1.1.2.2 thorpej }
278 1.1.2.2 thorpej
279 1.1.2.2 thorpej error = sysctl_createv(&acpicpu_log, 0, &fnode, &node,
280 1.1.2.2 thorpej CTLFLAG_READONLY, CTLTYPE_STRING, "available", NULL,
281 1.1.2.2 thorpej acpicpu_md_pstate_sysctl_available, 0, (void *)sc, 0, CTL_CREATE,
282 1.1.2.2 thorpej CTL_EOL);
283 1.1.2.2 thorpej if (error) {
284 1.1.2.2 thorpej goto teardown;
285 1.1.2.2 thorpej }
286 1.1.2.2 thorpej
287 1.1.2.2 thorpej return 0;
288 1.1.2.2 thorpej
289 1.1.2.2 thorpej teardown:
290 1.1.2.2 thorpej if (acpicpu_log != NULL) {
291 1.1.2.2 thorpej sysctl_teardown(&acpicpu_log);
292 1.1.2.2 thorpej acpicpu_log = NULL;
293 1.1.2.2 thorpej }
294 1.1.2.2 thorpej
295 1.1.2.2 thorpej return error;
296 1.1.2.2 thorpej }
297 1.1.2.2 thorpej
298 1.1.2.2 thorpej /*
299 1.1.2.2 thorpej * acpicpu_md_pstate_stop --
300 1.1.2.2 thorpej *
301 1.1.2.2 thorpej * MD shutdown for P-state support. Destroy sysctls here.
302 1.1.2.2 thorpej *
303 1.1.2.2 thorpej */
304 1.1.2.2 thorpej int
305 1.1.2.2 thorpej acpicpu_md_pstate_stop(void)
306 1.1.2.2 thorpej {
307 1.1.2.2 thorpej if (acpicpu_log != NULL) {
308 1.1.2.2 thorpej sysctl_teardown(&acpicpu_log);
309 1.1.2.2 thorpej acpicpu_log = NULL;
310 1.1.2.2 thorpej }
311 1.1.2.2 thorpej
312 1.1.2.2 thorpej return 0;
313 1.1.2.2 thorpej }
314 1.1.2.2 thorpej
315 1.1.2.2 thorpej /*
316 1.1.2.2 thorpej * acpicpu_md_pstate_get --
317 1.1.2.2 thorpej *
318 1.1.2.2 thorpej * Fixed hardware access method for getting current processor P-state.
319 1.1.2.2 thorpej * Not implemented.
320 1.1.2.2 thorpej *
321 1.1.2.2 thorpej */
322 1.1.2.2 thorpej int
323 1.1.2.2 thorpej acpicpu_md_pstate_get(struct acpicpu_softc *sc, uint32_t *freq)
324 1.1.2.2 thorpej {
325 1.1.2.2 thorpej return EINVAL;
326 1.1.2.2 thorpej }
327 1.1.2.2 thorpej
328 1.1.2.2 thorpej /*
329 1.1.2.2 thorpej * acpicpu_md_pstate_set --
330 1.1.2.2 thorpej *
331 1.1.2.2 thorpej * Fixed hardware access method for setting current processor P-state.
332 1.1.2.2 thorpej * Not implemented.
333 1.1.2.2 thorpej *
334 1.1.2.2 thorpej */
335 1.1.2.2 thorpej int
336 1.1.2.2 thorpej acpicpu_md_pstate_set(struct acpicpu_pstate *ps)
337 1.1.2.2 thorpej {
338 1.1.2.2 thorpej return EINVAL;
339 1.1.2.2 thorpej }
340 1.1.2.2 thorpej
341 1.1.2.2 thorpej /*
342 1.1.2.2 thorpej * acpicpu_md_tstate_get --
343 1.1.2.2 thorpej *
344 1.1.2.2 thorpej * Fixed hardware access method for getting current processor T-state.
345 1.1.2.2 thorpej * Not implemented.
346 1.1.2.2 thorpej *
347 1.1.2.2 thorpej */
348 1.1.2.2 thorpej int
349 1.1.2.2 thorpej acpicpu_md_tstate_get(struct acpicpu_softc *sc, uint32_t *percent)
350 1.1.2.2 thorpej {
351 1.1.2.2 thorpej return EINVAL;
352 1.1.2.2 thorpej }
353 1.1.2.2 thorpej
354 1.1.2.2 thorpej /*
355 1.1.2.2 thorpej * acpicpu_md_tstate_set --
356 1.1.2.2 thorpej *
357 1.1.2.2 thorpej * Fixed hardware access method for setting current processor T-state.
358 1.1.2.2 thorpej * Not implemented.
359 1.1.2.2 thorpej *
360 1.1.2.2 thorpej */
361 1.1.2.2 thorpej int
362 1.1.2.2 thorpej acpicpu_md_tstate_set(struct acpicpu_tstate *ts)
363 1.1.2.2 thorpej {
364 1.1.2.2 thorpej return EINVAL;
365 1.1.2.2 thorpej }
366