11.1Sriastrad/*	$NetBSD: acpi_hed.c,v 1.1 2024/03/21 02:36:01 riastradh Exp $	*/
21.1Sriastrad
31.1Sriastrad/*-
41.1Sriastrad * Copyright (c) 2024 The NetBSD Foundation, Inc.
51.1Sriastrad * All rights reserved.
61.1Sriastrad *
71.1Sriastrad * Redistribution and use in source and binary forms, with or without
81.1Sriastrad * modification, are permitted provided that the following conditions
91.1Sriastrad * are met:
101.1Sriastrad * 1. Redistributions of source code must retain the above copyright
111.1Sriastrad *    notice, this list of conditions and the following disclaimer.
121.1Sriastrad * 2. Redistributions in binary form must reproduce the above copyright
131.1Sriastrad *    notice, this list of conditions and the following disclaimer in the
141.1Sriastrad *    documentation and/or other materials provided with the distribution.
151.1Sriastrad *
161.1Sriastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171.1Sriastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181.1Sriastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191.1Sriastrad * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201.1Sriastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211.1Sriastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221.1Sriastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231.1Sriastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241.1Sriastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251.1Sriastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261.1Sriastrad * POSSIBILITY OF SUCH DAMAGE.
271.1Sriastrad */
281.1Sriastrad
291.1Sriastrad/*
301.1Sriastrad * HED: Hardware Error Device, PNP0C33.
311.1Sriastrad *
321.1Sriastrad * This device serves only to receive notifications about hardware
331.1Sriastrad * errors, which we then dispatch to apei(4).
341.1Sriastrad */
351.1Sriastrad
361.1Sriastrad#include <sys/cdefs.h>
371.1Sriastrad__KERNEL_RCSID(0, "$NetBSD: acpi_hed.c,v 1.1 2024/03/21 02:36:01 riastradh Exp $");
381.1Sriastrad
391.1Sriastrad#include <sys/device.h>
401.1Sriastrad#include <sys/module.h>
411.1Sriastrad
421.1Sriastrad#include <dev/acpi/acpireg.h>
431.1Sriastrad#include <dev/acpi/acpivar.h>
441.1Sriastrad#include <dev/acpi/apei_hed.h>
451.1Sriastrad
461.1Sriastrad#define	_COMPONENT		ACPI_RESOURCE_COMPONENT
471.1SriastradACPI_MODULE_NAME		("acpi_hed")
481.1Sriastrad
491.1Sriastradstruct acpihed_softc {
501.1Sriastrad	device_t		sc_dev;
511.1Sriastrad	struct acpi_devnode	*sc_node;
521.1Sriastrad};
531.1Sriastrad
541.1Sriastradstatic const struct device_compatible_entry compat_data[] = {
551.1Sriastrad	{ .compat = "PNP0C33" },
561.1Sriastrad	DEVICE_COMPAT_EOL
571.1Sriastrad};
581.1Sriastrad
591.1Sriastradstatic int acpihed_match(device_t, cfdata_t, void *);
601.1Sriastradstatic void acpihed_attach(device_t, device_t, void *);
611.1Sriastradstatic int acpihed_detach(device_t, int);
621.1Sriastradstatic void acpihed_notify(ACPI_HANDLE, uint32_t, void *);
631.1Sriastrad
641.1SriastradCFATTACH_DECL_NEW(acpihed, sizeof(struct acpihed_softc),
651.1Sriastrad    acpihed_match, acpihed_attach, acpihed_detach, NULL);
661.1Sriastrad
671.1Sriastradstatic int
681.1Sriastradacpihed_match(device_t parent, cfdata_t match, void *aux)
691.1Sriastrad{
701.1Sriastrad	struct acpi_attach_args *aa = aux;
711.1Sriastrad
721.1Sriastrad	return acpi_compatible_match(aa, compat_data);
731.1Sriastrad}
741.1Sriastrad
751.1Sriastradstatic void
761.1Sriastradacpihed_attach(device_t parent, device_t self, void *aux)
771.1Sriastrad{
781.1Sriastrad	struct acpihed_softc *sc = device_private(self);
791.1Sriastrad	struct acpi_attach_args *aa = aux;
801.1Sriastrad
811.1Sriastrad	aprint_naive("\n");
821.1Sriastrad	aprint_normal(": ACPI Hardware Error Device\n");
831.1Sriastrad
841.1Sriastrad	pmf_device_register(self, NULL, NULL);
851.1Sriastrad
861.1Sriastrad	sc->sc_dev = self;
871.1Sriastrad	sc->sc_node = aa->aa_node;
881.1Sriastrad
891.1Sriastrad	acpi_register_notify(sc->sc_node, acpihed_notify);
901.1Sriastrad}
911.1Sriastrad
921.1Sriastradstatic int
931.1Sriastradacpihed_detach(device_t self, int flags)
941.1Sriastrad{
951.1Sriastrad	struct acpihed_softc *sc = device_private(self);
961.1Sriastrad	int error;
971.1Sriastrad
981.1Sriastrad	error = config_detach_children(self, flags);
991.1Sriastrad	if (error)
1001.1Sriastrad		return error;
1011.1Sriastrad
1021.1Sriastrad	acpi_deregister_notify(sc->sc_node);
1031.1Sriastrad
1041.1Sriastrad	pmf_device_deregister(self);
1051.1Sriastrad
1061.1Sriastrad	return 0;
1071.1Sriastrad}
1081.1Sriastrad
1091.1Sriastradstatic void
1101.1Sriastradacpihed_notify(ACPI_HANDLE handle, uint32_t event, void *cookie)
1111.1Sriastrad{
1121.1Sriastrad
1131.1Sriastrad	apei_hed_notify();
1141.1Sriastrad}
1151.1Sriastrad
1161.1SriastradMODULE(MODULE_CLASS_DRIVER, acpihed, "apei");
1171.1Sriastrad
1181.1Sriastrad#ifdef _MODULE
1191.1Sriastrad#include "ioconf.c"
1201.1Sriastrad#endif
1211.1Sriastrad
1221.1Sriastradstatic int
1231.1Sriastradacpihed_modcmd(modcmd_t cmd, void *opaque)
1241.1Sriastrad{
1251.1Sriastrad	int error = 0;
1261.1Sriastrad
1271.1Sriastrad	switch (cmd) {
1281.1Sriastrad	case MODULE_CMD_INIT:
1291.1Sriastrad#ifdef _MODULE
1301.1Sriastrad		error = config_init_component(cfdriver_ioconf_acpihed,
1311.1Sriastrad		    cfattach_ioconf_acpihed, cfdata_ioconf_acpihed);
1321.1Sriastrad#endif
1331.1Sriastrad		return error;
1341.1Sriastrad	case MODULE_CMD_FINI:
1351.1Sriastrad#ifdef _MODULE
1361.1Sriastrad		error = config_fini_component(cfdriver_ioconf_acpihed,
1371.1Sriastrad		    cfattach_ioconf_acpihed, cfdata_ioconf_acpihed);
1381.1Sriastrad#endif
1391.1Sriastrad		return error;
1401.1Sriastrad	default:
1411.1Sriastrad		return ENOTTY;
1421.1Sriastrad	}
1431.1Sriastrad}
144