From 160fb63ee0c72f165ab131c4d725d9badaa646ab Mon Sep 17 00:00:00 2001 From: Damien Zammit Date: Mon, 12 Sep 2022 10:39:30 +0000 Subject: acpi: Link translator to libacpica and provide RPCs Provides two new acpi RPCs to sleep the machine and to get the irq of any pci device. ACPI mode is enabled by default when the translator is started. NB: Merging this commit means libacpica is a build dep. Message-Id: <20220912103837.556815-2-damien@zamaudio.com> --- Makefile | 5 ++- acpi/Makefile | 13 ++++++-- acpi/acpi-ops.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++ acpi/acpi.c | 2 +- acpi/acpi.h | 74 -------------------------------------------- acpi/acpifs.h | 2 +- acpi/func_files.h | 2 +- acpi/main.c | 12 ++++++-- acpi/mig-mutate.h | 28 +++++++++++++++++ acpi/myacpi.h | 74 ++++++++++++++++++++++++++++++++++++++++++++ config.make.in | 7 +++++ configure.ac | 9 ++++++ hurd/acpi.defs | 50 ++++++++++++++++++++++++++++++ hurd/hurd_types.defs | 18 +++++++++++ hurd/hurd_types.h | 1 + hurd/paths.h | 3 ++ 16 files changed, 303 insertions(+), 83 deletions(-) create mode 100644 acpi/acpi-ops.c delete mode 100644 acpi/acpi.h create mode 100644 acpi/mig-mutate.h create mode 100644 acpi/myacpi.h create mode 100644 hurd/acpi.defs diff --git a/Makefile b/Makefile index 0e2ee1ec..874349c0 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,6 @@ prog-subdirs = auth proc exec term \ init \ devnode \ eth-multiplexer \ - acpi \ shutdown ifeq ($(HAVE_LIBRUMP),yes) @@ -65,6 +64,10 @@ ifeq ($(HAVE_LIBPCIACCESS),yes) prog-subdirs += pci-arbiter endif +ifeq ($(HAVE_LIBACPICA),yes) +prog-subdirs += acpi +endif + # Other directories other-subdirs = hurd doc config release include diff --git a/acpi/Makefile b/acpi/Makefile index f84f4b35..76f27aef 100644 --- a/acpi/Makefile +++ b/acpi/Makefile @@ -21,15 +21,22 @@ makemode = server PORTDIR = $(srcdir)/port SRCS = main.c netfs_impl.c acpi.c \ - acpifs.c ncache.c options.c func_files.c + acpifs.c ncache.c options.c func_files.c acpi-ops.c \ + acpiServer.c + MIGSRCS = OBJS = $(patsubst %.S,%.o,$(patsubst %.c,%.o, $(SRCS) $(MIGSRCS))) HURDLIBS= fshelp ports shouldbeinlibc netfs iohelp ihash -LDLIBS = -lpthread +LDLIBS = -lpthread $(libacpica_LIBS) target = acpi include ../Makeconf -CFLAGS += -I$(PORTDIR)/include +CFLAGS += -I$(PORTDIR)/include $(libacpica_CFLAGS) + +acpi-MIGSFLAGS = -imacros $(srcdir)/mig-mutate.h + +# cpp doesn't automatically make dependencies for -imacros dependencies. argh. +acpi_S.h acpiServer.c: mig-mutate.h diff --git a/acpi/acpi-ops.c b/acpi/acpi-ops.c new file mode 100644 index 00000000..3bc90de7 --- /dev/null +++ b/acpi/acpi-ops.c @@ -0,0 +1,86 @@ +/* + Copyright (C) 2021 Free Software Foundation, Inc. + + This file is part of the GNU Hurd. + + The GNU Hurd is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2, or (at + your option) any later version. + + The GNU Hurd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with the GNU Hurd. If not, see . +*/ + +/* Implementation of ACPI operations */ + +#include + +#include +#include +#include +#include +#include + +#include +#include "acpifs.h" + +static error_t +check_permissions (struct protid *master, int flags) +{ + struct node *node; + struct acpifs_dirent *e; + + node = master->po->np; + e = node->nn->ln; + + /* Check whether the user has permissions to access this node */ + return entry_check_perms (master->user, e, flags); +} + +error_t +S_acpi_sleep (struct protid *master, + int sleep_state) +{ + error_t err; + + if (!master) + return EOPNOTSUPP; + + if (!master->user) + return EOPNOTSUPP; + + if (!idvec_contains (master->user->uids, 0)) + return EOPNOTSUPP; + + /* Perform sleep */ + acpi_enter_sleep(sleep_state); + + /* Never reached */ + return err; +} + +error_t +S_acpi_get_pci_irq (struct protid *master, + int bus, + int dev, + int func, + int *irq) +{ + error_t err; + + if (!master) + return EOPNOTSUPP; + + err = check_permissions (master, O_READ); + if (err) + return err; + + *irq = acpi_get_irq_number(bus, dev, func); + return err; +} diff --git a/acpi/acpi.c b/acpi/acpi.c index 210a229e..9827232a 100644 --- a/acpi/acpi.c +++ b/acpi/acpi.c @@ -28,7 +28,7 @@ #include #include -#include "acpi.h" +#include "myacpi.h" int mmap_phys_acpi_header(uintptr_t base_addr, struct acpi_header **ptr_to_header, diff --git a/acpi/acpi.h b/acpi/acpi.h deleted file mode 100644 index 7c21a442..00000000 --- a/acpi/acpi.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - Copyright (C) 2018 Free Software Foundation, Inc. - - This file is part of the GNU Hurd. - - The GNU Hurd is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2, or (at - your option) any later version. - - The GNU Hurd is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. -*/ - -/* ACPI tables basic structure */ - -#ifndef ACPI_H -#define ACPI_H - -#include -#include - -/* PnP Extended System Configuration Data (ESCD) memory region */ -#define ESCD 0xe0000U -#define RSDP_MAGIC (const unsigned char *)"RSD PTR " -#define ESCD_SIZE 0x20000U - -struct rsdp_descr -{ - uint8_t magic[8]; - uint8_t checksum; - uint8_t oem_id[6]; - uint8_t revision; - uint32_t rsdt_addr; -} __attribute__ ((packed)); - -struct rsdp_descr2 -{ - struct rsdp_descr v1; - uint32_t length; - uint64_t xsdt_addr; - uint8_t checksum; - uint8_t reserved[3]; -} __attribute__ ((packed)); - -struct acpi_header -{ - uint8_t signature[4]; - uint32_t length; - uint8_t revision; - uint8_t checksum; - uint8_t oem_id[6]; - uint8_t oem_table_id[8]; - uint32_t oem_revision; - uint32_t creator_id; - uint32_t creator_revision; -} __attribute__ ((packed)); - -struct acpi_table -{ - struct acpi_header h; - void *data; - size_t datalen; -} __attribute__ ((packed)); - -int acpi_get_num_tables(size_t *num_tables); -int acpi_get_tables(struct acpi_table **tables); - -#endif /* ACPI_H */ diff --git a/acpi/acpifs.h b/acpi/acpifs.h index 8e359efb..4597e1b0 100644 --- a/acpi/acpifs.h +++ b/acpi/acpifs.h @@ -27,7 +27,7 @@ #include #include -#include +#include "myacpi.h" /* Size of a directory entry name */ #ifndef NAME_SIZE diff --git a/acpi/func_files.h b/acpi/func_files.h index 90d92cc3..adc81cf7 100644 --- a/acpi/func_files.h +++ b/acpi/func_files.h @@ -23,7 +23,7 @@ #define FUNC_FILES_H #include -#include +#include "myacpi.h" typedef int (*acpi_read_op_t) (struct acpi_table *t, void *data, off_t offset, size_t *len); diff --git a/acpi/main.c b/acpi/main.c index ac325915..fc46f4f2 100644 --- a/acpi/main.c +++ b/acpi/main.c @@ -26,12 +26,14 @@ #include #include +#include "acpi_S.h" #include "libnetfs/io_S.h" #include "libnetfs/fs_S.h" #include "libports/notify_S.h" #include "libnetfs/fsys_S.h" #include "libports/interrupt_S.h" #include "libnetfs/ifsock_S.h" +#include #include /* Libnetfs stuff */ @@ -53,7 +55,8 @@ netfs_demuxer (mach_msg_header_t * inp, mach_msg_header_t * outp) (routine = ports_notify_server_routine (inp)) || (routine = netfs_fsys_server_routine (inp)) || (routine = ports_interrupt_server_routine (inp)) || - (routine = netfs_ifsock_server_routine (inp))) + (routine = netfs_ifsock_server_routine (inp)) || + (routine = acpi_server_routine (inp))) { (*routine) (inp, outp); return TRUE; @@ -76,10 +79,15 @@ main (int argc, char **argv) if (bootstrap == MACH_PORT_NULL) error (1, 0, "must be started as a translator"); + /* Initialize ACPI */ + acpi_init(); + /* Initialize netfs and start the translator. */ netfs_init (); err = maptime_map (0, 0, &acpifs_maptime); + if (err) + err = maptime_map (1, 0, &acpifs_maptime); if (err) error (1, err, "mapping time"); @@ -98,7 +106,7 @@ main (int argc, char **argv) if (err) error (1, err, "setting permissions"); - netfs_server_loop (); /* Never returns. */ + netfs_server_loop (); /* Never returns. */ return 0; } diff --git a/acpi/mig-mutate.h b/acpi/mig-mutate.h new file mode 100644 index 00000000..1ee33e5f --- /dev/null +++ b/acpi/mig-mutate.h @@ -0,0 +1,28 @@ +/* + Copyright (C) 2017 Free Software Foundation, Inc. + Written by Michael I. Bushnell, p/BSG. + + This file is part of the GNU Hurd. + + The GNU Hurd is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2, or (at + your option) any later version. + + The GNU Hurd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with the GNU Hurd. If not, see . +*/ + +/* Only CPP macro definitions should go in this file. */ + +#define ACPI_IMPORTS \ + import "../libnetfs/priv.h"; \ + +#define ACPI_INTRAN protid_t begin_using_protid_port (acpi_t) +#define ACPI_INTRAN_PAYLOAD protid_t begin_using_protid_payload +#define ACPI_DESTRUCTOR end_using_protid_port (protid_t) diff --git a/acpi/myacpi.h b/acpi/myacpi.h new file mode 100644 index 00000000..44925809 --- /dev/null +++ b/acpi/myacpi.h @@ -0,0 +1,74 @@ +/* + Copyright (C) 2018 Free Software Foundation, Inc. + + This file is part of the GNU Hurd. + + The GNU Hurd is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2, or (at + your option) any later version. + + The GNU Hurd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with the GNU Hurd. If not, see http://www.gnu.org/licenses +*/ + +/* ACPI tables basic structure */ + +#ifndef MYACPI_H +#define MYACPI_H + +#include +#include + +/* PnP Extended System Configuration Data (ESCD) memory region */ +#define ESCD 0xe0000U +#define RSDP_MAGIC (const unsigned char *)"RSD PTR " +#define ESCD_SIZE 0x20000U + +struct rsdp_descr +{ + uint8_t magic[8]; + uint8_t checksum; + uint8_t oem_id[6]; + uint8_t revision; + uint32_t rsdt_addr; +} __attribute__ ((packed)); + +struct rsdp_descr2 +{ + struct rsdp_descr v1; + uint32_t length; + uint64_t xsdt_addr; + uint8_t checksum; + uint8_t reserved[3]; +} __attribute__ ((packed)); + +struct acpi_header +{ + uint8_t signature[4]; + uint32_t length; + uint8_t revision; + uint8_t checksum; + uint8_t oem_id[6]; + uint8_t oem_table_id[8]; + uint32_t oem_revision; + uint32_t creator_id; + uint32_t creator_revision; +} __attribute__ ((packed)); + +struct acpi_table +{ + struct acpi_header h; + void *data; + size_t datalen; +} __attribute__ ((packed)); + +int acpi_get_num_tables(size_t *num_tables); +int acpi_get_tables(struct acpi_table **tables); + +#endif /* MYACPI_H */ diff --git a/config.make.in b/config.make.in index 7c113c37..4a722a7a 100644 --- a/config.make.in +++ b/config.make.in @@ -121,6 +121,13 @@ HAVE_LIBPCIACCESS = @HAVE_LIBPCIACCESS@ libpciaccess_CFLAGS = @libpciaccess_CFLAGS@ libpciaccess_LIBS = @libpciaccess_LIBS@ +# Whether we found libacpica. +HAVE_LIBACPICA = @HAVE_LIBACPICA@ + +# How to compile and link against libacpica. +libacpica_CFLAGS = @libacpica_CFLAGS@ +libacpica_LIBS = @libacpica_LIBS@ + # Installation tools. INSTALL = @INSTALL@ INSTALL_PROGRAM = @INSTALL_PROGRAM@ diff --git a/configure.ac b/configure.ac index 07e84a5b..11cafa5f 100644 --- a/configure.ac +++ b/configure.ac @@ -394,6 +394,15 @@ AC_SUBST([HAVE_LIBPCIACCESS]) AC_SUBST([libpciaccess_CFLAGS]) AC_SUBST([libpciaccess_LIBS]) +AC_CHECK_LIB(acpica, acpi_init, [ + HAVE_LIBACPICA=yes + libacpica_LIBS="-lacpica" + libacpica_CFLAGS=""], + [HAVE_LIBACPICA=no]) +AC_SUBST([HAVE_LIBACPICA]) +AC_SUBST([libacpica_CFLAGS]) +AC_SUBST([libacpica_LIBS]) + AC_CONFIG_FILES([config.make ${makefiles} daemons/runsystem.hurd.sh]) AC_OUTPUT diff --git a/hurd/acpi.defs b/hurd/acpi.defs new file mode 100644 index 00000000..f05c49c5 --- /dev/null +++ b/hurd/acpi.defs @@ -0,0 +1,50 @@ +/* Definitions for acpi-specific calls + Copyright (C) 2021 Free Software Foundation, Inc. + +This file is part of the GNU Hurd. + +The GNU Hurd is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +The GNU Hurd is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with the GNU Hurd; see the file COPYING. If not, write to +the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ + +subsystem acpi 41000; + +#include + +#ifdef ACPI_IMPORTS +ACPI_IMPORTS +#endif + +/* Works on root entry. + * + * Enter sleep state + * 3 = S3 (suspend) + * 5 = S5 (power off) + */ +routine acpi_sleep( + master: acpi_t; + sleep_state: int +); + +/* Works on root entry. + * + * Get the irq for a particular PCI device + * based on its B/D/F + */ +routine acpi_get_pci_irq( + master: acpi_t; + bus: int; + dev: int; + func: int; + out irq: int +); diff --git a/hurd/hurd_types.defs b/hurd/hurd_types.defs index 88024045..5b9dd85a 100644 --- a/hurd/hurd_types.defs +++ b/hurd/hurd_types.defs @@ -332,6 +332,24 @@ destructor: SHUTDOWN_DESTRUCTOR #endif ; +/* ACPI */ +type acpi_t = mach_port_copy_send_t +#ifdef ACPI_INTRAN +intran: ACPI_INTRAN +intranpayload: ACPI_INTRAN_PAYLOAD +#else +#ifdef HURD_DEFAULT_PAYLOAD_TO_PORT +intranpayload: acpi_t HURD_DEFAULT_PAYLOAD_TO_PORT +#endif +#endif +#ifdef ACPI_OUTTRAN +outtran: ACPI_OUTTRAN +#endif +#ifdef ACPI_DESTRUCTOR +destructor: ACPI_DESTRUCTOR +#endif +; + type proccoll_t = mach_port_copy_send_t; type sreply_port_t = MACH_MSG_TYPE_MAKE_SEND_ONCE | polymorphic diff --git a/hurd/hurd_types.h b/hurd/hurd_types.h index f0521191..cdc01906 100644 --- a/hurd/hurd_types.h +++ b/hurd/hurd_types.h @@ -53,6 +53,7 @@ typedef mach_port_t proccoll_t; typedef mach_port_t ctty_t; typedef mach_port_t pci_t; typedef mach_port_t shutdown_t; +typedef mach_port_t acpi_t; #include /* Defines `error_t'. */ diff --git a/hurd/paths.h b/hurd/paths.h index 10ae3a6f..49623cd1 100644 --- a/hurd/paths.h +++ b/hurd/paths.h @@ -39,6 +39,9 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ /* Directory containing virtual filesystems for buses */ #define _SERVERS_BUS _SERVERS "bus" +/* Directory containing ACPI tables */ +#define _SERVERS_ACPI _SERVERS "acpi" + /* Hurd servers are specified by symbols _HURD_FOO, the canonical pathname being /hurd/foo. */ -- cgit v1.2.3