xref: /netbsd-src/sys/dev/acpi/acpi_ged.c (revision 369725234a52d801d02d2e34cbc47050131c30cf)
1*36972523Sjmcneill /* $NetBSD: acpi_ged.c,v 1.4 2022/01/11 10:53:08 jmcneill Exp $ */
2efd4d136Sjmcneill 
3efd4d136Sjmcneill /*-
4efd4d136Sjmcneill  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5efd4d136Sjmcneill  * All rights reserved.
6efd4d136Sjmcneill  *
7efd4d136Sjmcneill  * This code is derived from software contributed to The NetBSD Foundation
8efd4d136Sjmcneill  * by Jared McNeill <jmcneill@invisible.ca>.
9efd4d136Sjmcneill  *
10efd4d136Sjmcneill  * Redistribution and use in source and binary forms, with or without
11efd4d136Sjmcneill  * modification, are permitted provided that the following conditions
12efd4d136Sjmcneill  * are met:
13efd4d136Sjmcneill  * 1. Redistributions of source code must retain the above copyright
14efd4d136Sjmcneill  *    notice, this list of conditions and the following disclaimer.
15efd4d136Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
16efd4d136Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
17efd4d136Sjmcneill  *    documentation and/or other materials provided with the distribution.
18efd4d136Sjmcneill  *
19efd4d136Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20efd4d136Sjmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21efd4d136Sjmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22efd4d136Sjmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23efd4d136Sjmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24efd4d136Sjmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25efd4d136Sjmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26efd4d136Sjmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27efd4d136Sjmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28efd4d136Sjmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29efd4d136Sjmcneill  * POSSIBILITY OF SUCH DAMAGE.
30efd4d136Sjmcneill  */
31efd4d136Sjmcneill 
32efd4d136Sjmcneill #include <sys/cdefs.h>
33*36972523Sjmcneill __KERNEL_RCSID(0, "$NetBSD: acpi_ged.c,v 1.4 2022/01/11 10:53:08 jmcneill Exp $");
34efd4d136Sjmcneill 
35efd4d136Sjmcneill #include <sys/param.h>
36efd4d136Sjmcneill #include <sys/bus.h>
37efd4d136Sjmcneill #include <sys/cpu.h>
38efd4d136Sjmcneill #include <sys/device.h>
39efd4d136Sjmcneill #include <sys/intr.h>
40efd4d136Sjmcneill #include <sys/kmem.h>
41efd4d136Sjmcneill 
42efd4d136Sjmcneill #include <dev/acpi/acpireg.h>
43efd4d136Sjmcneill #include <dev/acpi/acpivar.h>
44efd4d136Sjmcneill #include <dev/acpi/acpi_event.h>
45e6e2e5bfSjmcneill #include <dev/acpi/acpi_intr.h>
46efd4d136Sjmcneill 
47efd4d136Sjmcneill static int	acpi_ged_match(device_t, cfdata_t, void *);
48efd4d136Sjmcneill static void	acpi_ged_attach(device_t, device_t, void *);
49efd4d136Sjmcneill 
50efd4d136Sjmcneill static void	acpi_ged_register_event(void *, struct acpi_event *, struct acpi_irq *);
51efd4d136Sjmcneill static int	acpi_ged_intr(void *);
52efd4d136Sjmcneill 
53efd4d136Sjmcneill CFATTACH_DECL_NEW(acpiged, 0, acpi_ged_match, acpi_ged_attach, NULL, NULL);
54efd4d136Sjmcneill 
5537725553Sthorpej static const struct device_compatible_entry compat_data[] = {
5637725553Sthorpej 	{ .compat = "ACPI0013" },
5737725553Sthorpej 	DEVICE_COMPAT_EOL
58efd4d136Sjmcneill };
59efd4d136Sjmcneill 
60efd4d136Sjmcneill static int
acpi_ged_match(device_t parent,cfdata_t cf,void * aux)61efd4d136Sjmcneill acpi_ged_match(device_t parent, cfdata_t cf, void *aux)
62efd4d136Sjmcneill {
63efd4d136Sjmcneill 	struct acpi_attach_args *aa = aux;
64efd4d136Sjmcneill 
6537725553Sthorpej 	return acpi_compatible_match(aa, compat_data);
66efd4d136Sjmcneill }
67efd4d136Sjmcneill 
68efd4d136Sjmcneill static void
acpi_ged_attach(device_t parent,device_t self,void * aux)69efd4d136Sjmcneill acpi_ged_attach(device_t parent, device_t self, void *aux)
70efd4d136Sjmcneill {
71efd4d136Sjmcneill 	struct acpi_attach_args * const aa = aux;
72efd4d136Sjmcneill 	ACPI_HANDLE handle = aa->aa_node->ad_handle;
73efd4d136Sjmcneill 
74efd4d136Sjmcneill         if (ACPI_FAILURE(acpi_event_create_int(self, handle, acpi_ged_register_event, self)))
75efd4d136Sjmcneill                 aprint_error_dev(self, "failed to create events\n");
76efd4d136Sjmcneill }
77efd4d136Sjmcneill 
78efd4d136Sjmcneill static void
acpi_ged_register_event(void * priv,struct acpi_event * ev,struct acpi_irq * irq)79efd4d136Sjmcneill acpi_ged_register_event(void *priv, struct acpi_event *ev, struct acpi_irq *irq)
80efd4d136Sjmcneill {
81efd4d136Sjmcneill 	device_t dev = priv;
82efd4d136Sjmcneill 	void *ih;
83efd4d136Sjmcneill 
84e6e2e5bfSjmcneill 	ih = acpi_intr_establish_irq(dev, irq, IPL_VM, true,
85e6e2e5bfSjmcneill 	    acpi_ged_intr, ev, device_xname(dev));
86efd4d136Sjmcneill 	if (ih == NULL) {
87efd4d136Sjmcneill 		aprint_error_dev(dev, "couldn't establish interrupt (irq %d)\n", irq->ar_irq);
88efd4d136Sjmcneill 		return;
89efd4d136Sjmcneill 	}
90*36972523Sjmcneill 	acpi_event_set_intrcookie(ev, ih);
91efd4d136Sjmcneill }
92efd4d136Sjmcneill 
93efd4d136Sjmcneill static int
acpi_ged_intr(void * priv)94efd4d136Sjmcneill acpi_ged_intr(void *priv)
95efd4d136Sjmcneill {
96efd4d136Sjmcneill 	struct acpi_event * const ev = priv;
97efd4d136Sjmcneill 
98efd4d136Sjmcneill 	acpi_event_notify(ev);
99efd4d136Sjmcneill 
100efd4d136Sjmcneill 	return 1;
101efd4d136Sjmcneill }
102