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