xref: /freebsd/sys/dev/efidev/efirt.c (revision ab393e9548f8cc0ee28499c411963b798ebb38a5)
1 /*-
2  * Copyright (c) 2004 Marcel Moolenaar
3  * Copyright (c) 2001 Doug Rabson
4  * Copyright (c) 2016, 2018 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * Portions of this software were developed by Konstantin Belousov
8  * under sponsorship from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #include "opt_acpi.h"
34 
35 #include <sys/param.h>
36 #include <sys/efi.h>
37 #include <sys/eventhandler.h>
38 #include <sys/kernel.h>
39 #include <sys/linker.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/msan.h>
44 #include <sys/mutex.h>
45 #include <sys/clock.h>
46 #include <sys/proc.h>
47 #include <sys/reboot.h>
48 #include <sys/rwlock.h>
49 #include <sys/sched.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
52 #include <sys/uio.h>
53 #include <sys/vmmeter.h>
54 
55 #include <machine/fpu.h>
56 #include <machine/efi.h>
57 #include <machine/metadata.h>
58 #include <machine/vmparam.h>
59 
60 #include <vm/vm.h>
61 #include <vm/pmap.h>
62 #include <vm/vm_map.h>
63 
64 #ifdef DEV_ACPI
65 #include <contrib/dev/acpica/include/acpi.h>
66 #endif
67 
68 #define EFI_TABLE_ALLOC_MAX 0x800000
69 
70 static struct efi_systbl *efi_systbl;
71 static eventhandler_tag efi_shutdown_tag;
72 /*
73  * The following pointers point to tables in the EFI runtime service data pages.
74  * Care should be taken to make sure that we've properly entered the EFI runtime
75  * environment (efi_enter()) before dereferencing them.
76  */
77 static struct efi_cfgtbl *efi_cfgtbl;
78 static struct efi_rt *efi_runtime;
79 
80 static int efi_status2err[25] = {
81 	0,		/* EFI_SUCCESS */
82 	ENOEXEC,	/* EFI_LOAD_ERROR */
83 	EINVAL,		/* EFI_INVALID_PARAMETER */
84 	ENOSYS,		/* EFI_UNSUPPORTED */
85 	EMSGSIZE, 	/* EFI_BAD_BUFFER_SIZE */
86 	EOVERFLOW,	/* EFI_BUFFER_TOO_SMALL */
87 	EBUSY,		/* EFI_NOT_READY */
88 	EIO,		/* EFI_DEVICE_ERROR */
89 	EROFS,		/* EFI_WRITE_PROTECTED */
90 	EAGAIN,		/* EFI_OUT_OF_RESOURCES */
91 	EIO,		/* EFI_VOLUME_CORRUPTED */
92 	ENOSPC,		/* EFI_VOLUME_FULL */
93 	ENXIO,		/* EFI_NO_MEDIA */
94 	ESTALE,		/* EFI_MEDIA_CHANGED */
95 	ENOENT,		/* EFI_NOT_FOUND */
96 	EACCES,		/* EFI_ACCESS_DENIED */
97 	ETIMEDOUT,	/* EFI_NO_RESPONSE */
98 	EADDRNOTAVAIL,	/* EFI_NO_MAPPING */
99 	ETIMEDOUT,	/* EFI_TIMEOUT */
100 	EDOOFUS,	/* EFI_NOT_STARTED */
101 	EALREADY,	/* EFI_ALREADY_STARTED */
102 	ECANCELED,	/* EFI_ABORTED */
103 	EPROTO,		/* EFI_ICMP_ERROR */
104 	EPROTO,		/* EFI_TFTP_ERROR */
105 	EPROTO		/* EFI_PROTOCOL_ERROR */
106 };
107 
108 enum efi_table_type {
109 	TYPE_ESRT = 0,
110 	TYPE_PROP
111 };
112 
113 static int efi_enter(void);
114 static void efi_leave(void);
115 
116 int
117 efi_status_to_errno(efi_status status)
118 {
119 	u_long code;
120 
121 	code = status & 0x3ffffffffffffffful;
122 	return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS);
123 }
124 
125 static struct mtx efi_lock;
126 static SYSCTL_NODE(_hw, OID_AUTO, efi, CTLFLAG_RWTUN | CTLFLAG_MPSAFE, NULL,
127     "EFI");
128 static bool efi_poweroff = true;
129 SYSCTL_BOOL(_hw_efi, OID_AUTO, poweroff, CTLFLAG_RWTUN, &efi_poweroff, 0,
130     "If true, use EFI runtime services to power off in preference to ACPI");
131 
132 static bool
133 efi_is_in_map(struct efi_md *map, int ndesc, int descsz, vm_offset_t addr)
134 {
135 	struct efi_md *p;
136 	int i;
137 
138 	for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p,
139 	    descsz)) {
140 		if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
141 			continue;
142 
143 		if (addr >= p->md_virt &&
144 		    addr < p->md_virt + p->md_pages * EFI_PAGE_SIZE)
145 			return (true);
146 	}
147 
148 	return (false);
149 }
150 
151 static void
152 efi_shutdown_final(void *dummy __unused, int howto)
153 {
154 
155 	/*
156 	 * On some systems, ACPI S5 is missing or does not function properly.
157 	 * When present, shutdown via EFI Runtime Services instead, unless
158 	 * disabled.
159 	 */
160 	if ((howto & RB_POWEROFF) != 0 && efi_poweroff)
161 		(void)efi_reset_system(EFI_RESET_SHUTDOWN);
162 }
163 
164 static int
165 efi_init(void)
166 {
167 	struct efi_map_header *efihdr;
168 	struct efi_md *map;
169 	struct efi_rt *rtdm;
170 	caddr_t kmdp;
171 	size_t efisz;
172 	int ndesc, rt_disabled;
173 
174 	rt_disabled = 0;
175 	TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled);
176 	if (rt_disabled == 1)
177 		return (0);
178 	mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
179 
180 	if (efi_systbl_phys == 0) {
181 		if (bootverbose)
182 			printf("EFI systbl not available\n");
183 		return (0);
184 	}
185 
186 	efi_systbl = (struct efi_systbl *)efi_phys_to_kva(efi_systbl_phys);
187 	if (efi_systbl == NULL || efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
188 		efi_systbl = NULL;
189 		if (bootverbose)
190 			printf("EFI systbl signature invalid\n");
191 		return (0);
192 	}
193 	efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
194 	    (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
195 	if (efi_cfgtbl == NULL) {
196 		if (bootverbose)
197 			printf("EFI config table is not present\n");
198 	}
199 
200 	kmdp = preload_search_by_type("elf kernel");
201 	if (kmdp == NULL)
202 		kmdp = preload_search_by_type("elf64 kernel");
203 	efihdr = (struct efi_map_header *)preload_search_info(kmdp,
204 	    MODINFO_METADATA | MODINFOMD_EFI_MAP);
205 	if (efihdr == NULL) {
206 		if (bootverbose)
207 			printf("EFI map is not present\n");
208 		return (0);
209 	}
210 	efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
211 	map = (struct efi_md *)((uint8_t *)efihdr + efisz);
212 	if (efihdr->descriptor_size == 0)
213 		return (ENOMEM);
214 
215 	ndesc = efihdr->memory_size / efihdr->descriptor_size;
216 	if (!efi_create_1t1_map(map, ndesc, efihdr->descriptor_size)) {
217 		if (bootverbose)
218 			printf("EFI cannot create runtime map\n");
219 		return (ENOMEM);
220 	}
221 
222 	efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
223 	    (struct efi_rt *)efi_systbl->st_rt;
224 	if (efi_runtime == NULL) {
225 		if (bootverbose)
226 			printf("EFI runtime services table is not present\n");
227 		efi_destroy_1t1_map();
228 		return (ENXIO);
229 	}
230 
231 #if defined(__aarch64__) || defined(__amd64__)
232 	/*
233 	 * Some UEFI implementations have multiple implementations of the
234 	 * RS->GetTime function. They switch from one we can only use early
235 	 * in the boot process to one valid as a RunTime service only when we
236 	 * call RS->SetVirtualAddressMap. As this is not always the case, e.g.
237 	 * with an old loader.efi, check if the RS->GetTime function is within
238 	 * the EFI map, and fail to attach if not.
239 	 */
240 	rtdm = (struct efi_rt *)efi_phys_to_kva((uintptr_t)efi_runtime);
241 	if (rtdm == NULL || !efi_is_in_map(map, ndesc, efihdr->descriptor_size,
242 	    (vm_offset_t)rtdm->rt_gettime)) {
243 		if (bootverbose)
244 			printf(
245 			 "EFI runtime services table has an invalid pointer\n");
246 		efi_runtime = NULL;
247 		efi_destroy_1t1_map();
248 		return (ENXIO);
249 	}
250 #endif
251 
252 	/*
253 	 * We use SHUTDOWN_PRI_LAST - 1 to trigger after IPMI, but before ACPI.
254 	 */
255 	efi_shutdown_tag = EVENTHANDLER_REGISTER(shutdown_final,
256 	    efi_shutdown_final, NULL, SHUTDOWN_PRI_LAST - 1);
257 
258 	return (0);
259 }
260 
261 static void
262 efi_uninit(void)
263 {
264 
265 	/* Most likely disabled by tunable */
266 	if (efi_runtime == NULL)
267 		return;
268 	if (efi_shutdown_tag != NULL)
269 		EVENTHANDLER_DEREGISTER(shutdown_final, efi_shutdown_tag);
270 	efi_destroy_1t1_map();
271 
272 	efi_systbl = NULL;
273 	efi_cfgtbl = NULL;
274 	efi_runtime = NULL;
275 
276 	mtx_destroy(&efi_lock);
277 }
278 
279 static int
280 rt_ok(void)
281 {
282 
283 	if (efi_runtime == NULL)
284 		return (ENXIO);
285 	return (0);
286 }
287 
288 /*
289  * The fpu_kern_enter() call in allows firmware to use FPU, as
290  * mandated by the specification.  It also enters a critical section,
291  * giving us neccessary protection against context switches.
292  */
293 static int
294 efi_enter(void)
295 {
296 	struct thread *td;
297 	pmap_t curpmap;
298 	int error;
299 
300 	if (efi_runtime == NULL)
301 		return (ENXIO);
302 	td = curthread;
303 	curpmap = &td->td_proc->p_vmspace->vm_pmap;
304 	PMAP_LOCK(curpmap);
305 	mtx_lock(&efi_lock);
306 	fpu_kern_enter(td, NULL, FPU_KERN_NOCTX);
307 	error = efi_arch_enter();
308 	if (error != 0) {
309 		fpu_kern_leave(td, NULL);
310 		mtx_unlock(&efi_lock);
311 		PMAP_UNLOCK(curpmap);
312 	}
313 	return (error);
314 }
315 
316 static void
317 efi_leave(void)
318 {
319 	struct thread *td;
320 	pmap_t curpmap;
321 
322 	efi_arch_leave();
323 
324 	curpmap = &curproc->p_vmspace->vm_pmap;
325 	td = curthread;
326 	fpu_kern_leave(td, NULL);
327 	mtx_unlock(&efi_lock);
328 	PMAP_UNLOCK(curpmap);
329 }
330 
331 static int
332 get_table(struct uuid *uuid, void **ptr)
333 {
334 	struct efi_cfgtbl *ct;
335 	u_long count;
336 	int error;
337 
338 	if (efi_cfgtbl == NULL || efi_systbl == NULL)
339 		return (ENXIO);
340 	error = efi_enter();
341 	if (error != 0)
342 		return (error);
343 	count = efi_systbl->st_entries;
344 	ct = efi_cfgtbl;
345 	while (count--) {
346 		if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
347 			*ptr = ct->ct_data;
348 			efi_leave();
349 			return (0);
350 		}
351 		ct++;
352 	}
353 
354 	efi_leave();
355 	return (ENOENT);
356 }
357 
358 static int
359 get_table_length(enum efi_table_type type, size_t *table_len, void **taddr)
360 {
361 	switch (type) {
362 	case TYPE_ESRT:
363 	{
364 		struct efi_esrt_table *esrt = NULL;
365 		struct uuid uuid = EFI_TABLE_ESRT;
366 		uint32_t fw_resource_count = 0;
367 		size_t len = sizeof(*esrt);
368 		int error;
369 		void *buf;
370 
371 		error = efi_get_table(&uuid, (void **)&esrt);
372 		if (error != 0)
373 			return (error);
374 
375 		buf = malloc(len, M_TEMP, M_WAITOK);
376 		error = physcopyout((vm_paddr_t)esrt, buf, len);
377 		if (error != 0) {
378 			free(buf, M_TEMP);
379 			return (error);
380 		}
381 
382 		/* Check ESRT version */
383 		if (((struct efi_esrt_table *)buf)->fw_resource_version !=
384 		    ESRT_FIRMWARE_RESOURCE_VERSION) {
385 			free(buf, M_TEMP);
386 			return (ENODEV);
387 		}
388 
389 		fw_resource_count = ((struct efi_esrt_table *)buf)->
390 		    fw_resource_count;
391 		if (fw_resource_count > EFI_TABLE_ALLOC_MAX /
392 		    sizeof(struct efi_esrt_entry_v1)) {
393 			free(buf, M_TEMP);
394 			return (ENOMEM);
395 		}
396 
397 		len += fw_resource_count * sizeof(struct efi_esrt_entry_v1);
398 		*table_len = len;
399 
400 		if (taddr != NULL)
401 			*taddr = esrt;
402 		free(buf, M_TEMP);
403 		return (0);
404 	}
405 	case TYPE_PROP:
406 	{
407 		struct uuid uuid = EFI_PROPERTIES_TABLE;
408 		struct efi_prop_table *prop;
409 		size_t len = sizeof(*prop);
410 		uint32_t prop_len;
411 		int error;
412 		void *buf;
413 
414 		error = efi_get_table(&uuid, (void **)&prop);
415 		if (error != 0)
416 			return (error);
417 
418 		buf = malloc(len, M_TEMP, M_WAITOK);
419 		error = physcopyout((vm_paddr_t)prop, buf, len);
420 		if (error != 0) {
421 			free(buf, M_TEMP);
422 			return (error);
423 		}
424 
425 		prop_len = ((struct efi_prop_table *)buf)->length;
426 		if (prop_len > EFI_TABLE_ALLOC_MAX) {
427 			free(buf, M_TEMP);
428 			return (ENOMEM);
429 		}
430 		*table_len = prop_len;
431 
432 		if (taddr != NULL)
433 			*taddr = prop;
434 		free(buf, M_TEMP);
435 		return (0);
436 	}
437 	}
438 	return (ENOENT);
439 }
440 
441 static int
442 copy_table(struct uuid *uuid, void **buf, size_t buf_len, size_t *table_len)
443 {
444 	static const struct known_table {
445 		struct uuid uuid;
446 		enum efi_table_type type;
447 	} tables[] = {
448 		{ EFI_TABLE_ESRT,       TYPE_ESRT },
449 		{ EFI_PROPERTIES_TABLE, TYPE_PROP }
450 	};
451 	size_t table_idx;
452 	void *taddr;
453 	int rc;
454 
455 	for (table_idx = 0; table_idx < nitems(tables); table_idx++) {
456 		if (!bcmp(&tables[table_idx].uuid, uuid, sizeof(*uuid)))
457 			break;
458 	}
459 
460 	if (table_idx == nitems(tables))
461 		return (EINVAL);
462 
463 	rc = get_table_length(tables[table_idx].type, table_len, &taddr);
464 	if (rc != 0)
465 		return rc;
466 
467 	/* return table length to userspace */
468 	if (buf == NULL)
469 		return (0);
470 
471 	*buf = malloc(*table_len, M_TEMP, M_WAITOK);
472 	rc = physcopyout((vm_paddr_t)taddr, *buf, *table_len);
473 	return (rc);
474 }
475 
476 static int efi_rt_handle_faults = EFI_RT_HANDLE_FAULTS_DEFAULT;
477 SYSCTL_INT(_machdep, OID_AUTO, efi_rt_handle_faults, CTLFLAG_RWTUN,
478     &efi_rt_handle_faults, 0,
479     "Call EFI RT methods with fault handler wrapper around");
480 
481 static int
482 efi_rt_arch_call_nofault(struct efirt_callinfo *ec)
483 {
484 
485 	switch (ec->ec_argcnt) {
486 	case 0:
487 		ec->ec_efi_status = ((register_t (*)(void))ec->ec_fptr)();
488 		break;
489 	case 1:
490 		ec->ec_efi_status = ((register_t (*)(register_t))ec->ec_fptr)
491 		    (ec->ec_arg1);
492 		break;
493 	case 2:
494 		ec->ec_efi_status = ((register_t (*)(register_t, register_t))
495 		    ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2);
496 		break;
497 	case 3:
498 		ec->ec_efi_status = ((register_t (*)(register_t, register_t,
499 		    register_t))ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2,
500 		    ec->ec_arg3);
501 		break;
502 	case 4:
503 		ec->ec_efi_status = ((register_t (*)(register_t, register_t,
504 		    register_t, register_t))ec->ec_fptr)(ec->ec_arg1,
505 		    ec->ec_arg2, ec->ec_arg3, ec->ec_arg4);
506 		break;
507 	case 5:
508 		ec->ec_efi_status = ((register_t (*)(register_t, register_t,
509 		    register_t, register_t, register_t))ec->ec_fptr)(
510 		    ec->ec_arg1, ec->ec_arg2, ec->ec_arg3, ec->ec_arg4,
511 		    ec->ec_arg5);
512 		break;
513 	default:
514 		panic("efi_rt_arch_call: %d args", (int)ec->ec_argcnt);
515 	}
516 
517 	return (0);
518 }
519 
520 static int
521 efi_call(struct efirt_callinfo *ecp)
522 {
523 	int error;
524 
525 	error = efi_enter();
526 	if (error != 0)
527 		return (error);
528 	error = efi_rt_handle_faults ? efi_rt_arch_call(ecp) :
529 	    efi_rt_arch_call_nofault(ecp);
530 	efi_leave();
531 	if (error == 0)
532 		error = efi_status_to_errno(ecp->ec_efi_status);
533 	else if (bootverbose)
534 		printf("EFI %s call faulted, error %d\n", ecp->ec_name, error);
535 	return (error);
536 }
537 
538 #define	EFI_RT_METHOD_PA(method)				\
539     ((uintptr_t)((struct efi_rt *)efi_phys_to_kva((uintptr_t)	\
540     efi_runtime))->method)
541 
542 static int
543 efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap)
544 {
545 	struct efirt_callinfo ec;
546 	int error;
547 
548 	EFI_TIME_OWNED();
549 	if (efi_runtime == NULL)
550 		return (ENXIO);
551 	bzero(&ec, sizeof(ec));
552 	ec.ec_name = "rt_gettime";
553 	ec.ec_argcnt = 2;
554 	ec.ec_arg1 = (uintptr_t)tm;
555 	ec.ec_arg2 = (uintptr_t)tmcap;
556 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_gettime);
557 	error = efi_call(&ec);
558 	if (error == 0)
559 		kmsan_mark(tm, sizeof(*tm), KMSAN_STATE_INITED);
560 	return (error);
561 }
562 
563 static int
564 get_time(struct efi_tm *tm)
565 {
566 	struct efi_tmcap dummy;
567 	int error;
568 
569 	if (efi_runtime == NULL)
570 		return (ENXIO);
571 	EFI_TIME_LOCK();
572 	/*
573 	 * UEFI spec states that the Capabilities argument to GetTime is
574 	 * optional, but some UEFI implementations choke when passed a NULL
575 	 * pointer. Pass a dummy efi_tmcap, even though we won't use it,
576 	 * to workaround such implementations.
577 	 */
578 	error = efi_get_time_locked(tm, &dummy);
579 	EFI_TIME_UNLOCK();
580 	return (error);
581 }
582 
583 static int
584 get_waketime(uint8_t *enabled, uint8_t *pending, struct efi_tm *tm)
585 {
586 	struct efirt_callinfo ec;
587 	int error;
588 #ifdef DEV_ACPI
589 	UINT32 acpiRtcEnabled;
590 #endif
591 
592 	if (efi_runtime == NULL)
593 		return (ENXIO);
594 
595 	EFI_TIME_LOCK();
596 	bzero(&ec, sizeof(ec));
597 	ec.ec_name = "rt_getwaketime";
598 	ec.ec_argcnt = 3;
599 	ec.ec_arg1 = (uintptr_t)enabled;
600 	ec.ec_arg2 = (uintptr_t)pending;
601 	ec.ec_arg3 = (uintptr_t)tm;
602 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_getwaketime);
603 	error = efi_call(&ec);
604 	EFI_TIME_UNLOCK();
605 
606 #ifdef DEV_ACPI
607 	if (error == 0) {
608 		error = AcpiReadBitRegister(ACPI_BITREG_RT_CLOCK_ENABLE,
609 		    &acpiRtcEnabled);
610 		if (ACPI_SUCCESS(error)) {
611 			*enabled = *enabled && acpiRtcEnabled;
612 		} else
613 			error = EIO;
614 	}
615 #endif
616 
617 	return (error);
618 }
619 
620 static int
621 set_waketime(uint8_t enable, struct efi_tm *tm)
622 {
623 	struct efirt_callinfo ec;
624 	int error;
625 
626 	if (efi_runtime == NULL)
627 		return (ENXIO);
628 
629 	EFI_TIME_LOCK();
630 	bzero(&ec, sizeof(ec));
631 	ec.ec_name = "rt_setwaketime";
632 	ec.ec_argcnt = 2;
633 	ec.ec_arg1 = (uintptr_t)enable;
634 	ec.ec_arg2 = (uintptr_t)tm;
635 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_setwaketime);
636 	error = efi_call(&ec);
637 	EFI_TIME_UNLOCK();
638 
639 #ifdef DEV_ACPI
640 	if (error == 0) {
641 		error = AcpiWriteBitRegister(ACPI_BITREG_RT_CLOCK_ENABLE,
642 		    (enable != 0) ? 1 : 0);
643 		if (ACPI_FAILURE(error))
644 			error = EIO;
645 	}
646 #endif
647 
648 	return (error);
649 }
650 
651 static int
652 get_time_capabilities(struct efi_tmcap *tmcap)
653 {
654 	struct efi_tm dummy;
655 	int error;
656 
657 	if (efi_runtime == NULL)
658 		return (ENXIO);
659 	EFI_TIME_LOCK();
660 	error = efi_get_time_locked(&dummy, tmcap);
661 	EFI_TIME_UNLOCK();
662 	return (error);
663 }
664 
665 static int
666 reset_system(enum efi_reset type)
667 {
668 	struct efirt_callinfo ec;
669 
670 	switch (type) {
671 	case EFI_RESET_COLD:
672 	case EFI_RESET_WARM:
673 	case EFI_RESET_SHUTDOWN:
674 		break;
675 	default:
676 		return (EINVAL);
677 	}
678 	if (efi_runtime == NULL)
679 		return (ENXIO);
680 	bzero(&ec, sizeof(ec));
681 	ec.ec_name = "rt_reset";
682 	ec.ec_argcnt = 4;
683 	ec.ec_arg1 = (uintptr_t)type;
684 	ec.ec_arg2 = (uintptr_t)0;
685 	ec.ec_arg3 = (uintptr_t)0;
686 	ec.ec_arg4 = (uintptr_t)NULL;
687 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_reset);
688 	return (efi_call(&ec));
689 }
690 
691 static int
692 efi_set_time_locked(struct efi_tm *tm)
693 {
694 	struct efirt_callinfo ec;
695 
696 	EFI_TIME_OWNED();
697 	if (efi_runtime == NULL)
698 		return (ENXIO);
699 	bzero(&ec, sizeof(ec));
700 	ec.ec_name = "rt_settime";
701 	ec.ec_argcnt = 1;
702 	ec.ec_arg1 = (uintptr_t)tm;
703 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_settime);
704 	return (efi_call(&ec));
705 }
706 
707 static int
708 set_time(struct efi_tm *tm)
709 {
710 	int error;
711 
712 	if (efi_runtime == NULL)
713 		return (ENXIO);
714 	EFI_TIME_LOCK();
715 	error = efi_set_time_locked(tm);
716 	EFI_TIME_UNLOCK();
717 	return (error);
718 }
719 
720 static int
721 var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
722     size_t *datasize, void *data)
723 {
724 	struct efirt_callinfo ec;
725 	int error;
726 
727 	if (efi_runtime == NULL)
728 		return (ENXIO);
729 	bzero(&ec, sizeof(ec));
730 	ec.ec_argcnt = 5;
731 	ec.ec_name = "rt_getvar";
732 	ec.ec_arg1 = (uintptr_t)name;
733 	ec.ec_arg2 = (uintptr_t)vendor;
734 	ec.ec_arg3 = (uintptr_t)attrib;
735 	ec.ec_arg4 = (uintptr_t)datasize;
736 	ec.ec_arg5 = (uintptr_t)data;
737 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_getvar);
738 	error = efi_call(&ec);
739 	if (error == 0)
740 		kmsan_mark(data, *datasize, KMSAN_STATE_INITED);
741 	return (error);
742 }
743 
744 static int
745 var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
746 {
747 	struct efirt_callinfo ec;
748 	int error;
749 
750 	if (efi_runtime == NULL)
751 		return (ENXIO);
752 	bzero(&ec, sizeof(ec));
753 	ec.ec_argcnt = 3;
754 	ec.ec_name = "rt_scanvar";
755 	ec.ec_arg1 = (uintptr_t)namesize;
756 	ec.ec_arg2 = (uintptr_t)name;
757 	ec.ec_arg3 = (uintptr_t)vendor;
758 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_scanvar);
759 	error = efi_call(&ec);
760 	if (error == 0)
761 		kmsan_mark(name, *namesize, KMSAN_STATE_INITED);
762 	return (error);
763 }
764 
765 static int
766 var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
767     size_t datasize, void *data)
768 {
769 	struct efirt_callinfo ec;
770 
771 	if (efi_runtime == NULL)
772 		return (ENXIO);
773 	bzero(&ec, sizeof(ec));
774 	ec.ec_argcnt = 5;
775 	ec.ec_name = "rt_setvar";
776 	ec.ec_arg1 = (uintptr_t)name;
777 	ec.ec_arg2 = (uintptr_t)vendor;
778 	ec.ec_arg3 = (uintptr_t)attrib;
779 	ec.ec_arg4 = (uintptr_t)datasize;
780 	ec.ec_arg5 = (uintptr_t)data;
781 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_setvar);
782 	return (efi_call(&ec));
783 }
784 
785 const static struct efi_ops efi_ops = {
786 	.rt_ok = rt_ok,
787 	.get_table = get_table,
788 	.copy_table = copy_table,
789 	.get_time = get_time,
790 	.get_time_capabilities = get_time_capabilities,
791 	.reset_system = reset_system,
792 	.set_time = set_time,
793 	.get_waketime = get_waketime,
794 	.set_waketime = set_waketime,
795 	.var_get = var_get,
796 	.var_nextname = var_nextname,
797 	.var_set = var_set,
798 };
799 const struct efi_ops *active_efi_ops = &efi_ops;
800 
801 static int
802 efirt_modevents(module_t m, int event, void *arg __unused)
803 {
804 
805 	switch (event) {
806 	case MOD_LOAD:
807 		return (efi_init());
808 
809 	case MOD_UNLOAD:
810 		efi_uninit();
811 		return (0);
812 
813 	case MOD_SHUTDOWN:
814 		return (0);
815 
816 	default:
817 		return (EOPNOTSUPP);
818 	}
819 }
820 
821 static moduledata_t efirt_moddata = {
822 	.name = "efirt",
823 	.evhand = efirt_modevents,
824 	.priv = NULL,
825 };
826 /* After fpuinitstate, before efidev */
827 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND);
828 MODULE_VERSION(efirt, 1);
829