apple_smc.c revision 1.8 1 1.8 thorpej /* $NetBSD: apple_smc.c,v 1.8 2021/08/07 16:19:12 thorpej Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*
4 1.1 riastrad * Apple System Management Controller
5 1.1 riastrad */
6 1.1 riastrad
7 1.1 riastrad /*-
8 1.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
9 1.1 riastrad * All rights reserved.
10 1.1 riastrad *
11 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
12 1.1 riastrad * by Taylor R. Campbell.
13 1.1 riastrad *
14 1.1 riastrad * Redistribution and use in source and binary forms, with or without
15 1.1 riastrad * modification, are permitted provided that the following conditions
16 1.1 riastrad * are met:
17 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
18 1.1 riastrad * notice, this list of conditions and the following disclaimer.
19 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
20 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
21 1.1 riastrad * documentation and/or other materials provided with the distribution.
22 1.1 riastrad *
23 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
34 1.1 riastrad */
35 1.1 riastrad
36 1.1 riastrad #include <sys/cdefs.h>
37 1.8 thorpej __KERNEL_RCSID(0, "$NetBSD: apple_smc.c,v 1.8 2021/08/07 16:19:12 thorpej Exp $");
38 1.1 riastrad
39 1.1 riastrad #include <sys/types.h>
40 1.1 riastrad #include <sys/param.h>
41 1.1 riastrad #include <sys/device.h>
42 1.1 riastrad #include <sys/errno.h>
43 1.1 riastrad #include <sys/kmem.h>
44 1.2 riastrad #include <sys/module.h>
45 1.1 riastrad #include <sys/mutex.h>
46 1.2 riastrad #include <sys/rwlock.h>
47 1.1 riastrad #if 0 /* XXX sysctl */
48 1.1 riastrad #include <sys/sysctl.h>
49 1.1 riastrad #endif
50 1.1 riastrad #include <sys/systm.h>
51 1.1 riastrad
52 1.1 riastrad #include <dev/ic/apple_smc.h>
53 1.1 riastrad #include <dev/ic/apple_smcreg.h>
54 1.1 riastrad #include <dev/ic/apple_smcvar.h>
55 1.1 riastrad
56 1.3 riastrad /* Must match the config(5) name. */
57 1.2 riastrad #define APPLE_SMC_BUS "applesmcbus"
58 1.2 riastrad
59 1.3 riastrad static int apple_smc_search(device_t, cfdata_t, const int *, void *);
60 1.1 riastrad static uint8_t apple_smc_bus_read_1(struct apple_smc_tag *, bus_size_t);
61 1.1 riastrad static void apple_smc_bus_write_1(struct apple_smc_tag *, bus_size_t,
62 1.1 riastrad uint8_t);
63 1.1 riastrad static int apple_smc_read_data(struct apple_smc_tag *, uint8_t *);
64 1.1 riastrad static int apple_smc_write(struct apple_smc_tag *, bus_size_t, uint8_t);
65 1.1 riastrad static int apple_smc_write_cmd(struct apple_smc_tag *, uint8_t);
66 1.1 riastrad static int apple_smc_write_data(struct apple_smc_tag *, uint8_t);
67 1.1 riastrad static int apple_smc_begin(struct apple_smc_tag *, uint8_t,
68 1.1 riastrad const char *, uint8_t);
69 1.1 riastrad static int apple_smc_input(struct apple_smc_tag *, uint8_t,
70 1.1 riastrad const char *, void *, uint8_t);
71 1.1 riastrad static int apple_smc_output(struct apple_smc_tag *, uint8_t,
72 1.1 riastrad const char *, const void *, uint8_t);
73 1.2 riastrad
74 1.2 riastrad void
76 1.1 riastrad apple_smc_attach(struct apple_smc_tag *smc)
77 1.1 riastrad {
78 1.4 riastrad
79 1.2 riastrad mutex_init(&smc->smc_io_lock, MUTEX_DEFAULT, IPL_NONE);
80 1.2 riastrad #if 0 /* XXX sysctl */
81 1.2 riastrad apple_smc_sysctl_setup(smc);
82 1.2 riastrad #endif
83 1.4 riastrad
84 1.7 thorpej /* Attach any children. */
85 1.1 riastrad (void)apple_smc_rescan(smc, NULL, NULL);
86 1.1 riastrad }
87 1.1 riastrad
88 1.1 riastrad int
89 1.1 riastrad apple_smc_detach(struct apple_smc_tag *smc, int flags)
90 1.1 riastrad {
91 1.1 riastrad int error;
92 1.4 riastrad
93 1.1 riastrad /* Fail if we can't detach all our children. */
94 1.1 riastrad error = config_detach_children(smc->smc_dev, flags);
95 1.1 riastrad if (error)
96 1.1 riastrad return error;
97 1.1 riastrad
98 1.1 riastrad #if 0 /* XXX sysctl */
99 1.1 riastrad sysctl_teardown(&smc->smc_log);
100 1.4 riastrad #endif
101 1.1 riastrad mutex_destroy(&smc->smc_io_lock);
102 1.1 riastrad
103 1.1 riastrad return 0;
104 1.1 riastrad }
105 1.2 riastrad
106 1.2 riastrad int
107 1.3 riastrad apple_smc_rescan(struct apple_smc_tag *smc, const char *ifattr,
108 1.2 riastrad const int *locators)
109 1.2 riastrad {
110 1.4 riastrad
111 1.7 thorpej /* Let autoconf(9) do the work of finding new children. */
112 1.8 thorpej config_search(smc->smc_dev, smc,
113 1.3 riastrad CFARGS(.search = apple_smc_search));
114 1.3 riastrad return 0;
115 1.2 riastrad }
116 1.3 riastrad
117 1.3 riastrad static int
118 1.3 riastrad apple_smc_search(device_t parent, cfdata_t cf, const int *locators, void *aux)
119 1.3 riastrad {
120 1.3 riastrad struct apple_smc_tag *const smc = aux;
121 1.3 riastrad static const struct apple_smc_attach_args zero_asa;
122 1.3 riastrad struct apple_smc_attach_args asa = zero_asa;
123 1.3 riastrad device_t dev;
124 1.3 riastrad deviter_t di;
125 1.2 riastrad bool attached = false;
126 1.2 riastrad
127 1.3 riastrad /*
128 1.2 riastrad * If this device has already attached, don't attach it again.
129 1.3 riastrad *
130 1.3 riastrad * XXX This is a pretty silly way to query the children, but
131 1.2 riastrad * struct device doesn't seem to list its children.
132 1.3 riastrad */
133 1.3 riastrad for (dev = deviter_first(&di, DEVITER_F_LEAVES_FIRST);
134 1.3 riastrad dev != NULL;
135 1.3 riastrad dev = deviter_next(&di)) {
136 1.2 riastrad if (device_parent(dev) != parent)
137 1.3 riastrad continue;
138 1.2 riastrad if (!device_is_a(dev, cf->cf_name))
139 1.3 riastrad continue;
140 1.3 riastrad attached = true;
141 1.3 riastrad break;
142 1.3 riastrad }
143 1.3 riastrad deviter_release(&di);
144 1.3 riastrad if (attached)
145 1.2 riastrad return 0;
146 1.3 riastrad
147 1.7 thorpej /* If this device doesn't match, don't attach it. */
148 1.3 riastrad if (!config_probe(parent, cf, aux))
149 1.2 riastrad return 0;
150 1.3 riastrad
151 1.3 riastrad /* Looks hunky-dory. Attach. */
152 1.7 thorpej asa.asa_smc = smc;
153 1.8 thorpej config_attach(parent, cf, &asa, NULL,
154 1.3 riastrad CFARGS(.locators = locators));
155 1.2 riastrad return 0;
156 1.2 riastrad }
157 1.2 riastrad
158 1.3 riastrad void
159 1.3 riastrad apple_smc_child_detached(struct apple_smc_tag *smc __unused,
160 1.2 riastrad device_t child __unused)
161 1.4 riastrad {
162 1.2 riastrad /* We keep no books about our children. */
163 1.2 riastrad }
164 1.1 riastrad
165 1.1 riastrad static uint8_t
167 1.1 riastrad apple_smc_bus_read_1(struct apple_smc_tag *smc, bus_size_t reg)
168 1.1 riastrad {
169 1.1 riastrad
170 1.1 riastrad return bus_space_read_1(smc->smc_bst, smc->smc_bsh, reg);
171 1.1 riastrad }
172 1.1 riastrad
173 1.1 riastrad static void
174 1.1 riastrad apple_smc_bus_write_1(struct apple_smc_tag *smc, bus_size_t reg, uint8_t v)
175 1.1 riastrad {
176 1.1 riastrad
177 1.1 riastrad bus_space_write_1(smc->smc_bst, smc->smc_bsh, reg, v);
178 1.1 riastrad }
179 1.1 riastrad
180 1.1 riastrad /*
181 1.1 riastrad * XXX These delays are pretty randomly chosen. Wait in 100 us
182 1.1 riastrad * increments, up to a total of 1 ms.
183 1.1 riastrad */
184 1.1 riastrad
185 1.1 riastrad static int
186 1.1 riastrad apple_smc_read_data(struct apple_smc_tag *smc, uint8_t *byte)
187 1.1 riastrad {
188 1.1 riastrad uint8_t status;
189 1.4 riastrad unsigned int i;
190 1.1 riastrad
191 1.4 riastrad KASSERT(mutex_owned(&smc->smc_io_lock));
192 1.4 riastrad
193 1.4 riastrad /*
194 1.4 riastrad * Wait until the status register says there's data to read and
195 1.1 riastrad * read it.
196 1.1 riastrad */
197 1.1 riastrad for (i = 0; i < 100; i++) {
198 1.1 riastrad status = apple_smc_bus_read_1(smc, APPLE_SMC_CSR);
199 1.1 riastrad if (status & APPLE_SMC_STATUS_READ_READY) {
200 1.1 riastrad *byte = apple_smc_bus_read_1(smc, APPLE_SMC_DATA);
201 1.1 riastrad return 0;
202 1.1 riastrad }
203 1.1 riastrad DELAY(100);
204 1.1 riastrad }
205 1.1 riastrad
206 1.1 riastrad return ETIMEDOUT;
207 1.1 riastrad }
208 1.1 riastrad
209 1.1 riastrad static int
210 1.1 riastrad apple_smc_write(struct apple_smc_tag *smc, bus_size_t reg, uint8_t byte)
211 1.1 riastrad {
212 1.1 riastrad uint8_t status;
213 1.4 riastrad unsigned int i;
214 1.1 riastrad
215 1.4 riastrad KASSERT(mutex_owned(&smc->smc_io_lock));
216 1.4 riastrad
217 1.4 riastrad /*
218 1.4 riastrad * Write the byte and then wait until the status register says
219 1.1 riastrad * it has been accepted.
220 1.1 riastrad */
221 1.1 riastrad apple_smc_bus_write_1(smc, reg, byte);
222 1.1 riastrad for (i = 0; i < 100; i++) {
223 1.1 riastrad status = apple_smc_bus_read_1(smc, APPLE_SMC_CSR);
224 1.1 riastrad if (status & APPLE_SMC_STATUS_WRITE_ACCEPTED)
225 1.4 riastrad return 0;
226 1.4 riastrad DELAY(100);
227 1.1 riastrad
228 1.1 riastrad /* Write again if it hasn't been acknowledged at all. */
229 1.1 riastrad if (!(status & APPLE_SMC_STATUS_WRITE_PENDING))
230 1.1 riastrad apple_smc_bus_write_1(smc, reg, byte);
231 1.1 riastrad }
232 1.1 riastrad
233 1.1 riastrad return ETIMEDOUT;
234 1.1 riastrad }
235 1.1 riastrad
236 1.1 riastrad static int
237 1.1 riastrad apple_smc_write_cmd(struct apple_smc_tag *smc, uint8_t cmd)
238 1.1 riastrad {
239 1.1 riastrad
240 1.1 riastrad return apple_smc_write(smc, APPLE_SMC_CSR, cmd);
241 1.1 riastrad }
242 1.1 riastrad
243 1.1 riastrad static int
244 1.1 riastrad apple_smc_write_data(struct apple_smc_tag *smc, uint8_t data)
245 1.1 riastrad {
246 1.1 riastrad
247 1.2 riastrad return apple_smc_write(smc, APPLE_SMC_DATA, data);
248 1.1 riastrad }
249 1.1 riastrad
250 1.1 riastrad static int
252 1.1 riastrad apple_smc_begin(struct apple_smc_tag *smc, uint8_t cmd, const char *key,
253 1.1 riastrad uint8_t size)
254 1.1 riastrad {
255 1.4 riastrad unsigned int i;
256 1.1 riastrad int error;
257 1.4 riastrad
258 1.1 riastrad KASSERT(mutex_owned(&smc->smc_io_lock));
259 1.1 riastrad
260 1.1 riastrad /* Write the command first. */
261 1.1 riastrad error = apple_smc_write_cmd(smc, cmd);
262 1.4 riastrad if (error)
263 1.1 riastrad return error;
264 1.1 riastrad
265 1.1 riastrad /* Write the key next. */
266 1.1 riastrad for (i = 0; i < 4; i++) {
267 1.1 riastrad error = apple_smc_write_data(smc, key[i]);
268 1.1 riastrad if (error)
269 1.4 riastrad return error;
270 1.1 riastrad }
271 1.1 riastrad
272 1.1 riastrad /* Finally, report how many bytes of data we want to send/receive. */
273 1.1 riastrad error = apple_smc_write_data(smc, size);
274 1.1 riastrad if (error)
275 1.1 riastrad return error;
276 1.1 riastrad
277 1.1 riastrad return 0;
278 1.1 riastrad }
279 1.1 riastrad
280 1.1 riastrad static int
281 1.1 riastrad apple_smc_input(struct apple_smc_tag *smc, uint8_t cmd, const char *key,
282 1.1 riastrad void *buffer, uint8_t size)
283 1.1 riastrad {
284 1.1 riastrad uint8_t *bytes = buffer;
285 1.4 riastrad uint8_t i;
286 1.4 riastrad int error;
287 1.4 riastrad
288 1.4 riastrad /* Grab the SMC I/O lock. */
289 1.1 riastrad mutex_enter(&smc->smc_io_lock);
290 1.1 riastrad
291 1.1 riastrad /* Initiate the command with this key. */
292 1.1 riastrad error = apple_smc_begin(smc, cmd, key, size);
293 1.4 riastrad if (error)
294 1.1 riastrad goto out;
295 1.1 riastrad
296 1.1 riastrad /* Read each byte of data in sequence. */
297 1.1 riastrad for (i = 0; i < size; i++) {
298 1.1 riastrad error = apple_smc_read_data(smc, &bytes[i]);
299 1.1 riastrad if (error)
300 1.1 riastrad goto out;
301 1.1 riastrad }
302 1.1 riastrad
303 1.4 riastrad /* Success! */
304 1.1 riastrad error = 0;
305 1.1 riastrad
306 1.1 riastrad out: mutex_exit(&smc->smc_io_lock);
307 1.1 riastrad return error;
308 1.1 riastrad }
309 1.1 riastrad
310 1.1 riastrad static int
311 1.1 riastrad apple_smc_output(struct apple_smc_tag *smc, uint8_t cmd, const char *key,
312 1.1 riastrad const void *buffer, uint8_t size)
313 1.1 riastrad {
314 1.1 riastrad const uint8_t *bytes = buffer;
315 1.4 riastrad uint8_t i;
316 1.4 riastrad int error;
317 1.4 riastrad
318 1.4 riastrad /* Grab the SMC I/O lock. */
319 1.1 riastrad mutex_enter(&smc->smc_io_lock);
320 1.1 riastrad
321 1.1 riastrad /* Initiate the command with this key. */
322 1.1 riastrad error = apple_smc_begin(smc, cmd, key, size);
323 1.4 riastrad if (error)
324 1.1 riastrad goto out;
325 1.1 riastrad
326 1.1 riastrad /* Write each byte of data in sequence. */
327 1.1 riastrad for (i = 0; i < size; i++) {
328 1.1 riastrad error = apple_smc_write_data(smc, bytes[i]);
329 1.1 riastrad if (error)
330 1.4 riastrad goto out;
331 1.4 riastrad }
332 1.4 riastrad
333 1.4 riastrad /* Success! */
334 1.1 riastrad error = 0;
335 1.1 riastrad
336 1.2 riastrad out: mutex_exit(&smc->smc_io_lock);
337 1.2 riastrad return error;
338 1.2 riastrad }
339 1.2 riastrad
340 1.2 riastrad struct apple_smc_key {
342 1.2 riastrad char ask_name[4 + 1];
343 1.2 riastrad struct apple_smc_desc ask_desc;
344 1.1 riastrad #ifdef DIAGNOSTIC
345 1.1 riastrad struct apple_smc_tag *ask_smc;
346 1.1 riastrad #endif
347 1.1 riastrad };
348 1.1 riastrad
349 1.1 riastrad const char *
350 1.1 riastrad apple_smc_key_name(const struct apple_smc_key *key)
351 1.1 riastrad {
352 1.1 riastrad
353 1.1 riastrad return key->ask_name;
354 1.1 riastrad }
355 1.1 riastrad
356 1.1 riastrad const struct apple_smc_desc *
357 1.1 riastrad apple_smc_key_desc(const struct apple_smc_key *key)
358 1.1 riastrad {
359 1.1 riastrad
360 1.1 riastrad return &key->ask_desc;
361 1.1 riastrad }
362 1.1 riastrad
363 1.1 riastrad uint32_t
364 1.1 riastrad apple_smc_nkeys(struct apple_smc_tag *smc)
365 1.1 riastrad {
366 1.1 riastrad
367 1.1 riastrad return smc->smc_nkeys;
368 1.1 riastrad }
369 1.1 riastrad
370 1.1 riastrad int
371 1.1 riastrad apple_smc_nth_key(struct apple_smc_tag *smc, uint32_t index,
372 1.1 riastrad const char type[4 + 1], struct apple_smc_key **keyp)
373 1.1 riastrad {
374 1.4 riastrad union { uint32_t u32; char name[4]; } index_be;
375 1.1 riastrad struct apple_smc_key *key;
376 1.1 riastrad int error;
377 1.1 riastrad
378 1.4 riastrad /* Paranoia: type must be NULL or 4 non-null characters long. */
379 1.1 riastrad if ((type != NULL) && (strlen(type) != 4))
380 1.1 riastrad return EINVAL;
381 1.1 riastrad
382 1.1 riastrad /* Create a new key. XXX Consider caching these. */
383 1.1 riastrad key = kmem_alloc(sizeof(*key), KM_SLEEP);
384 1.4 riastrad #ifdef DIAGNOSTIC
385 1.1 riastrad key->ask_smc = smc;
386 1.1 riastrad #endif
387 1.1 riastrad
388 1.1 riastrad /* Ask the SMC what the name of the key by this number is. */
389 1.1 riastrad index_be.u32 = htobe32(index);
390 1.4 riastrad error = apple_smc_input(smc, APPLE_SMC_CMD_NTH_KEY, index_be.name,
391 1.4 riastrad key->ask_name, 4);
392 1.1 riastrad if (error)
393 1.1 riastrad goto fail;
394 1.4 riastrad
395 1.1 riastrad /* Null-terminate the name. */
396 1.1 riastrad key->ask_name[4] = '\0';
397 1.1 riastrad
398 1.1 riastrad /* Ask the SMC for a description of this key by name. */
399 1.1 riastrad CTASSERT(sizeof(key->ask_desc) == 6);
400 1.1 riastrad error = apple_smc_input(smc, APPLE_SMC_CMD_KEY_DESC, key->ask_name,
401 1.4 riastrad &key->ask_desc, 6);
402 1.1 riastrad if (error)
403 1.1 riastrad goto fail;
404 1.1 riastrad
405 1.1 riastrad /* Fail with EINVAL if the types don't match. */
406 1.1 riastrad if ((type != NULL) && (0 != memcmp(key->ask_desc.asd_type, type, 4))) {
407 1.1 riastrad error = EINVAL;
408 1.1 riastrad goto fail;
409 1.1 riastrad }
410 1.1 riastrad
411 1.4 riastrad /* Success! */
412 1.1 riastrad *keyp = key;
413 1.1 riastrad return 0;
414 1.1 riastrad
415 1.1 riastrad fail: kmem_free(key, sizeof(*key));
416 1.1 riastrad return error;
417 1.1 riastrad }
418 1.1 riastrad
419 1.1 riastrad int
420 1.1 riastrad apple_smc_named_key(struct apple_smc_tag *smc, const char name[4 + 1],
421 1.1 riastrad const char type[4 + 1], struct apple_smc_key **keyp)
422 1.4 riastrad {
423 1.1 riastrad struct apple_smc_key *key;
424 1.1 riastrad int error;
425 1.1 riastrad
426 1.1 riastrad /* Paranoia: name must be 4 non-null characters long. */
427 1.4 riastrad KASSERT(name != NULL);
428 1.1 riastrad if (strlen(name) != 4)
429 1.1 riastrad return EINVAL;
430 1.1 riastrad
431 1.4 riastrad /* Paranoia: type must be NULL or 4 non-null characters long. */
432 1.1 riastrad if ((type != NULL) && (strlen(type) != 4))
433 1.1 riastrad return EINVAL;
434 1.1 riastrad
435 1.1 riastrad /* Create a new key. XXX Consider caching these. */
436 1.4 riastrad key = kmem_alloc(sizeof(*key), KM_SLEEP);
437 1.4 riastrad #ifdef DIAGNOSTIC
438 1.1 riastrad key->ask_smc = smc;
439 1.1 riastrad #endif
440 1.1 riastrad
441 1.4 riastrad /* Use the specified name, and make sure it's null-terminated. */
442 1.1 riastrad (void)memcpy(key->ask_name, name, 4);
443 1.1 riastrad key->ask_name[4] = '\0';
444 1.1 riastrad
445 1.1 riastrad /* Ask the SMC for a description of this key by name. */
446 1.1 riastrad CTASSERT(sizeof(key->ask_desc) == 6);
447 1.1 riastrad error = apple_smc_input(smc, APPLE_SMC_CMD_KEY_DESC, key->ask_name,
448 1.4 riastrad &key->ask_desc, 6);
449 1.1 riastrad if (error)
450 1.1 riastrad goto fail;
451 1.1 riastrad
452 1.1 riastrad /* Fail with EINVAL if the types don't match. */
453 1.1 riastrad if ((type != NULL) && (0 != memcmp(key->ask_desc.asd_type, type, 4))) {
454 1.4 riastrad error = EINVAL;
455 1.1 riastrad goto fail;
456 1.1 riastrad }
457 1.1 riastrad
458 1.4 riastrad /* Success! */
459 1.1 riastrad *keyp = key;
460 1.1 riastrad return 0;
461 1.1 riastrad
462 1.1 riastrad fail: kmem_free(key, sizeof(*key));
463 1.1 riastrad return error;
464 1.1 riastrad }
465 1.1 riastrad
466 1.1 riastrad void
467 1.4 riastrad apple_smc_release_key(struct apple_smc_tag *smc, struct apple_smc_key *key)
468 1.1 riastrad {
469 1.1 riastrad
470 1.1 riastrad #ifdef DIAGNOSTIC
471 1.1 riastrad /* Make sure the caller didn't mix up SMC tags. */
472 1.1 riastrad if (key->ask_smc != smc)
473 1.4 riastrad aprint_error_dev(smc->smc_dev,
474 1.4 riastrad "releasing key with wrong tag: %p != %p",
475 1.1 riastrad smc, key->ask_smc);
476 1.1 riastrad #endif
477 1.1 riastrad
478 1.1 riastrad /* Nothing to do but free the key's memory. */
479 1.1 riastrad kmem_free(key, sizeof(*key));
480 1.1 riastrad }
481 1.1 riastrad
482 1.1 riastrad int
483 1.1 riastrad apple_smc_key_search(struct apple_smc_tag *smc, const char *name,
484 1.6 riastrad uint32_t *result)
485 1.1 riastrad {
486 1.1 riastrad struct apple_smc_key *key;
487 1.4 riastrad uint32_t start = 0, end = apple_smc_nkeys(smc), median;
488 1.1 riastrad int cmp;
489 1.1 riastrad int error;
490 1.1 riastrad
491 1.1 riastrad /* Do a binary search on the SMC's key space. */
492 1.1 riastrad while (start < end) {
493 1.1 riastrad median = (start + ((end - start) / 2));
494 1.6 riastrad error = apple_smc_nth_key(smc, median, NULL, &key);
495 1.6 riastrad if (error)
496 1.1 riastrad return error;
497 1.6 riastrad
498 1.6 riastrad cmp = memcmp(name, apple_smc_key_name(key), 4);
499 1.1 riastrad if (cmp < 0)
500 1.6 riastrad end = median;
501 1.6 riastrad else if (cmp > 0)
502 1.1 riastrad start = (median + 1);
503 1.1 riastrad else
504 1.1 riastrad start = end = median; /* stop here */
505 1.4 riastrad
506 1.1 riastrad apple_smc_release_key(smc, key);
507 1.1 riastrad }
508 1.1 riastrad
509 1.2 riastrad /* Success! */
510 1.1 riastrad *result = start;
511 1.1 riastrad return 0;
512 1.1 riastrad }
513 1.1 riastrad
514 1.1 riastrad int
516 1.1 riastrad apple_smc_read_key(struct apple_smc_tag *smc, const struct apple_smc_key *key,
517 1.1 riastrad void *buffer, uint8_t size)
518 1.4 riastrad {
519 1.4 riastrad
520 1.1 riastrad /* Refuse if software and hardware disagree on the key's size. */
521 1.1 riastrad if (key->ask_desc.asd_size != size)
522 1.1 riastrad return EINVAL;
523 1.4 riastrad
524 1.1 riastrad /* Refuse if the hardware doesn't want us to read it. */
525 1.1 riastrad if (!(key->ask_desc.asd_flags & APPLE_SMC_FLAG_READ))
526 1.1 riastrad return EACCES;
527 1.1 riastrad
528 1.1 riastrad /* Looks good. Try reading it from the hardware. */
529 1.1 riastrad return apple_smc_input(smc, APPLE_SMC_CMD_READ_KEY, key->ask_name,
530 1.1 riastrad buffer, size);
531 1.1 riastrad }
532 1.1 riastrad
533 1.1 riastrad int
534 1.1 riastrad apple_smc_read_key_1(struct apple_smc_tag *smc,
535 1.1 riastrad const struct apple_smc_key *key, uint8_t *p)
536 1.1 riastrad {
537 1.1 riastrad
538 1.1 riastrad return apple_smc_read_key(smc, key, p, 1);
539 1.1 riastrad }
540 1.1 riastrad
541 1.1 riastrad int
542 1.1 riastrad apple_smc_read_key_2(struct apple_smc_tag *smc,
543 1.4 riastrad const struct apple_smc_key *key, uint16_t *p)
544 1.1 riastrad {
545 1.1 riastrad uint16_t be;
546 1.1 riastrad int error;
547 1.1 riastrad
548 1.4 riastrad /* Read a big-endian quantity from the hardware. */
549 1.1 riastrad error = apple_smc_read_key(smc, key, &be, 2);
550 1.4 riastrad if (error)
551 1.4 riastrad return error;
552 1.1 riastrad
553 1.1 riastrad /* Convert it to host order. */
554 1.1 riastrad *p = be16toh(be);
555 1.1 riastrad
556 1.1 riastrad /* Success! */
557 1.1 riastrad return 0;
558 1.1 riastrad }
559 1.1 riastrad
560 1.1 riastrad int
561 1.1 riastrad apple_smc_read_key_4(struct apple_smc_tag *smc,
562 1.4 riastrad const struct apple_smc_key *key, uint32_t *p)
563 1.1 riastrad {
564 1.1 riastrad uint32_t be;
565 1.1 riastrad int error;
566 1.1 riastrad
567 1.4 riastrad /* Read a big-endian quantity from the hardware. */
568 1.1 riastrad error = apple_smc_read_key(smc, key, &be, 4);
569 1.4 riastrad if (error)
570 1.4 riastrad return error;
571 1.1 riastrad
572 1.1 riastrad /* Convert it to host order. */
573 1.1 riastrad *p = be32toh(be);
574 1.1 riastrad
575 1.1 riastrad /* Success! */
576 1.1 riastrad return 0;
577 1.1 riastrad }
578 1.1 riastrad
579 1.4 riastrad int
580 1.1 riastrad apple_smc_write_key(struct apple_smc_tag *smc, const struct apple_smc_key *key,
581 1.1 riastrad const void *buffer, uint8_t size)
582 1.4 riastrad {
583 1.4 riastrad
584 1.1 riastrad /* Refuse if software and hardware disagree on the key's size. */
585 1.1 riastrad if (key->ask_desc.asd_size != size)
586 1.1 riastrad return EINVAL;
587 1.4 riastrad
588 1.1 riastrad /* Refuse if the hardware doesn't want us to write it. */
589 1.1 riastrad if (!(key->ask_desc.asd_flags & APPLE_SMC_FLAG_WRITE))
590 1.1 riastrad return EACCES;
591 1.1 riastrad
592 1.1 riastrad /* Looks good. Try writing it to the hardware. */
593 1.1 riastrad return apple_smc_output(smc, APPLE_SMC_CMD_WRITE_KEY, key->ask_name,
594 1.1 riastrad buffer, size);
595 1.1 riastrad }
596 1.1 riastrad
597 1.1 riastrad int
598 1.1 riastrad apple_smc_write_key_1(struct apple_smc_tag *smc,
599 1.1 riastrad const struct apple_smc_key *key, uint8_t v)
600 1.1 riastrad {
601 1.1 riastrad
602 1.1 riastrad return apple_smc_write_key(smc, key, &v, 1);
603 1.1 riastrad }
604 1.4 riastrad
605 1.1 riastrad int
606 1.1 riastrad apple_smc_write_key_2(struct apple_smc_tag *smc,
607 1.4 riastrad const struct apple_smc_key *key, uint16_t v)
608 1.1 riastrad {
609 1.1 riastrad /* Convert the quantity from host to big-endian byte order. */
610 1.1 riastrad const uint16_t v_be = htobe16(v);
611 1.1 riastrad
612 1.1 riastrad /* Write the big-endian quantity to the hardware. */
613 1.1 riastrad return apple_smc_write_key(smc, key, &v_be, 2);
614 1.1 riastrad }
615 1.4 riastrad
616 1.5 riastrad int
617 1.1 riastrad apple_smc_write_key_4(struct apple_smc_tag *smc,
618 1.4 riastrad const struct apple_smc_key *key, uint32_t v)
619 1.1 riastrad {
620 1.1 riastrad /* Convert the quantity from host to big-endian byte order. */
621 1.2 riastrad const uint32_t v_be = htobe32(v);
622 1.2 riastrad
623 1.2 riastrad /* Write the big-endian quantity to the hardware. */
624 1.2 riastrad return apple_smc_write_key(smc, key, &v_be, 4);
625 1.2 riastrad }
626 1.2 riastrad
627 1.2 riastrad MODULE(MODULE_CLASS_MISC, apple_smc, NULL)
629 1.2 riastrad
630 1.2 riastrad static int
631 1.3 riastrad apple_smc_modcmd(modcmd_t cmd, void *data __unused)
632 1.2 riastrad {
633 1.2 riastrad
634 1.3 riastrad /* Nothing to do for now to set up or tear down the module. */
635 1.2 riastrad switch (cmd) {
636 1.2 riastrad case MODULE_CMD_INIT:
637 1.2 riastrad return 0;
638 1.2 riastrad
639 1.2 riastrad case MODULE_CMD_FINI:
640 return 0;
641
642 default:
643 return ENOTTY;
644 }
645 }
646