1*40cfbdd6Sriastradh /* $NetBSD: acpi_hed.c,v 1.1 2024/03/21 02:36:01 riastradh Exp $ */
2*40cfbdd6Sriastradh
3*40cfbdd6Sriastradh /*-
4*40cfbdd6Sriastradh * Copyright (c) 2024 The NetBSD Foundation, Inc.
5*40cfbdd6Sriastradh * All rights reserved.
6*40cfbdd6Sriastradh *
7*40cfbdd6Sriastradh * Redistribution and use in source and binary forms, with or without
8*40cfbdd6Sriastradh * modification, are permitted provided that the following conditions
9*40cfbdd6Sriastradh * are met:
10*40cfbdd6Sriastradh * 1. Redistributions of source code must retain the above copyright
11*40cfbdd6Sriastradh * notice, this list of conditions and the following disclaimer.
12*40cfbdd6Sriastradh * 2. Redistributions in binary form must reproduce the above copyright
13*40cfbdd6Sriastradh * notice, this list of conditions and the following disclaimer in the
14*40cfbdd6Sriastradh * documentation and/or other materials provided with the distribution.
15*40cfbdd6Sriastradh *
16*40cfbdd6Sriastradh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*40cfbdd6Sriastradh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*40cfbdd6Sriastradh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*40cfbdd6Sriastradh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*40cfbdd6Sriastradh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*40cfbdd6Sriastradh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*40cfbdd6Sriastradh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*40cfbdd6Sriastradh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*40cfbdd6Sriastradh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*40cfbdd6Sriastradh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*40cfbdd6Sriastradh * POSSIBILITY OF SUCH DAMAGE.
27*40cfbdd6Sriastradh */
28*40cfbdd6Sriastradh
29*40cfbdd6Sriastradh /*
30*40cfbdd6Sriastradh * HED: Hardware Error Device, PNP0C33.
31*40cfbdd6Sriastradh *
32*40cfbdd6Sriastradh * This device serves only to receive notifications about hardware
33*40cfbdd6Sriastradh * errors, which we then dispatch to apei(4).
34*40cfbdd6Sriastradh */
35*40cfbdd6Sriastradh
36*40cfbdd6Sriastradh #include <sys/cdefs.h>
37*40cfbdd6Sriastradh __KERNEL_RCSID(0, "$NetBSD: acpi_hed.c,v 1.1 2024/03/21 02:36:01 riastradh Exp $");
38*40cfbdd6Sriastradh
39*40cfbdd6Sriastradh #include <sys/device.h>
40*40cfbdd6Sriastradh #include <sys/module.h>
41*40cfbdd6Sriastradh
42*40cfbdd6Sriastradh #include <dev/acpi/acpireg.h>
43*40cfbdd6Sriastradh #include <dev/acpi/acpivar.h>
44*40cfbdd6Sriastradh #include <dev/acpi/apei_hed.h>
45*40cfbdd6Sriastradh
46*40cfbdd6Sriastradh #define _COMPONENT ACPI_RESOURCE_COMPONENT
47*40cfbdd6Sriastradh ACPI_MODULE_NAME ("acpi_hed")
48*40cfbdd6Sriastradh
49*40cfbdd6Sriastradh struct acpihed_softc {
50*40cfbdd6Sriastradh device_t sc_dev;
51*40cfbdd6Sriastradh struct acpi_devnode *sc_node;
52*40cfbdd6Sriastradh };
53*40cfbdd6Sriastradh
54*40cfbdd6Sriastradh static const struct device_compatible_entry compat_data[] = {
55*40cfbdd6Sriastradh { .compat = "PNP0C33" },
56*40cfbdd6Sriastradh DEVICE_COMPAT_EOL
57*40cfbdd6Sriastradh };
58*40cfbdd6Sriastradh
59*40cfbdd6Sriastradh static int acpihed_match(device_t, cfdata_t, void *);
60*40cfbdd6Sriastradh static void acpihed_attach(device_t, device_t, void *);
61*40cfbdd6Sriastradh static int acpihed_detach(device_t, int);
62*40cfbdd6Sriastradh static void acpihed_notify(ACPI_HANDLE, uint32_t, void *);
63*40cfbdd6Sriastradh
64*40cfbdd6Sriastradh CFATTACH_DECL_NEW(acpihed, sizeof(struct acpihed_softc),
65*40cfbdd6Sriastradh acpihed_match, acpihed_attach, acpihed_detach, NULL);
66*40cfbdd6Sriastradh
67*40cfbdd6Sriastradh static int
acpihed_match(device_t parent,cfdata_t match,void * aux)68*40cfbdd6Sriastradh acpihed_match(device_t parent, cfdata_t match, void *aux)
69*40cfbdd6Sriastradh {
70*40cfbdd6Sriastradh struct acpi_attach_args *aa = aux;
71*40cfbdd6Sriastradh
72*40cfbdd6Sriastradh return acpi_compatible_match(aa, compat_data);
73*40cfbdd6Sriastradh }
74*40cfbdd6Sriastradh
75*40cfbdd6Sriastradh static void
acpihed_attach(device_t parent,device_t self,void * aux)76*40cfbdd6Sriastradh acpihed_attach(device_t parent, device_t self, void *aux)
77*40cfbdd6Sriastradh {
78*40cfbdd6Sriastradh struct acpihed_softc *sc = device_private(self);
79*40cfbdd6Sriastradh struct acpi_attach_args *aa = aux;
80*40cfbdd6Sriastradh
81*40cfbdd6Sriastradh aprint_naive("\n");
82*40cfbdd6Sriastradh aprint_normal(": ACPI Hardware Error Device\n");
83*40cfbdd6Sriastradh
84*40cfbdd6Sriastradh pmf_device_register(self, NULL, NULL);
85*40cfbdd6Sriastradh
86*40cfbdd6Sriastradh sc->sc_dev = self;
87*40cfbdd6Sriastradh sc->sc_node = aa->aa_node;
88*40cfbdd6Sriastradh
89*40cfbdd6Sriastradh acpi_register_notify(sc->sc_node, acpihed_notify);
90*40cfbdd6Sriastradh }
91*40cfbdd6Sriastradh
92*40cfbdd6Sriastradh static int
acpihed_detach(device_t self,int flags)93*40cfbdd6Sriastradh acpihed_detach(device_t self, int flags)
94*40cfbdd6Sriastradh {
95*40cfbdd6Sriastradh struct acpihed_softc *sc = device_private(self);
96*40cfbdd6Sriastradh int error;
97*40cfbdd6Sriastradh
98*40cfbdd6Sriastradh error = config_detach_children(self, flags);
99*40cfbdd6Sriastradh if (error)
100*40cfbdd6Sriastradh return error;
101*40cfbdd6Sriastradh
102*40cfbdd6Sriastradh acpi_deregister_notify(sc->sc_node);
103*40cfbdd6Sriastradh
104*40cfbdd6Sriastradh pmf_device_deregister(self);
105*40cfbdd6Sriastradh
106*40cfbdd6Sriastradh return 0;
107*40cfbdd6Sriastradh }
108*40cfbdd6Sriastradh
109*40cfbdd6Sriastradh static void
acpihed_notify(ACPI_HANDLE handle,uint32_t event,void * cookie)110*40cfbdd6Sriastradh acpihed_notify(ACPI_HANDLE handle, uint32_t event, void *cookie)
111*40cfbdd6Sriastradh {
112*40cfbdd6Sriastradh
113*40cfbdd6Sriastradh apei_hed_notify();
114*40cfbdd6Sriastradh }
115*40cfbdd6Sriastradh
116*40cfbdd6Sriastradh MODULE(MODULE_CLASS_DRIVER, acpihed, "apei");
117*40cfbdd6Sriastradh
118*40cfbdd6Sriastradh #ifdef _MODULE
119*40cfbdd6Sriastradh #include "ioconf.c"
120*40cfbdd6Sriastradh #endif
121*40cfbdd6Sriastradh
122*40cfbdd6Sriastradh static int
acpihed_modcmd(modcmd_t cmd,void * opaque)123*40cfbdd6Sriastradh acpihed_modcmd(modcmd_t cmd, void *opaque)
124*40cfbdd6Sriastradh {
125*40cfbdd6Sriastradh int error = 0;
126*40cfbdd6Sriastradh
127*40cfbdd6Sriastradh switch (cmd) {
128*40cfbdd6Sriastradh case MODULE_CMD_INIT:
129*40cfbdd6Sriastradh #ifdef _MODULE
130*40cfbdd6Sriastradh error = config_init_component(cfdriver_ioconf_acpihed,
131*40cfbdd6Sriastradh cfattach_ioconf_acpihed, cfdata_ioconf_acpihed);
132*40cfbdd6Sriastradh #endif
133*40cfbdd6Sriastradh return error;
134*40cfbdd6Sriastradh case MODULE_CMD_FINI:
135*40cfbdd6Sriastradh #ifdef _MODULE
136*40cfbdd6Sriastradh error = config_fini_component(cfdriver_ioconf_acpihed,
137*40cfbdd6Sriastradh cfattach_ioconf_acpihed, cfdata_ioconf_acpihed);
138*40cfbdd6Sriastradh #endif
139*40cfbdd6Sriastradh return error;
140*40cfbdd6Sriastradh default:
141*40cfbdd6Sriastradh return ENOTTY;
142*40cfbdd6Sriastradh }
143*40cfbdd6Sriastradh }
144