acpi_ged.c revision 1.4
11.4Sjmcneill/* $NetBSD: acpi_ged.c,v 1.4 2022/01/11 10:53:08 jmcneill Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2018 The NetBSD Foundation, Inc.
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * This code is derived from software contributed to The NetBSD Foundation
81.1Sjmcneill * by Jared McNeill <jmcneill@invisible.ca>.
91.1Sjmcneill *
101.1Sjmcneill * Redistribution and use in source and binary forms, with or without
111.1Sjmcneill * modification, are permitted provided that the following conditions
121.1Sjmcneill * are met:
131.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
141.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
151.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
161.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
171.1Sjmcneill *    documentation and/or other materials provided with the distribution.
181.1Sjmcneill *
191.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sjmcneill * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sjmcneill * POSSIBILITY OF SUCH DAMAGE.
301.1Sjmcneill */
311.1Sjmcneill
321.1Sjmcneill#include <sys/cdefs.h>
331.4Sjmcneill__KERNEL_RCSID(0, "$NetBSD: acpi_ged.c,v 1.4 2022/01/11 10:53:08 jmcneill Exp $");
341.1Sjmcneill
351.1Sjmcneill#include <sys/param.h>
361.1Sjmcneill#include <sys/bus.h>
371.1Sjmcneill#include <sys/cpu.h>
381.1Sjmcneill#include <sys/device.h>
391.1Sjmcneill#include <sys/intr.h>
401.1Sjmcneill#include <sys/kmem.h>
411.1Sjmcneill
421.1Sjmcneill#include <dev/acpi/acpireg.h>
431.1Sjmcneill#include <dev/acpi/acpivar.h>
441.1Sjmcneill#include <dev/acpi/acpi_event.h>
451.2Sjmcneill#include <dev/acpi/acpi_intr.h>
461.1Sjmcneill
471.1Sjmcneillstatic int	acpi_ged_match(device_t, cfdata_t, void *);
481.1Sjmcneillstatic void	acpi_ged_attach(device_t, device_t, void *);
491.1Sjmcneill
501.1Sjmcneillstatic void	acpi_ged_register_event(void *, struct acpi_event *, struct acpi_irq *);
511.1Sjmcneillstatic int	acpi_ged_intr(void *);
521.1Sjmcneill
531.1SjmcneillCFATTACH_DECL_NEW(acpiged, 0, acpi_ged_match, acpi_ged_attach, NULL, NULL);
541.1Sjmcneill
551.3Sthorpejstatic const struct device_compatible_entry compat_data[] = {
561.3Sthorpej	{ .compat = "ACPI0013" },
571.3Sthorpej	DEVICE_COMPAT_EOL
581.1Sjmcneill};
591.1Sjmcneill
601.1Sjmcneillstatic int
611.1Sjmcneillacpi_ged_match(device_t parent, cfdata_t cf, void *aux)
621.1Sjmcneill{
631.1Sjmcneill	struct acpi_attach_args *aa = aux;
641.1Sjmcneill
651.3Sthorpej	return acpi_compatible_match(aa, compat_data);
661.1Sjmcneill}
671.1Sjmcneill
681.1Sjmcneillstatic void
691.1Sjmcneillacpi_ged_attach(device_t parent, device_t self, void *aux)
701.1Sjmcneill{
711.1Sjmcneill	struct acpi_attach_args * const aa = aux;
721.1Sjmcneill	ACPI_HANDLE handle = aa->aa_node->ad_handle;
731.1Sjmcneill
741.1Sjmcneill        if (ACPI_FAILURE(acpi_event_create_int(self, handle, acpi_ged_register_event, self)))
751.1Sjmcneill                aprint_error_dev(self, "failed to create events\n");
761.1Sjmcneill}
771.1Sjmcneill
781.1Sjmcneillstatic void
791.1Sjmcneillacpi_ged_register_event(void *priv, struct acpi_event *ev, struct acpi_irq *irq)
801.1Sjmcneill{
811.1Sjmcneill	device_t dev = priv;
821.1Sjmcneill	void *ih;
831.1Sjmcneill
841.2Sjmcneill	ih = acpi_intr_establish_irq(dev, irq, IPL_VM, true,
851.2Sjmcneill	    acpi_ged_intr, ev, device_xname(dev));
861.1Sjmcneill	if (ih == NULL) {
871.1Sjmcneill		aprint_error_dev(dev, "couldn't establish interrupt (irq %d)\n", irq->ar_irq);
881.1Sjmcneill		return;
891.1Sjmcneill	}
901.4Sjmcneill	acpi_event_set_intrcookie(ev, ih);
911.1Sjmcneill}
921.1Sjmcneill
931.1Sjmcneillstatic int
941.1Sjmcneillacpi_ged_intr(void *priv)
951.1Sjmcneill{
961.1Sjmcneill	struct acpi_event * const ev = priv;
971.1Sjmcneill
981.1Sjmcneill	acpi_event_notify(ev);
991.1Sjmcneill
1001.1Sjmcneill	return 1;
1011.1Sjmcneill}
102