CVS commit: src

2023-07-18 Thread Luke Mewburn
Module Name:src
Committed By:   lukem
Date:   Tue Jul 18 16:59:57 UTC 2023

Modified Files:
src: BUILDING UPDATING build.sh
src/doc: BUILDING.mdoc

Log Message:
build.sh: use mk.conf (if present) as MAKECONF

Set MAKECONF to mk.conf in the same directory as build.sh
(i.e., the top of the source tree) if mk.conf is present.
This means unprivileged users can benefit from mk.conf(5)
semantics without write privileges to /etc/mk.conf, and
also simplifies per-source-directory configuration.

Fail early if $MAKECONF is empty, since build.sh will fail
anyway after rebuilding nbmake.

Ensure that the computed MAKECONF isn't empty, and then
always set MAKECONF in the makewrapper (nbmake-$MACHINE).

Improve some formatting consistency in BUILDING.mdoc,
(even if BUILDING is generated without markup).


To generate a diff of this commit:
cvs rdiff -u -r1.156 -r1.157 src/BUILDING
cvs rdiff -u -r1.340 -r1.341 src/UPDATING
cvs rdiff -u -r1.372 -r1.373 src/build.sh
cvs rdiff -u -r1.145 -r1.146 src/doc/BUILDING.mdoc

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/BUILDING
diff -u src/BUILDING:1.156 src/BUILDING:1.157
--- src/BUILDING:1.156	Wed Jul  5 16:41:12 2023
+++ src/BUILDING	Tue Jul 18 16:59:57 2023
@@ -9,12 +9,16 @@ REQUIREMENTS
  same NetBSD architecture) or cross compiling (on another architecture or
  OS).
 
+ This source tree contains the build.sh shell script which supports both
+ native and cross builds of NetBSD.
+
  This source tree contains a special subtree, "tools", which uses the host
  system to create a build toolchain for the target architecture.  The host
  system must have at least C and C++ compilers in order to create the
- toolchain (make is not required); all other tools are created as part of
- the NetBSD build process.  (See the Environment variables section below
- if you need to override or manually select your compilers.)
+ toolchain (make(1) is not required); all other tools (including make(1)
+ as nbmake) are created as part of the NetBSD build process.  (See the
+ Environment variables section below if you need to override or manually
+ select your compilers.)
 
  Note: Within this document, cross-references to manual pages are to the
  NetBSD manual pages, not the host system manual pages.  The mdoc(7)
@@ -71,6 +75,12 @@ FILES
 "Reachover" build structure for modular Xorg; the source
 is in X11SRCDIR.
 
+ mk.confOptional source tree specific mk.conf(5), used (if
+present) instead of /etc/mk.conf unless MAKECONF is
+defined.
+
+Note: Not part of the NetBSD source repository.
+
  regress/, tests/
 Regression test harness.  Can be cross-compiled, but only
 run natively.  tests/ uses the atf(7) test framework;
@@ -164,7 +174,9 @@ CONFIGURATION
 
  Note: Only settable in the process environment.
 
- Default: "/etc/mk.conf".
+ Default: "/etc/mk.conf", although build.sh will set the
+ default to the full path to mk.conf if the latter is
+ present in the same directory as build.sh.
 
  MAKEFLAGS   Flags to invoke make(1) with.
 
@@ -819,4 +831,4 @@ CAVEATS
  in object directories.  Instead, one may have to manually remove the
  files.  Consult the UPDATING file for notices concerning this.
 
-NetBSD   July 5, 2023   NetBSD
+NetBSD   July 18, 2023  NetBSD

Index: src/UPDATING
diff -u src/UPDATING:1.340 src/UPDATING:1.341
--- src/UPDATING:1.340	Tue Jul 11 15:05:17 2023
+++ src/UPDATING	Tue Jul 18 16:59:57 2023
@@ -1,4 +1,4 @@
-$NetBSD: UPDATING,v 1.340 2023/07/11 15:05:17 martin Exp $
+$NetBSD: UPDATING,v 1.341 2023/07/18 16:59:57 lukem Exp $
 
 This file (UPDATING) is intended to be a brief reference to recent
 changes that might cause problems in the build process, and a guide for
@@ -19,6 +19,10 @@ See also: BUILDING, build.sh, Makefile.
 Recent changes:
 ^^^
 
+20230718:
+	build.sh will use mk.conf in the same directory as build.sh instead
+	of /etc/mk.conf, unless MAKECONF is already defined in the environment.
+
 20230604:
 	Building native kernels with the installed make(1) instead of
 	the $TOOLDIR/bin/nbmake-${arch} wrapper will fail if userland

Index: src/build.sh
diff -u src/build.sh:1.372 src/build.sh:1.373
--- src/build.sh:1.372	Tue Jun 13 16:56:00 2023
+++ src/build.sh	Tue Jul 18 16:59:57 2023
@@ -1,5 +1,5 @@
 #! /usr/bin/env sh
-#	$NetBSD: build.sh,v 1.372 2023/06/13 16:56:00 christos Exp $
+#	$NetBSD: build.sh,v 1.373 2023/07/18 16:59:57 lukem Exp $
 #
 # Copyright (c) 2001-2

CVS commit: src

2023-07-18 Thread Luke Mewburn
Module Name:src
Committed By:   lukem
Date:   Tue Jul 18 16:59:57 UTC 2023

Modified Files:
src: BUILDING UPDATING build.sh
src/doc: BUILDING.mdoc

Log Message:
build.sh: use mk.conf (if present) as MAKECONF

Set MAKECONF to mk.conf in the same directory as build.sh
(i.e., the top of the source tree) if mk.conf is present.
This means unprivileged users can benefit from mk.conf(5)
semantics without write privileges to /etc/mk.conf, and
also simplifies per-source-directory configuration.

Fail early if $MAKECONF is empty, since build.sh will fail
anyway after rebuilding nbmake.

Ensure that the computed MAKECONF isn't empty, and then
always set MAKECONF in the makewrapper (nbmake-$MACHINE).

Improve some formatting consistency in BUILDING.mdoc,
(even if BUILDING is generated without markup).


To generate a diff of this commit:
cvs rdiff -u -r1.156 -r1.157 src/BUILDING
cvs rdiff -u -r1.340 -r1.341 src/UPDATING
cvs rdiff -u -r1.372 -r1.373 src/build.sh
cvs rdiff -u -r1.145 -r1.146 src/doc/BUILDING.mdoc

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/hdaudio

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 13:35:57 UTC 2023

Modified Files:
src/sys/dev/hdaudio: hdafg.c

Log Message:
hdafg(4): Do hotplug detection in kthread, not callout.

This can sometimes take a while (~1ms), and the logic to suspend the
callout on device suspend/resume was racy (PR kern/57322).

XXX pullup-8
XXX pullup-9
XXX pullup-10


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 src/sys/dev/hdaudio/hdafg.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/hdaudio

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 13:35:57 UTC 2023

Modified Files:
src/sys/dev/hdaudio: hdafg.c

Log Message:
hdafg(4): Do hotplug detection in kthread, not callout.

This can sometimes take a while (~1ms), and the logic to suspend the
callout on device suspend/resume was racy (PR kern/57322).

XXX pullup-8
XXX pullup-9
XXX pullup-10


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 src/sys/dev/hdaudio/hdafg.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/hdaudio/hdafg.c
diff -u src/sys/dev/hdaudio/hdafg.c:1.29 src/sys/dev/hdaudio/hdafg.c:1.30
--- src/sys/dev/hdaudio/hdafg.c:1.29	Thu Jan  5 09:57:39 2023
+++ src/sys/dev/hdaudio/hdafg.c	Tue Jul 18 13:35:57 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: hdafg.c,v 1.29 2023/01/05 09:57:39 kardel Exp $ */
+/* $NetBSD: hdafg.c,v 1.30 2023/07/18 13:35:57 riastradh Exp $ */
 
 /*
  * Copyright (c) 2009 Precedence Technologies Ltd 
@@ -60,7 +60,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: hdafg.c,v 1.29 2023/01/05 09:57:39 kardel Exp $");
+__KERNEL_RCSID(0, "$NetBSD: hdafg.c,v 1.30 2023/07/18 13:35:57 riastradh Exp $");
 
 #include 
 #include 
@@ -71,6 +71,9 @@ __KERNEL_RCSID(0, "$NetBSD: hdafg.c,v 1.
 #include 
 #include 
 #include 
+#include 
+#include 
+#include 
 
 #include 
 #include 
@@ -311,8 +314,12 @@ struct hdafg_softc {
 	intsc_pchan, sc_rchan;
 	audio_params_t			sc_pparam, sc_rparam;
 
-	struct callout			sc_jack_callout;
+	kmutex_t			sc_jack_lock;
+	kcondvar_t			sc_jack_cv;
+	struct lwp			*sc_jack_thread;
 	boolsc_jack_polling;
+	boolsc_jack_suspended;
+	boolsc_jack_dying;
 
 	struct {
 		uint32_t		afg_cap;
@@ -3512,16 +3519,18 @@ hdafg_configure_encodings(struct hdafg_s
 }
 
 static void
-hdafg_hp_switch_handler(void *opaque)
+hdafg_hp_switch_handler(struct hdafg_softc *sc)
 {
-	struct hdafg_softc *sc = opaque;
 	struct hdaudio_assoc *as = sc->sc_assocs;
 	struct hdaudio_widget *w;
 	uint32_t res = 0;
 	int i, j;
 
+	KASSERT(sc->sc_jack_polling);
+	KASSERT(mutex_owned(&sc->sc_jack_lock));
+
 	if (!device_is_active(sc->sc_dev))
-		goto resched;
+		return;
 
 	for (i = 0; i < sc->sc_nassocs; i++) {
 		if (as[i].as_digital != HDAFG_AS_ANALOG &&
@@ -3580,9 +3589,28 @@ hdafg_hp_switch_handler(void *opaque)
 			}
 		}
 	}
+}
+
+static void
+hdafg_hp_switch_thread(void *opaque)
+{
+	struct hdafg_softc *sc = opaque;
+
+	KASSERT(sc->sc_jack_polling);
+
+	mutex_enter(&sc->sc_jack_lock);
+	while (!sc->sc_jack_dying) {
+		if (sc->sc_jack_suspended) {
+			cv_wait(&sc->sc_jack_cv, &sc->sc_jack_lock);
+			continue;
+		}
+		hdafg_hp_switch_handler(sc);
+		(void)cv_timedwait(&sc->sc_jack_cv, &sc->sc_jack_lock,
+		HDAUDIO_HP_SENSE_PERIOD);
+	}
+	mutex_exit(&sc->sc_jack_lock);
 
-resched:
-	callout_schedule(&sc->sc_jack_callout, HDAUDIO_HP_SENSE_PERIOD);
+	kthread_exit(0);
 }
 
 static void
@@ -3592,6 +3620,7 @@ hdafg_hp_switch_init(struct hdafg_softc 
 	struct hdaudio_widget *w;
 	bool enable = false;
 	int i, j;
+	int error;
 
 	for (i = 0; i < sc->sc_nassocs; i++) {
 		if (as[i].as_hpredir < 0 && as[i].as_displaydev == false)
@@ -3650,11 +3679,24 @@ hdafg_hp_switch_init(struct hdafg_softc 
 			hda_trace1(sc, ",displayport");
 		hda_trace1(sc, "]\n");
 	}
-	if (enable) {
-		sc->sc_jack_polling = true;
-		hdafg_hp_switch_handler(sc);
-	} else
+	if (!enable) {
 		hda_trace(sc, "jack detect not enabled\n");
+		return;
+	}
+
+	mutex_init(&sc->sc_jack_lock, MUTEX_DEFAULT, IPL_NONE);
+	cv_init(&sc->sc_jack_cv, "hdafghp");
+	sc->sc_jack_polling = true;
+	error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, /*ci*/NULL,
+	hdafg_hp_switch_thread, sc, &sc->sc_jack_thread,
+	"%s hotplug detect", device_xname(sc->sc_dev));
+	if (error) {
+		aprint_error_dev(sc->sc_dev, "failed to create hotplug thread:"
+		" %d", error);
+		sc->sc_jack_polling = false;
+		cv_destroy(&sc->sc_jack_cv);
+		mutex_destroy(&sc->sc_jack_lock);
+	}
 }
 
 static void
@@ -3675,10 +3717,6 @@ hdafg_attach(device_t parent, device_t s
 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
 
-	callout_init(&sc->sc_jack_callout, 0);
-	callout_setfunc(&sc->sc_jack_callout,
-	hdafg_hp_switch_handler, sc);
-
 	if (!pmf_device_register(self, hdafg_suspend, hdafg_resume))
 		aprint_error_dev(self, "couldn't establish power handler\n");
 
@@ -3825,8 +3863,16 @@ hdafg_detach(device_t self, int flags)
 	struct hdaudio_mixer *mx = sc->sc_mixers;
 	int nid;
 
-	callout_halt(&sc->sc_jack_callout, NULL);
-	callout_destroy(&sc->sc_jack_callout);
+	if (sc->sc_jack_polling) {
+		int error __diagused;
+
+		mutex_enter(&sc->sc_jack_lock);
+		sc->sc_jack_dying = true;
+		cv_broadcast(&sc->sc_jack_cv);
+		mutex_exit(&sc->sc_jack_lock);
+		error = kthread_join(sc->sc_jack_thread);
+		KASSERTMSG(error == 0, "error=%d", error);
+	}
 
 	if (sc->sc_config)
 		prop_object_release(sc->sc_config

CVS commit: src/sys/arch/x86/x86

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 12:34:25 UTC 2023

Modified Files:
src/sys/arch/x86/x86: fpu.c

Log Message:
x86/fpu: In kernel mode fpu traps, print the instruction pointer.


To generate a diff of this commit:
cvs rdiff -u -r1.86 -r1.87 src/sys/arch/x86/x86/fpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/fpu.c
diff -u src/sys/arch/x86/x86/fpu.c:1.86 src/sys/arch/x86/x86/fpu.c:1.87
--- src/sys/arch/x86/x86/fpu.c:1.86	Fri Mar  3 14:40:16 2023
+++ src/sys/arch/x86/x86/fpu.c	Tue Jul 18 12:34:25 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: fpu.c,v 1.86 2023/03/03 14:40:16 riastradh Exp $	*/
+/*	$NetBSD: fpu.c,v 1.87 2023/07/18 12:34:25 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.  All
@@ -96,8 +96,9 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.86 2023/03/03 14:40:16 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.87 2023/07/18 12:34:25 riastradh Exp $");
 
+#include "opt_ddb.h"
 #include "opt_multiprocessor.h"
 
 #include 
@@ -121,6 +122,10 @@ __KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.86
 #include 
 #include 
 
+#ifdef DDB
+#include 
+#endif
+
 #ifdef XENPV
 #define clts() HYPERVISOR_fpu_taskswitch(0)
 #define stts() HYPERVISOR_fpu_taskswitch(1)
@@ -560,7 +565,16 @@ fputrap(struct trapframe *frame)
 	ksiginfo_t ksi;
 
 	if (__predict_false(!USERMODE(frame->tf_cs))) {
-		panic("fpu trap from kernel, trapframe %p\n", frame);
+		register_t ip = X86_TF_RIP(frame);
+		char where[128];
+
+#ifdef DDB
+		db_symstr(where, sizeof(where), (db_expr_t)ip, DB_STGY_PROC);
+#else
+		snprintf(where, sizeof(where), "%p", (void *)ip);
+#endif
+		panic("fpu trap from kernel at %s, trapframe %p\n", where,
+		frame);
 	}
 
 	KASSERT(curlwp->l_md.md_flags & MDL_FPU_IN_CPU);



CVS commit: src/sys/arch/x86/x86

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 12:34:25 UTC 2023

Modified Files:
src/sys/arch/x86/x86: fpu.c

Log Message:
x86/fpu: In kernel mode fpu traps, print the instruction pointer.


To generate a diff of this commit:
cvs rdiff -u -r1.86 -r1.87 src/sys/arch/x86/x86/fpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/kern

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 11:57:37 UTC 2023

Modified Files:
src/sys/kern: subr_autoconf.c

Log Message:
autoconf(9): Print `waiting for devices' normally once a minute.


To generate a diff of this commit:
cvs rdiff -u -r1.313 -r1.314 src/sys/kern/subr_autoconf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/subr_autoconf.c
diff -u src/sys/kern/subr_autoconf.c:1.313 src/sys/kern/subr_autoconf.c:1.314
--- src/sys/kern/subr_autoconf.c:1.313	Tue May 23 08:16:43 2023
+++ src/sys/kern/subr_autoconf.c	Tue Jul 18 11:57:37 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_autoconf.c,v 1.313 2023/05/23 08:16:43 riastradh Exp $ */
+/* $NetBSD: subr_autoconf.c,v 1.314 2023/07/18 11:57:37 riastradh Exp $ */
 
 /*
  * Copyright (c) 1996, 2000 Christopher G. Demetriou
@@ -77,7 +77,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.313 2023/05/23 08:16:43 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.314 2023/07/18 11:57:37 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -2690,6 +2690,7 @@ config_finalize(void)
 	struct finalize_hook *f;
 	struct pdevinit *pdev;
 	extern struct pdevinit pdevinit[];
+	unsigned t0 = getticks();
 	int errcnt, rv;
 
 	/*
@@ -2698,17 +2699,27 @@ config_finalize(void)
 	 */
 	mutex_enter(&config_misc_lock);
 	while (!TAILQ_EMPTY(&config_pending)) {
-		device_t dev;
-		int error;
+		const unsigned t1 = getticks();
 
-		error = cv_timedwait(&config_misc_cv, &config_misc_lock,
-		mstohz(1000));
-		if (error == EWOULDBLOCK) {
-			aprint_debug("waiting for devices:");
+		if (t1 - t0 >= hz) {
+			void (*pr)(const char *, ...) __printflike(1,2);
+			device_t dev;
+
+			if (t1 - t0 >= 60*hz) {
+pr = aprint_normal;
+t0 = t1;
+			} else {
+pr = aprint_debug;
+			}
+
+			(*pr)("waiting for devices:");
 			TAILQ_FOREACH(dev, &config_pending, dv_pending_list)
-aprint_debug(" %s", device_xname(dev));
-			aprint_debug("\n");
+(*pr)(" %s", device_xname(dev));
+			(*pr)("\n");
 		}
+
+		(void)cv_timedwait(&config_misc_cv, &config_misc_lock,
+		mstohz(1000));
 	}
 	mutex_exit(&config_misc_lock);
 



CVS commit: src/sys/kern

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 11:57:37 UTC 2023

Modified Files:
src/sys/kern: subr_autoconf.c

Log Message:
autoconf(9): Print `waiting for devices' normally once a minute.


To generate a diff of this commit:
cvs rdiff -u -r1.313 -r1.314 src/sys/kern/subr_autoconf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/sys

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 11:46:48 UTC 2023

Modified Files:
src/sys/sys: rbtree.h

Log Message:
rbtree(3): Fix RBDEBUG build with RB_TREE_INITIALIZER.

XXX pullup-10


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/sys/rbtree.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/sys/rbtree.h
diff -u src/sys/sys/rbtree.h:1.6 src/sys/sys/rbtree.h:1.7
--- src/sys/sys/rbtree.h:1.6	Tue Jul 18 11:43:21 2023
+++ src/sys/sys/rbtree.h	Tue Jul 18 11:46:47 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: rbtree.h,v 1.6 2023/07/18 11:43:21 riastradh Exp $	*/
+/*	$NetBSD: rbtree.h,v 1.7 2023/07/18 11:46:47 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -127,7 +127,7 @@ TAILQ_HEAD(rb_node_qh, rb_node);
 #define	RB_TAILQ_INSERT_AFTER(a, b, c, d)	TAILQ_INSERT_AFTER(a, b, c, d)
 
 #define	RBDEBUG_TREE_INITIALIZER(t)	  \
-	.rbt_nodes = TAILQ_INITIALIZER((t).rbt_nodes),
+	.rbt_nodes = TAILQ_HEAD_INITIALIZER((t).rbt_nodes),
 #else
 #define	RB_TAILQ_REMOVE(a, b, c)		do { } while (/*CONSTCOND*/0)
 #define	RB_TAILQ_INIT(a)			do { } while (/*CONSTCOND*/0)



CVS commit: src/sys/sys

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 11:46:48 UTC 2023

Modified Files:
src/sys/sys: rbtree.h

Log Message:
rbtree(3): Fix RBDEBUG build with RB_TREE_INITIALIZER.

XXX pullup-10


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/sys/rbtree.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/lib/libc

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 11:44:32 UTC 2023

Modified Files:
src/lib/libc/misc: initfini.c
src/lib/libc/stdlib: _env.c

Log Message:
libc: Use RB_TREE_INITIALIZER to nix initfini.c/_env.c coupling.

Doesn't actually help to remove _env.c from programs that don't use
it, though, because various internal things like __diagassert13 still
call getenv.


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/lib/libc/misc/initfini.c
cvs rdiff -u -r1.13 -r1.14 src/lib/libc/stdlib/_env.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/misc/initfini.c
diff -u src/lib/libc/misc/initfini.c:1.15 src/lib/libc/misc/initfini.c:1.16
--- src/lib/libc/misc/initfini.c:1.15	Tue Apr 20 21:42:32 2021
+++ src/lib/libc/misc/initfini.c	Tue Jul 18 11:44:32 2023
@@ -1,4 +1,4 @@
-/* 	$NetBSD: initfini.c,v 1.15 2021/04/20 21:42:32 christos Exp $	 */
+/* 	$NetBSD: initfini.c,v 1.16 2023/07/18 11:44:32 riastradh Exp $	 */
 
 /*-
  * Copyright (c) 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: initfini.c,v 1.15 2021/04/20 21:42:32 christos Exp $");
+__RCSID("$NetBSD: initfini.c,v 1.16 2023/07/18 11:44:32 riastradh Exp $");
 
 #ifdef _LIBC
 #include "namespace.h"
@@ -132,7 +132,4 @@ _libc_init(void)
 
 	/* Initialize the atexit mutexes */
 	__libc_atexit_init();
-
-	/* Initialize environment memory RB tree. */
-	__libc_env_init();
 }

Index: src/lib/libc/stdlib/_env.c
diff -u src/lib/libc/stdlib/_env.c:1.13 src/lib/libc/stdlib/_env.c:1.14
--- src/lib/libc/stdlib/_env.c:1.13	Sat Mar 12 17:31:39 2022
+++ src/lib/libc/stdlib/_env.c	Tue Jul 18 11:44:32 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: _env.c,v 1.13 2022/03/12 17:31:39 christos Exp $ */
+/*	$NetBSD: _env.c,v 1.14 2023/07/18 11:44:32 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include 
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: _env.c,v 1.13 2022/03/12 17:31:39 christos Exp $");
+__RCSID("$NetBSD: _env.c,v 1.14 2023/07/18 11:44:32 riastradh Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include "namespace.h"
@@ -73,7 +73,8 @@ static const rb_tree_ops_t env_tree_ops 
 };
 
 /* The single instance of above tree. */
-static rb_tree_t	env_tree;
+static rb_tree_t	env_tree =
+RB_TREE_INITIALIZER(env_tree, &env_tree_ops);
 
 /* The allocated environment. */
 static char	**allocated_environ;
@@ -401,10 +402,3 @@ __unlockenv(void)
 }
 
 #endif
-
-/* Initialize environment memory RB tree. */
-void __section(".text.startup")
-__libc_env_init(void)
-{
-	rb_tree_init(&env_tree, &env_tree_ops);
-}



CVS commit: src/lib/libc

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 11:44:32 UTC 2023

Modified Files:
src/lib/libc/misc: initfini.c
src/lib/libc/stdlib: _env.c

Log Message:
libc: Use RB_TREE_INITIALIZER to nix initfini.c/_env.c coupling.

Doesn't actually help to remove _env.c from programs that don't use
it, though, because various internal things like __diagassert13 still
call getenv.


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/lib/libc/misc/initfini.c
cvs rdiff -u -r1.13 -r1.14 src/lib/libc/stdlib/_env.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/sys

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 11:43:21 UTC 2023

Modified Files:
src/sys/sys: rbtree.h

Log Message:
rbtree(3): New RB_TREE_INITIALIZER macro.

Allows static initialization of an rbtree.

XXX pullup-10


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/sys/rbtree.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/sys/rbtree.h
diff -u src/sys/sys/rbtree.h:1.5 src/sys/sys/rbtree.h:1.6
--- src/sys/sys/rbtree.h:1.5	Thu Mar  7 14:39:21 2019
+++ src/sys/sys/rbtree.h	Tue Jul 18 11:43:21 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: rbtree.h,v 1.5 2019/03/07 14:39:21 roy Exp $	*/
+/*	$NetBSD: rbtree.h,v 1.6 2023/07/18 11:43:21 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -125,12 +125,17 @@ TAILQ_HEAD(rb_node_qh, rb_node);
 #define	RB_TAILQ_INSERT_HEAD(a, b, c)		TAILQ_INSERT_HEAD(a, b, c)
 #define	RB_TAILQ_INSERT_BEFORE(a, b, c)		TAILQ_INSERT_BEFORE(a, b, c)
 #define	RB_TAILQ_INSERT_AFTER(a, b, c, d)	TAILQ_INSERT_AFTER(a, b, c, d)
+
+#define	RBDEBUG_TREE_INITIALIZER(t)	  \
+	.rbt_nodes = TAILQ_INITIALIZER((t).rbt_nodes),
 #else
 #define	RB_TAILQ_REMOVE(a, b, c)		do { } while (/*CONSTCOND*/0)
 #define	RB_TAILQ_INIT(a)			do { } while (/*CONSTCOND*/0)
 #define	RB_TAILQ_INSERT_HEAD(a, b, c)		do { } while (/*CONSTCOND*/0)
 #define	RB_TAILQ_INSERT_BEFORE(a, b, c)		do { } while (/*CONSTCOND*/0)
 #define	RB_TAILQ_INSERT_AFTER(a, b, c, d)	do { } while (/*CONSTCOND*/0)
+
+#define	RBDEBUG_TREE_INITIALIZER(t)		/* nothing */
 #endif /* RBDEBUG */
 
 /*
@@ -181,6 +186,15 @@ typedef struct rb_tree {
 #define	RBSTAT_DEC(v)	do { } while (/*CONSTCOND*/0)
 #endif
 
+#define	RB_TREE_INIT_TYPECHECK(t)	  \
+	0*sizeof(&(t) - (struct rb_tree *)0)
+
+#define	RB_TREE_INITIALIZER(t, ops) (rb_tree_t)  \
+{	  \
+	.rbt_ops = (ops) + RB_TREE_INIT_TYPECHECK(t),			  \
+	RBDEBUG_TREE_INITIALIZER(t)	  \
+}
+
 void	rb_tree_init(rb_tree_t *, const rb_tree_ops_t *);
 void *	rb_tree_insert_node(rb_tree_t *, void *);
 void *	rb_tree_find_node(rb_tree_t *, const void *);



CVS commit: src/sys/sys

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 11:43:21 UTC 2023

Modified Files:
src/sys/sys: rbtree.h

Log Message:
rbtree(3): New RB_TREE_INITIALIZER macro.

Allows static initialization of an rbtree.

XXX pullup-10


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/sys/rbtree.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:17:12 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): One more debug message about read/write polling timeout.


To generate a diff of this commit:
cvs rdiff -u -r1.107 -r1.108 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.107 src/sys/dev/acpi/acpi_ec.c:1.108
--- src/sys/dev/acpi/acpi_ec.c:1.107	Tue Jul 18 10:17:02 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:17:12 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.107 2023/07/18 10:17:02 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.108 2023/07/18 10:17:12 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.107 2023/07/18 10:17:02 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.108 2023/07/18 10:17:12 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -688,6 +688,7 @@ acpiec_wait_timeout(struct acpiec_softc 
 		delay(1);
 	}
 
+	DPRINTF(ACPIEC_DEBUG_RW, sc, "SCI polling timeout\n");
 	if (cold || acpiec_cold) {
 		int timeo = 1000 * EC_CMD_TIMEOUT;
 



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:17:12 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): One more debug message about read/write polling timeout.


To generate a diff of this commit:
cvs rdiff -u -r1.107 -r1.108 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:17:02 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Take a lock around acpiec_cold updates.

Otherwise we race with readers -- probably harmlessly, but let's
avoid the appearance of problems.

XXX Maybe acpiec_suspend and acpiec_shutdown should interrupt
transactions and force them to fail promptly?

XXX This looks bad because acpiec_cold is global and sc->sc_mtx
doesn't look like it's global, but we expect to have only one
acpiec(4) device anyway from what I understand.  Maybe we should move
acpiec_cold into the softc?


To generate a diff of this commit:
cvs rdiff -u -r1.106 -r1.107 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:17:02 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Take a lock around acpiec_cold updates.

Otherwise we race with readers -- probably harmlessly, but let's
avoid the appearance of problems.

XXX Maybe acpiec_suspend and acpiec_shutdown should interrupt
transactions and force them to fail promptly?

XXX This looks bad because acpiec_cold is global and sc->sc_mtx
doesn't look like it's global, but we expect to have only one
acpiec(4) device anyway from what I understand.  Maybe we should move
acpiec_cold into the softc?


To generate a diff of this commit:
cvs rdiff -u -r1.106 -r1.107 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.106 src/sys/dev/acpi/acpi_ec.c:1.107
--- src/sys/dev/acpi/acpi_ec.c:1.106	Tue Jul 18 10:10:49 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:17:02 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.106 2023/07/18 10:10:49 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.107 2023/07/18 10:17:02 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.106 2023/07/18 10:10:49 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.107 2023/07/18 10:17:02 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -477,8 +477,23 @@ post_data_map:
 static bool
 acpiec_suspend(device_t dv, const pmf_qual_t *qual)
 {
+	struct acpiec_softc *sc = device_private(dv);
 
+	/*
+	 * XXX This looks bad because acpiec_cold is global and
+	 * sc->sc_mtx doesn't look like it's global, but we can have
+	 * only one acpiec(4) device anyway.  Maybe acpiec_cold should
+	 * live in the softc to make this look less bad?
+	 *
+	 * XXX Should this block read/write/query transactions until
+	 * resume?
+	 *
+	 * XXX Should this interrupt existing transactions to make them
+	 * fail promptly or restart on resume?
+	 */
+	mutex_enter(&sc->sc_mtx);
 	acpiec_cold = true;
+	mutex_exit(&sc->sc_mtx);
 
 	return true;
 }
@@ -486,8 +501,11 @@ acpiec_suspend(device_t dv, const pmf_qu
 static bool
 acpiec_resume(device_t dv, const pmf_qual_t *qual)
 {
+	struct acpiec_softc *sc = device_private(dv);
 
+	mutex_enter(&sc->sc_mtx);
 	acpiec_cold = false;
+	mutex_exit(&sc->sc_mtx);
 
 	return true;
 }
@@ -495,8 +513,12 @@ acpiec_resume(device_t dv, const pmf_qua
 static bool
 acpiec_shutdown(device_t dv, int how)
 {
+	struct acpiec_softc *sc = device_private(dv);
 
+	mutex_enter(&sc->sc_mtx);
 	acpiec_cold = true;
+	mutex_exit(&sc->sc_mtx);
+
 	return true;
 }
 



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:10:49 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Factor out if (state == FREE) cv_signal(sc_cv).

In principle this could have a functional change, but at worst, it is
to signal more wakeups than needed, which should always be safe.


To generate a diff of this commit:
cvs rdiff -u -r1.105 -r1.106 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.105 src/sys/dev/acpi/acpi_ec.c:1.106
--- src/sys/dev/acpi/acpi_ec.c:1.105	Tue Jul 18 10:06:55 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:10:49 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.105 2023/07/18 10:06:55 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.106 2023/07/18 10:10:49 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.105 2023/07/18 10:06:55 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.106 2023/07/18 10:10:49 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -967,7 +967,6 @@ acpiec_gpe_state_machine(struct acpiec_s
 			break; /* Nothing of interest here. */
 		sc->sc_cur_val = acpiec_read_data(sc);
 		sc->sc_state = EC_STATE_FREE;
-		cv_signal(&sc->sc_cv);
 		break;
 
 	case EC_STATE_READ:
@@ -989,7 +988,6 @@ acpiec_gpe_state_machine(struct acpiec_s
 			break; /* Nothing of interest here. */
 		sc->sc_cur_val = acpiec_read_data(sc);
 		sc->sc_state = EC_STATE_FREE;
-		cv_signal(&sc->sc_cv);
 		break;
 
 	case EC_STATE_WRITE:
@@ -1009,9 +1007,8 @@ acpiec_gpe_state_machine(struct acpiec_s
 	case EC_STATE_WRITE_VAL:
 		if ((reg & EC_STATUS_IBF) != 0)
 			break; /* Nothing of interest here. */
-		sc->sc_state = EC_STATE_FREE;
-		cv_signal(&sc->sc_cv);
 		acpiec_write_data(sc, sc->sc_cur_val);
+		sc->sc_state = EC_STATE_FREE;
 		break;
 
 	case EC_STATE_FREE:
@@ -1022,10 +1019,12 @@ acpiec_gpe_state_machine(struct acpiec_s
 	}
 
 	/*
-	 * If we just ended a transaction, and an SCI was requested,
-	 * notify the SCI thread.
+	 * If we are not in a transaction, wake anyone waiting to start
+	 * one.  If an SCI was requested, notify the SCI thread that it
+	 * needs to handle the SCI.
 	 */
 	if (sc->sc_state == EC_STATE_FREE) {
+		cv_signal(&sc->sc_cv);
 		if (reg & EC_STATUS_SCI) {
 			DPRINTF(ACPIEC_DEBUG_TRANSITION, sc,
 			"wake SCI thread\n");



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:10:49 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Factor out if (state == FREE) cv_signal(sc_cv).

In principle this could have a functional change, but at worst, it is
to signal more wakeups than needed, which should always be safe.


To generate a diff of this commit:
cvs rdiff -u -r1.105 -r1.106 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:06:55 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Pass softc, not device_t, to acpiec_space_handler.

Better to keep the device_t isolated to public interfaces.  Simpler
internally this way.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.104 -r1.105 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.104 src/sys/dev/acpi/acpi_ec.c:1.105
--- src/sys/dev/acpi/acpi_ec.c:1.104	Tue Jul 18 10:06:44 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:06:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.104 2023/07/18 10:06:44 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.105 2023/07/18 10:06:55 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.104 2023/07/18 10:06:44 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.105 2023/07/18 10:06:55 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -425,7 +425,7 @@ acpiec_common_attach(device_t parent, de
 	callout_setfunc(&sc->sc_pseudo_intr, acpiec_callout, sc);
 
 	rv = AcpiInstallAddressSpaceHandler(sc->sc_ech, ACPI_ADR_SPACE_EC,
-	acpiec_space_handler, acpiec_space_setup, self);
+	acpiec_space_handler, acpiec_space_setup, sc);
 	if (rv != AE_OK) {
 		aprint_error_dev(self,
 		"unable to install address space handler: %s\n",
@@ -777,8 +777,8 @@ out:	mutex_exit(&sc->sc_mtx);
  *
  *	Transfer bitwidth/8 bytes of data between paddr and *value:
  *	from paddr to *value when func is ACPI_READ, and the other way
- *	when func is ACPI_WRITE.  arg is the acpiec(4) or acpiecdt(4)
- *	device.  region_arg is ignored (XXX why? determined by
+ *	when func is ACPI_WRITE.  arg is the acpiec_softc pointer.
+ *	region_arg is ignored (XXX why? determined by
  *	acpiec_space_setup but never used by anything that I can see).
  *
  *	The caller always provides storage at *value large enough for
@@ -808,8 +808,7 @@ static ACPI_STATUS
 acpiec_space_handler(uint32_t func, ACPI_PHYSICAL_ADDRESS paddr,
 uint32_t width, ACPI_INTEGER *value, void *arg, void *region_arg)
 {
-	device_t dv = arg;
-	struct acpiec_softc *sc = device_private(dv);
+	struct acpiec_softc *sc = arg;
 	ACPI_STATUS rv;
 	uint8_t addr, *buf;
 	unsigned int i;
@@ -1074,13 +1073,17 @@ acpiec_gpe_handler(ACPI_HANDLE hdl, uint
 ACPI_STATUS
 acpiec_bus_read(device_t dv, u_int addr, ACPI_INTEGER *val, int width)
 {
-	return acpiec_space_handler(ACPI_READ, addr, width * 8, val, dv, NULL);
+	struct acpiec_softc *sc = device_private(dv);
+
+	return acpiec_space_handler(ACPI_READ, addr, width * 8, val, sc, NULL);
 }
 
 ACPI_STATUS
 acpiec_bus_write(device_t dv, u_int addr, ACPI_INTEGER val, int width)
 {
-	return acpiec_space_handler(ACPI_WRITE, addr, width * 8, &val, dv,
+	struct acpiec_softc *sc = device_private(dv);
+
+	return acpiec_space_handler(ACPI_WRITE, addr, width * 8, &val, sc,
 	NULL);
 }
 



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:06:55 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Pass softc, not device_t, to acpiec_space_handler.

Better to keep the device_t isolated to public interfaces.  Simpler
internally this way.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.104 -r1.105 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:06:44 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Pass softc, not device_t, to acpiec_gpe_query thread.

Simpler.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.103 -r1.104 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.103 src/sys/dev/acpi/acpi_ec.c:1.104
--- src/sys/dev/acpi/acpi_ec.c:1.103	Tue Jul 18 10:06:33 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:06:44 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.103 2023/07/18 10:06:33 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.104 2023/07/18 10:06:44 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.103 2023/07/18 10:06:33 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.104 2023/07/18 10:06:44 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -449,7 +449,7 @@ acpiec_common_attach(device_t parent, de
 	}
 
 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, acpiec_gpe_query,
-		   self, NULL, "acpiec sci thread")) {
+		sc, NULL, "acpiec sci thread")) {
 		aprint_error_dev(self, "unable to create query kthread\n");
 		goto post_csr_map;
 	}
@@ -882,8 +882,7 @@ acpiec_wait(struct acpiec_softc *sc)
 static void
 acpiec_gpe_query(void *arg)
 {
-	device_t dv = arg;
-	struct acpiec_softc *sc = device_private(dv);
+	struct acpiec_softc *sc = arg;
 	uint8_t reg;
 	char qxx[5];
 	ACPI_STATUS rv;
@@ -931,7 +930,7 @@ loop:
 	snprintf(qxx, sizeof(qxx), "_Q%02X", (unsigned int)reg);
 	rv = AcpiEvaluateObject(sc->sc_ech, qxx, NULL, NULL);
 	if (rv != AE_OK && rv != AE_NOT_FOUND) {
-		aprint_error_dev(dv, "GPE query method %s failed: %s",
+		aprint_error_dev(sc->sc_dev, "GPE query method %s failed: %s",
 		qxx, AcpiFormatException(rv));
 	}
 



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:06:44 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Pass softc, not device_t, to acpiec_gpe_query thread.

Simpler.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.103 -r1.104 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:06:33 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Pass softc, not device_t, to acpiec_read/write.

Simpler, type-safer.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.102 -r1.103 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.102 src/sys/dev/acpi/acpi_ec.c:1.103
--- src/sys/dev/acpi/acpi_ec.c:1.102	Tue Jul 18 10:06:22 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:06:33 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.102 2023/07/18 10:06:22 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.103 2023/07/18 10:06:33 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.102 2023/07/18 10:06:22 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.103 2023/07/18 10:06:33 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -697,9 +697,8 @@ acpiec_wait_timeout(struct acpiec_softc 
 }
 
 static ACPI_STATUS
-acpiec_read(device_t dv, uint8_t addr, uint8_t *val)
+acpiec_read(struct acpiec_softc *sc, uint8_t addr, uint8_t *val)
 {
-	struct acpiec_softc *sc = device_private(dv);
 	ACPI_STATUS rv;
 
 	acpiec_lock(sc);
@@ -736,9 +735,8 @@ out:	mutex_exit(&sc->sc_mtx);
 }
 
 static ACPI_STATUS
-acpiec_write(device_t dv, uint8_t addr, uint8_t val)
+acpiec_write(struct acpiec_softc *sc, uint8_t addr, uint8_t val)
 {
-	struct acpiec_softc *sc = device_private(dv);
 	ACPI_STATUS rv;
 
 	acpiec_lock(sc);
@@ -810,7 +808,8 @@ static ACPI_STATUS
 acpiec_space_handler(uint32_t func, ACPI_PHYSICAL_ADDRESS paddr,
 uint32_t width, ACPI_INTEGER *value, void *arg, void *region_arg)
 {
-	device_t dv;
+	device_t dv = arg;
+	struct acpiec_softc *sc = device_private(dv);
 	ACPI_STATUS rv;
 	uint8_t addr, *buf;
 	unsigned int i;
@@ -820,7 +819,6 @@ acpiec_space_handler(uint32_t func, ACPI
 		return AE_BAD_PARAMETER;
 
 	addr = paddr;
-	dv = arg;
 	buf = (uint8_t *)value;
 
 	rv = AE_OK;
@@ -828,7 +826,7 @@ acpiec_space_handler(uint32_t func, ACPI
 	switch (func) {
 	case ACPI_READ:
 		for (i = 0; i < width; i += 8, ++addr, ++buf) {
-			rv = acpiec_read(dv, addr, buf);
+			rv = acpiec_read(sc, addr, buf);
 			if (rv != AE_OK)
 break;
 		}
@@ -841,14 +839,15 @@ acpiec_space_handler(uint32_t func, ACPI
 		break;
 	case ACPI_WRITE:
 		for (i = 0; i < width; i += 8, ++addr, ++buf) {
-			rv = acpiec_write(dv, addr, *buf);
+			rv = acpiec_write(sc, addr, *buf);
 			if (rv != AE_OK)
 break;
 		}
 		break;
 	default:
-		aprint_error("%s: invalid Address Space function called: %x\n",
-		device_xname(dv), (unsigned int)func);
+		aprint_error_dev(sc->sc_dev,
+		"invalid Address Space function called: %x\n",
+		(unsigned int)func);
 		return AE_BAD_PARAMETER;
 	}
 



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:06:33 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Pass softc, not device_t, to acpiec_read/write.

Simpler, type-safer.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.102 -r1.103 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:06:22 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Pass softc, not device_t, to acpiec_lock/unlock.

Simpler, type-safer.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.101 -r1.102 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.101 src/sys/dev/acpi/acpi_ec.c:1.102
--- src/sys/dev/acpi/acpi_ec.c:1.101	Tue Jul 18 10:06:11 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:06:22 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.101 2023/07/18 10:06:11 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.102 2023/07/18 10:06:22 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.101 2023/07/18 10:06:11 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.102 2023/07/18 10:06:22 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -619,9 +619,8 @@ acpiec_space_setup(ACPI_HANDLE region, u
 }
 
 static void
-acpiec_lock(device_t dv)
+acpiec_lock(struct acpiec_softc *sc)
 {
-	struct acpiec_softc *sc = device_private(dv);
 	ACPI_STATUS rv;
 
 	mutex_enter(&sc->sc_access_mtx);
@@ -630,7 +629,7 @@ acpiec_lock(device_t dv)
 		rv = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT,
 		&sc->sc_global_lock);
 		if (rv != AE_OK) {
-			aprint_error_dev(dv,
+			aprint_error_dev(sc->sc_dev,
 			"failed to acquire global lock: %s\n",
 			AcpiFormatException(rv));
 			return;
@@ -639,15 +638,14 @@ acpiec_lock(device_t dv)
 }
 
 static void
-acpiec_unlock(device_t dv)
+acpiec_unlock(struct acpiec_softc *sc)
 {
-	struct acpiec_softc *sc = device_private(dv);
 	ACPI_STATUS rv;
 
 	if (sc->sc_need_global_lock) {
 		rv = AcpiReleaseGlobalLock(sc->sc_global_lock);
 		if (rv != AE_OK) {
-			aprint_error_dev(dv,
+			aprint_error_dev(sc->sc_dev,
 			"failed to release global lock: %s\n",
 			AcpiFormatException(rv));
 		}
@@ -704,7 +702,7 @@ acpiec_read(device_t dv, uint8_t addr, u
 	struct acpiec_softc *sc = device_private(dv);
 	ACPI_STATUS rv;
 
-	acpiec_lock(dv);
+	acpiec_lock(sc);
 	mutex_enter(&sc->sc_mtx);
 
 	DPRINTF(ACPIEC_DEBUG_RW, sc,
@@ -733,7 +731,7 @@ acpiec_read(device_t dv, uint8_t addr, u
 	*val = sc->sc_cur_val;
 
 out:	mutex_exit(&sc->sc_mtx);
-	acpiec_unlock(dv);
+	acpiec_unlock(sc);
 	return rv;
 }
 
@@ -743,7 +741,7 @@ acpiec_write(device_t dv, uint8_t addr, 
 	struct acpiec_softc *sc = device_private(dv);
 	ACPI_STATUS rv;
 
-	acpiec_lock(dv);
+	acpiec_lock(sc);
 	mutex_enter(&sc->sc_mtx);
 
 	DPRINTF(ACPIEC_DEBUG_RW, sc,
@@ -772,7 +770,7 @@ acpiec_write(device_t dv, uint8_t addr, 
 	addr, val);
 
 out:	mutex_exit(&sc->sc_mtx);
-	acpiec_unlock(dv);
+	acpiec_unlock(sc);
 	return rv;
 }
 
@@ -905,7 +903,7 @@ loop:
 	 * EC wants to submit a query to us.  Exclude concurrent reads
 	 * and writes while we handle it.
 	 */
-	acpiec_lock(dv);
+	acpiec_lock(sc);
 	mutex_enter(&sc->sc_mtx);
 
 	DPRINTF(ACPIEC_DEBUG_QUERY, sc, "SCI query\n");
@@ -923,7 +921,7 @@ loop:
 	DPRINTF(ACPIEC_DEBUG_QUERY, sc, "SCI query: 0x%"PRIx8"\n", reg);
 
 	mutex_exit(&sc->sc_mtx);
-	acpiec_unlock(dv);
+	acpiec_unlock(sc);
 
 	if (reg == 0)
 		goto loop; /* Spurious query result */



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:06:22 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Pass softc, not device_t, to acpiec_lock/unlock.

Simpler, type-safer.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.101 -r1.102 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:06:12 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Pass softc, not device_t, to acpiec_gpe_handler.

Simpler.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.100 -r1.101 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.100 src/sys/dev/acpi/acpi_ec.c:1.101
--- src/sys/dev/acpi/acpi_ec.c:1.100	Tue Jul 18 10:06:00 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:06:11 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.100 2023/07/18 10:06:00 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.101 2023/07/18 10:06:11 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.100 2023/07/18 10:06:00 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.101 2023/07/18 10:06:11 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -434,7 +434,7 @@ acpiec_common_attach(device_t parent, de
 	}
 
 	rv = AcpiInstallGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
-	ACPI_GPE_EDGE_TRIGGERED, acpiec_gpe_handler, self);
+	ACPI_GPE_EDGE_TRIGGERED, acpiec_gpe_handler, sc);
 	if (rv != AE_OK) {
 		aprint_error_dev(self, "unable to install GPE handler: %s\n",
 		AcpiFormatException(rv));
@@ -1065,8 +1065,7 @@ acpiec_callout(void *arg)
 static uint32_t
 acpiec_gpe_handler(ACPI_HANDLE hdl, uint32_t gpebit, void *arg)
 {
-	device_t dv = arg;
-	struct acpiec_softc *sc = device_private(dv);
+	struct acpiec_softc *sc = arg;
 
 	mutex_enter(&sc->sc_mtx);
 	DPRINTF(ACPIEC_DEBUG_INTR, sc, "GPE\n");



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:06:12 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Pass softc, not device_t, to acpiec_gpe_handler.

Simpler.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.100 -r1.101 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:06:00 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Pass softc, not device_t, to acpiec_callout.

Simpler.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.99 -r1.100 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:06:00 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Pass softc, not device_t, to acpiec_callout.

Simpler.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.99 -r1.100 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.99 src/sys/dev/acpi/acpi_ec.c:1.100
--- src/sys/dev/acpi/acpi_ec.c:1.99	Tue Jul 18 10:05:49 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:06:00 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.99 2023/07/18 10:05:49 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.100 2023/07/18 10:06:00 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.99 2023/07/18 10:05:49 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.100 2023/07/18 10:06:00 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -422,7 +422,7 @@ acpiec_common_attach(device_t parent, de
 		aprint_normal_dev(self, "using global ACPI lock\n");
 
 	callout_init(&sc->sc_pseudo_intr, CALLOUT_MPSAFE);
-	callout_setfunc(&sc->sc_pseudo_intr, acpiec_callout, self);
+	callout_setfunc(&sc->sc_pseudo_intr, acpiec_callout, sc);
 
 	rv = AcpiInstallAddressSpaceHandler(sc->sc_ech, ACPI_ADR_SPACE_EC,
 	acpiec_space_handler, acpiec_space_setup, self);
@@ -1054,8 +1054,7 @@ acpiec_gpe_state_machine(struct acpiec_s
 static void
 acpiec_callout(void *arg)
 {
-	device_t dv = arg;
-	struct acpiec_softc *sc = device_private(dv);
+	struct acpiec_softc *sc = arg;
 
 	mutex_enter(&sc->sc_mtx);
 	DPRINTF(ACPIEC_DEBUG_INTR, sc, "callout\n");



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:05:49 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Pass softc, not device_t, to acpiec_gpe_state_machine.

Simpler, type-safer.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.98 -r1.99 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.98 src/sys/dev/acpi/acpi_ec.c:1.99
--- src/sys/dev/acpi/acpi_ec.c:1.98	Tue Jul 18 10:05:24 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:05:49 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.98 2023/07/18 10:05:24 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.99 2023/07/18 10:05:49 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.98 2023/07/18 10:05:24 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.99 2023/07/18 10:05:49 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -235,7 +235,7 @@ static ACPI_STATUS acpiec_space_setup(AC
 static ACPI_STATUS acpiec_space_handler(uint32_t, ACPI_PHYSICAL_ADDRESS,
 uint32_t, ACPI_INTEGER *, void *, void *);
 
-static void acpiec_gpe_state_machine(device_t);
+static void acpiec_gpe_state_machine(struct acpiec_softc *);
 
 CFATTACH_DECL_NEW(acpiec, sizeof(struct acpiec_softc),
 acpiec_match, acpiec_attach, NULL, NULL);
@@ -662,7 +662,7 @@ acpiec_wait_timeout(struct acpiec_softc 
 	int i;
 
 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
-		acpiec_gpe_state_machine(dv);
+		acpiec_gpe_state_machine(sc);
 		if (sc->sc_state == EC_STATE_FREE)
 			return AE_OK;
 		delay(1);
@@ -673,7 +673,7 @@ acpiec_wait_timeout(struct acpiec_softc 
 
 		while (sc->sc_state != EC_STATE_FREE && timeo-- > 0) {
 			delay(1000);
-			acpiec_gpe_state_machine(dv);
+			acpiec_gpe_state_machine(sc);
 		}
 		if (sc->sc_state != EC_STATE_FREE) {
 			aprint_error_dev(dv, "command timed out, state %d\n",
@@ -860,14 +860,13 @@ acpiec_space_handler(uint32_t func, ACPI
 static void
 acpiec_wait(struct acpiec_softc *sc)
 {
-	device_t dv = sc->sc_dev;
 	int i;
 
 	/*
 	 * First, attempt to get the query by polling.
 	 */
 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
-		acpiec_gpe_state_machine(dv);
+		acpiec_gpe_state_machine(sc);
 		if (sc->sc_state == EC_STATE_FREE)
 			return;
 		delay(1);
@@ -943,9 +942,8 @@ loop:
 }
 
 static void
-acpiec_gpe_state_machine(device_t dv)
+acpiec_gpe_state_machine(struct acpiec_softc *sc)
 {
-	struct acpiec_softc *sc = device_private(dv);
 	uint8_t reg;
 
 	KASSERT(mutex_owned(&sc->sc_mtx));
@@ -1061,7 +1059,7 @@ acpiec_callout(void *arg)
 
 	mutex_enter(&sc->sc_mtx);
 	DPRINTF(ACPIEC_DEBUG_INTR, sc, "callout\n");
-	acpiec_gpe_state_machine(dv);
+	acpiec_gpe_state_machine(sc);
 	mutex_exit(&sc->sc_mtx);
 }
 
@@ -1073,7 +1071,7 @@ acpiec_gpe_handler(ACPI_HANDLE hdl, uint
 
 	mutex_enter(&sc->sc_mtx);
 	DPRINTF(ACPIEC_DEBUG_INTR, sc, "GPE\n");
-	acpiec_gpe_state_machine(dv);
+	acpiec_gpe_state_machine(sc);
 	mutex_exit(&sc->sc_mtx);
 
 	return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:05:49 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Pass softc, not device_t, to acpiec_gpe_state_machine.

Simpler, type-safer.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.98 -r1.99 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:05:25 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Factor wait logic out.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.97 -r1.98 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.97 src/sys/dev/acpi/acpi_ec.c:1.98
--- src/sys/dev/acpi/acpi_ec.c:1.97	Tue Jul 18 10:05:13 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:05:24 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.97 2023/07/18 10:05:13 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.98 2023/07/18 10:05:24 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.97 2023/07/18 10:05:13 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.98 2023/07/18 10:05:24 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -656,35 +656,21 @@ acpiec_unlock(device_t dv)
 }
 
 static ACPI_STATUS
-acpiec_read(device_t dv, uint8_t addr, uint8_t *val)
+acpiec_wait_timeout(struct acpiec_softc *sc)
 {
-	struct acpiec_softc *sc = device_private(dv);
-	int i, timeo = 1000 * EC_CMD_TIMEOUT;
-	ACPI_STATUS rv;
-
-	acpiec_lock(dv);
-	mutex_enter(&sc->sc_mtx);
-
-	DPRINTF(ACPIEC_DEBUG_RW, sc,
-	"pid %ld %s, lid %ld%s%s: read addr 0x%"PRIx8"\n",
-	(long)curproc->p_pid, curproc->p_comm,
-	(long)curlwp->l_lid, curlwp->l_name ? " " : "",
-	curlwp->l_name ? curlwp->l_name : "",
-	addr);
-
-	KASSERT(sc->sc_state == EC_STATE_FREE);
-
-	sc->sc_cur_addr = addr;
-	sc->sc_state = EC_STATE_READ;
+	device_t dv = sc->sc_dev;
+	int i;
 
 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
 		acpiec_gpe_state_machine(dv);
 		if (sc->sc_state == EC_STATE_FREE)
-			goto done;
+			return AE_OK;
 		delay(1);
 	}
 
 	if (cold || acpiec_cold) {
+		int timeo = 1000 * EC_CMD_TIMEOUT;
+
 		while (sc->sc_state != EC_STATE_FREE && timeo-- > 0) {
 			delay(1000);
 			acpiec_gpe_state_machine(dv);
@@ -692,8 +678,7 @@ acpiec_read(device_t dv, uint8_t addr, u
 		if (sc->sc_state != EC_STATE_FREE) {
 			aprint_error_dev(dv, "command timed out, state %d\n",
 			sc->sc_state);
-			rv = AE_ERROR;
-			goto out;
+			return AE_ERROR;
 		}
 	} else {
 		const unsigned deadline = getticks() + EC_CMD_TIMEOUT*hz;
@@ -706,12 +691,38 @@ acpiec_read(device_t dv, uint8_t addr, u
 			aprint_error_dev(dv,
 			"command takes over %d sec...\n",
 			EC_CMD_TIMEOUT);
-			rv = AE_ERROR;
-			goto out;
+			return AE_ERROR;
 		}
 	}
 
-done:
+	return AE_OK;
+}
+
+static ACPI_STATUS
+acpiec_read(device_t dv, uint8_t addr, uint8_t *val)
+{
+	struct acpiec_softc *sc = device_private(dv);
+	ACPI_STATUS rv;
+
+	acpiec_lock(dv);
+	mutex_enter(&sc->sc_mtx);
+
+	DPRINTF(ACPIEC_DEBUG_RW, sc,
+	"pid %ld %s, lid %ld%s%s: read addr 0x%"PRIx8"\n",
+	(long)curproc->p_pid, curproc->p_comm,
+	(long)curlwp->l_lid, curlwp->l_name ? " " : "",
+	curlwp->l_name ? curlwp->l_name : "",
+	addr);
+
+	KASSERT(sc->sc_state == EC_STATE_FREE);
+
+	sc->sc_cur_addr = addr;
+	sc->sc_state = EC_STATE_READ;
+
+	rv = acpiec_wait_timeout(sc);
+	if (ACPI_FAILURE(rv))
+		goto out;
+
 	DPRINTF(ACPIEC_DEBUG_RW, sc,
 	"pid %ld %s, lid %ld%s%s: read addr 0x%"PRIx8": 0x%"PRIx8"\n",
 	(long)curproc->p_pid, curproc->p_comm,
@@ -720,9 +731,8 @@ done:
 	addr, sc->sc_cur_val);
 
 	*val = sc->sc_cur_val;
-	rv = AE_OK;
-out:
-	mutex_exit(&sc->sc_mtx);
+
+out:	mutex_exit(&sc->sc_mtx);
 	acpiec_unlock(dv);
 	return rv;
 }
@@ -731,7 +741,6 @@ static ACPI_STATUS
 acpiec_write(device_t dv, uint8_t addr, uint8_t val)
 {
 	struct acpiec_softc *sc = device_private(dv);
-	int i, timeo = 1000 * EC_CMD_TIMEOUT;
 	ACPI_STATUS rv;
 
 	acpiec_lock(dv);
@@ -750,41 +759,10 @@ acpiec_write(device_t dv, uint8_t addr, 
 	sc->sc_cur_val = val;
 	sc->sc_state = EC_STATE_WRITE;
 
-	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
-		acpiec_gpe_state_machine(dv);
-		if (sc->sc_state == EC_STATE_FREE)
-			goto done;
-		delay(1);
-	}
-
-	if (cold || acpiec_cold) {
-		while (sc->sc_state != EC_STATE_FREE && timeo-- > 0) {
-			delay(1000);
-			acpiec_gpe_state_machine(dv);
-		}
-		if (sc->sc_state != EC_STATE_FREE) {
-			aprint_error_dev(dv, "command timed out, state %d\n",
-			sc->sc_state);
-			rv = AE_ERROR;
-			goto out;
-		}
-	} else {
-		const unsigned deadline = getticks() + EC_CMD_TIMEOUT*hz;
-		unsigned delta;
-
-		while (sc->sc_state != EC_STATE_FREE &&
-		(delta = deadline - getticks()) < INT_MAX)
-			(void)cv_timedwait(&sc->sc_cv, &sc->sc_mtx, delta);
-		if (sc->sc_state != EC_STATE_FREE) {
-			aprint_error_dev(dv,
-			"command takes over %d sec...\n",
-			EC_CMD_TIMEOUT);
-			rv = AE_ERROR;
-			goto out;
-		}
-	}
+	rv = acpiec_wait_timeout(sc);
+	if (ACPI_FAILURE(rv))
+		goto out;
 
-done:

CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:05:25 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Factor wait logic out.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.97 -r1.98 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:05:13 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Merge returns in acpiec_read/write.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.96 -r1.97 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:05:13 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Merge returns in acpiec_read/write.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.96 -r1.97 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.96 src/sys/dev/acpi/acpi_ec.c:1.97
--- src/sys/dev/acpi/acpi_ec.c:1.96	Tue Jul 18 10:05:01 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:05:13 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.96 2023/07/18 10:05:01 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.97 2023/07/18 10:05:13 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.96 2023/07/18 10:05:01 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.97 2023/07/18 10:05:13 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -660,6 +660,7 @@ acpiec_read(device_t dv, uint8_t addr, u
 {
 	struct acpiec_softc *sc = device_private(dv);
 	int i, timeo = 1000 * EC_CMD_TIMEOUT;
+	ACPI_STATUS rv;
 
 	acpiec_lock(dv);
 	mutex_enter(&sc->sc_mtx);
@@ -691,9 +692,8 @@ acpiec_read(device_t dv, uint8_t addr, u
 		if (sc->sc_state != EC_STATE_FREE) {
 			aprint_error_dev(dv, "command timed out, state %d\n",
 			sc->sc_state);
-			mutex_exit(&sc->sc_mtx);
-			acpiec_unlock(dv);
-			return AE_ERROR;
+			rv = AE_ERROR;
+			goto out;
 		}
 	} else {
 		const unsigned deadline = getticks() + EC_CMD_TIMEOUT*hz;
@@ -703,12 +703,11 @@ acpiec_read(device_t dv, uint8_t addr, u
 		(delta = deadline - getticks()) < INT_MAX)
 			(void)cv_timedwait(&sc->sc_cv, &sc->sc_mtx, delta);
 		if (sc->sc_state != EC_STATE_FREE) {
-			mutex_exit(&sc->sc_mtx);
-			acpiec_unlock(dv);
 			aprint_error_dev(dv,
 			"command takes over %d sec...\n",
 			EC_CMD_TIMEOUT);
-			return AE_ERROR;
+			rv = AE_ERROR;
+			goto out;
 		}
 	}
 
@@ -721,10 +720,11 @@ done:
 	addr, sc->sc_cur_val);
 
 	*val = sc->sc_cur_val;
-
+	rv = AE_OK;
+out:
 	mutex_exit(&sc->sc_mtx);
 	acpiec_unlock(dv);
-	return AE_OK;
+	return rv;
 }
 
 static ACPI_STATUS
@@ -732,6 +732,7 @@ acpiec_write(device_t dv, uint8_t addr, 
 {
 	struct acpiec_softc *sc = device_private(dv);
 	int i, timeo = 1000 * EC_CMD_TIMEOUT;
+	ACPI_STATUS rv;
 
 	acpiec_lock(dv);
 	mutex_enter(&sc->sc_mtx);
@@ -764,9 +765,8 @@ acpiec_write(device_t dv, uint8_t addr, 
 		if (sc->sc_state != EC_STATE_FREE) {
 			aprint_error_dev(dv, "command timed out, state %d\n",
 			sc->sc_state);
-			mutex_exit(&sc->sc_mtx);
-			acpiec_unlock(dv);
-			return AE_ERROR;
+			rv = AE_ERROR;
+			goto out;
 		}
 	} else {
 		const unsigned deadline = getticks() + EC_CMD_TIMEOUT*hz;
@@ -776,12 +776,11 @@ acpiec_write(device_t dv, uint8_t addr, 
 		(delta = deadline - getticks()) < INT_MAX)
 			(void)cv_timedwait(&sc->sc_cv, &sc->sc_mtx, delta);
 		if (sc->sc_state != EC_STATE_FREE) {
-			mutex_exit(&sc->sc_mtx);
-			acpiec_unlock(dv);
 			aprint_error_dev(dv,
 			"command takes over %d sec...\n",
 			EC_CMD_TIMEOUT);
-			return AE_ERROR;
+			rv = AE_ERROR;
+			goto out;
 		}
 	}
 
@@ -793,10 +792,11 @@ done:
 	(long)curlwp->l_lid, curlwp->l_name ? " " : "",
 	curlwp->l_name ? curlwp->l_name : "",
 	addr, val);
-
+	rv = AE_OK;
+out:
 	mutex_exit(&sc->sc_mtx);
 	acpiec_unlock(dv);
-	return AE_OK;
+	return rv;
 }
 
 /*



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:05:01 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Don't touch sc->sc_state outside sc->sc_mtx.


To generate a diff of this commit:
cvs rdiff -u -r1.95 -r1.96 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.95 src/sys/dev/acpi/acpi_ec.c:1.96
--- src/sys/dev/acpi/acpi_ec.c:1.95	Tue Jul 18 10:04:50 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:05:01 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.95 2023/07/18 10:04:50 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.96 2023/07/18 10:05:01 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.95 2023/07/18 10:04:50 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.96 2023/07/18 10:05:01 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -689,10 +689,10 @@ acpiec_read(device_t dv, uint8_t addr, u
 			acpiec_gpe_state_machine(dv);
 		}
 		if (sc->sc_state != EC_STATE_FREE) {
-			mutex_exit(&sc->sc_mtx);
-			acpiec_unlock(dv);
 			aprint_error_dev(dv, "command timed out, state %d\n",
 			sc->sc_state);
+			mutex_exit(&sc->sc_mtx);
+			acpiec_unlock(dv);
 			return AE_ERROR;
 		}
 	} else {
@@ -762,10 +762,10 @@ acpiec_write(device_t dv, uint8_t addr, 
 			acpiec_gpe_state_machine(dv);
 		}
 		if (sc->sc_state != EC_STATE_FREE) {
-			mutex_exit(&sc->sc_mtx);
-			acpiec_unlock(dv);
 			aprint_error_dev(dv, "command timed out, state %d\n",
 			sc->sc_state);
+			mutex_exit(&sc->sc_mtx);
+			acpiec_unlock(dv);
 			return AE_ERROR;
 		}
 	} else {



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:05:01 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Don't touch sc->sc_state outside sc->sc_mtx.


To generate a diff of this commit:
cvs rdiff -u -r1.95 -r1.96 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:04:50 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Fix cv_timedwait abuse in acpiec_read/write.


To generate a diff of this commit:
cvs rdiff -u -r1.94 -r1.95 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:04:50 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Fix cv_timedwait abuse in acpiec_read/write.


To generate a diff of this commit:
cvs rdiff -u -r1.94 -r1.95 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.94 src/sys/dev/acpi/acpi_ec.c:1.95
--- src/sys/dev/acpi/acpi_ec.c:1.94	Tue Jul 18 10:04:40 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:04:50 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.94 2023/07/18 10:04:40 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.95 2023/07/18 10:04:50 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.94 2023/07/18 10:04:40 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.95 2023/07/18 10:04:50 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -695,17 +695,21 @@ acpiec_read(device_t dv, uint8_t addr, u
 			sc->sc_state);
 			return AE_ERROR;
 		}
-	} else if (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
-		/*
-		 * XXX while (sc->sc_state != EC_STATE_FREE)
-		 *	cv_timedwait(...),
-		 * plus deadline
-		 */
-		mutex_exit(&sc->sc_mtx);
-		acpiec_unlock(dv);
-		aprint_error_dev(dv,
-		"command takes over %d sec...\n", EC_CMD_TIMEOUT);
-		return AE_ERROR;
+	} else {
+		const unsigned deadline = getticks() + EC_CMD_TIMEOUT*hz;
+		unsigned delta;
+
+		while (sc->sc_state != EC_STATE_FREE &&
+		(delta = deadline - getticks()) < INT_MAX)
+			(void)cv_timedwait(&sc->sc_cv, &sc->sc_mtx, delta);
+		if (sc->sc_state != EC_STATE_FREE) {
+			mutex_exit(&sc->sc_mtx);
+			acpiec_unlock(dv);
+			aprint_error_dev(dv,
+			"command takes over %d sec...\n",
+			EC_CMD_TIMEOUT);
+			return AE_ERROR;
+		}
 	}
 
 done:
@@ -764,17 +768,21 @@ acpiec_write(device_t dv, uint8_t addr, 
 			sc->sc_state);
 			return AE_ERROR;
 		}
-	} else if (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
-		/*
-		 * XXX while (sc->sc_state != EC_STATE_FREE)
-		 *	cv_timedwait(...),
-		 * plus deadline
-		 */
-		mutex_exit(&sc->sc_mtx);
-		acpiec_unlock(dv);
-		aprint_error_dev(dv,
-		"command takes over %d sec...\n", EC_CMD_TIMEOUT);
-		return AE_ERROR;
+	} else {
+		const unsigned deadline = getticks() + EC_CMD_TIMEOUT*hz;
+		unsigned delta;
+
+		while (sc->sc_state != EC_STATE_FREE &&
+		(delta = deadline - getticks()) < INT_MAX)
+			(void)cv_timedwait(&sc->sc_cv, &sc->sc_mtx, delta);
+		if (sc->sc_state != EC_STATE_FREE) {
+			mutex_exit(&sc->sc_mtx);
+			acpiec_unlock(dv);
+			aprint_error_dev(dv,
+			"command takes over %d sec...\n",
+			EC_CMD_TIMEOUT);
+			return AE_ERROR;
+		}
 	}
 
 done:



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:04:40 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Fix interrupt wait loop in acpiec_gpe_query thread.


To generate a diff of this commit:
cvs rdiff -u -r1.93 -r1.94 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.93 src/sys/dev/acpi/acpi_ec.c:1.94
--- src/sys/dev/acpi/acpi_ec.c:1.93	Tue Jul 18 10:04:28 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:04:40 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.93 2023/07/18 10:04:28 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.94 2023/07/18 10:04:40 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.93 2023/07/18 10:04:28 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.94 2023/07/18 10:04:40 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -908,6 +908,9 @@ loop:
 	sc->sc_got_sci = false;
 	sc->sc_state = EC_STATE_QUERY;
 
+	/*
+	 * First, attempt to get the query by polling.
+	 */
 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
 		acpiec_gpe_state_machine(dv);
 		if (sc->sc_state == EC_STATE_FREE)
@@ -915,9 +918,14 @@ loop:
 		delay(1);
 	}
 
+	/*
+	 * Polling timed out.  Try waiting for interrupts -- either GPE
+	 * interrupts, or periodic callouts in case GPE interrupts are
+	 * broken.
+	 */
 	DPRINTF(ACPIEC_DEBUG_QUERY, sc, "SCI polling timeout\n");
-	/* XXX while (sc->sc_state != EC_STATE_FREE) cv_wait(...) */
-	cv_wait(&sc->sc_cv, &sc->sc_mtx);
+	while (sc->sc_state != EC_STATE_FREE)
+		cv_wait(&sc->sc_cv, &sc->sc_mtx);
 
 done:
 	reg = sc->sc_cur_val;



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:04:40 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Fix interrupt wait loop in acpiec_gpe_query thread.


To generate a diff of this commit:
cvs rdiff -u -r1.93 -r1.94 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:04:28 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Fix cv_wait loop around sc->sc_got_sci.

That is, make it actually loop as required, so it gracefully handles
spurious wakeups instead of barging into invalid states.


To generate a diff of this commit:
cvs rdiff -u -r1.92 -r1.93 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.92 src/sys/dev/acpi/acpi_ec.c:1.93
--- src/sys/dev/acpi/acpi_ec.c:1.92	Tue Jul 18 10:04:14 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:04:28 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.92 2023/07/18 10:04:14 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.93 2023/07/18 10:04:28 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.92 2023/07/18 10:04:14 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.93 2023/07/18 10:04:28 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -885,11 +885,9 @@ acpiec_gpe_query(void *arg)
 loop:
 	/*
 	 * Wait until the EC sends an SCI requesting a query.
-	 *
-	 * XXX This needs to be `while', not `if'.
 	 */
 	mutex_enter(&sc->sc_mtx);
-	if (sc->sc_got_sci == false)
+	while (!sc->sc_got_sci)
 		cv_wait(&sc->sc_cv_sci, &sc->sc_mtx);
 	DPRINTF(ACPIEC_DEBUG_QUERY, sc, "SCI query requested\n");
 	mutex_exit(&sc->sc_mtx);
@@ -906,6 +904,7 @@ loop:
 	KASSERT(sc->sc_state == EC_STATE_FREE);
 
 	/* The Query command can always be issued, so be defensive here. */
+	KASSERT(sc->sc_got_sci);
 	sc->sc_got_sci = false;
 	sc->sc_state = EC_STATE_QUERY;
 



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:04:28 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Fix cv_wait loop around sc->sc_got_sci.

That is, make it actually loop as required, so it gracefully handles
spurious wakeups instead of barging into invalid states.


To generate a diff of this commit:
cvs rdiff -u -r1.92 -r1.93 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:04:14 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Set sc_got_sci only when a transaction is over.

Before, when the acpiec thread noticed an SCI had been requested and
entered acpiec_gpe_state_machine to send the query command, it would
see the SCI is still requested -- because it had yet to acknowledge
it by setting the query command! -- and think the EC was asking for a
_second_ SCI.

So once the first SCI transaction was over, it would start a second
one, even though the EC hadn't asked for another -- and this would
wedge on some ECs.

Now, acpiec_gpe_state_machine waits to see what state we transition
to before taking the SCI bit to mean we need to notify the acpiec
thread to handle another query.

That way, when the acpiec thread enters acpiec_gpe_state_machine with
EC_STATE_QUERY, it can send the query command first, with the side
effect of clearing the SCI bit in subsequent reads of the status
register, and it won't think another SCI has been requested until it
returns to EC_STATE_FREE and sees the SCI bit set again in the status
register.

Possibly relevant PRs:

PR kern/53135
PR kern/52763
PR kern/57162


To generate a diff of this commit:
cvs rdiff -u -r1.91 -r1.92 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:04:14 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Set sc_got_sci only when a transaction is over.

Before, when the acpiec thread noticed an SCI had been requested and
entered acpiec_gpe_state_machine to send the query command, it would
see the SCI is still requested -- because it had yet to acknowledge
it by setting the query command! -- and think the EC was asking for a
_second_ SCI.

So once the first SCI transaction was over, it would start a second
one, even though the EC hadn't asked for another -- and this would
wedge on some ECs.

Now, acpiec_gpe_state_machine waits to see what state we transition
to before taking the SCI bit to mean we need to notify the acpiec
thread to handle another query.

That way, when the acpiec thread enters acpiec_gpe_state_machine with
EC_STATE_QUERY, it can send the query command first, with the side
effect of clearing the SCI bit in subsequent reads of the status
register, and it won't think another SCI has been requested until it
returns to EC_STATE_FREE and sees the SCI bit set again in the status
register.

Possibly relevant PRs:

PR kern/53135
PR kern/52763
PR kern/57162


To generate a diff of this commit:
cvs rdiff -u -r1.91 -r1.92 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.91 src/sys/dev/acpi/acpi_ec.c:1.92
--- src/sys/dev/acpi/acpi_ec.c:1.91	Tue Jul 18 10:03:59 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:04:14 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.91 2023/07/18 10:03:59 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.92 2023/07/18 10:04:14 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.91 2023/07/18 10:03:59 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.92 2023/07/18 10:04:14 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -962,9 +962,6 @@ acpiec_gpe_state_machine(device_t dv)
 	}
 #endif
 
-	if (reg & EC_STATUS_SCI)
-		sc->sc_got_sci = true;
-
 	switch (sc->sc_state) {
 	case EC_STATE_QUERY:
 		if ((reg & EC_STATUS_IBF) != 0)
@@ -1026,11 +1023,6 @@ acpiec_gpe_state_machine(device_t dv)
 		break;
 
 	case EC_STATE_FREE:
-		if (sc->sc_got_sci) {
-			DPRINTF(ACPIEC_DEBUG_TRANSITION, sc,
-			"wake SCI thread\n");
-			cv_signal(&sc->sc_cv_sci);
-		}
 		break;
 
 	default:
@@ -1038,6 +1030,19 @@ acpiec_gpe_state_machine(device_t dv)
 	}
 
 	/*
+	 * If we just ended a transaction, and an SCI was requested,
+	 * notify the SCI thread.
+	 */
+	if (sc->sc_state == EC_STATE_FREE) {
+		if (reg & EC_STATUS_SCI) {
+			DPRINTF(ACPIEC_DEBUG_TRANSITION, sc,
+			"wake SCI thread\n");
+			sc->sc_got_sci = true;
+			cv_signal(&sc->sc_cv_sci);
+		}
+	}
+
+	/*
 	 * In case GPE interrupts are broken, poll once per tick for EC
 	 * status updates while a transaction is still pending.
 	 */



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:03:59 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Assert state is free when we start a transaction.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.90 -r1.91 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:03:59 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Assert state is free when we start a transaction.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.90 -r1.91 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.90 src/sys/dev/acpi/acpi_ec.c:1.91
--- src/sys/dev/acpi/acpi_ec.c:1.90	Tue Jul 18 10:03:46 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:03:59 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.90 2023/07/18 10:03:46 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.91 2023/07/18 10:03:59 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.90 2023/07/18 10:03:46 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.91 2023/07/18 10:03:59 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -671,6 +671,8 @@ acpiec_read(device_t dv, uint8_t addr, u
 	curlwp->l_name ? curlwp->l_name : "",
 	addr);
 
+	KASSERT(sc->sc_state == EC_STATE_FREE);
+
 	sc->sc_cur_addr = addr;
 	sc->sc_state = EC_STATE_READ;
 
@@ -737,6 +739,8 @@ acpiec_write(device_t dv, uint8_t addr, 
 	curlwp->l_name ? curlwp->l_name : "",
 	addr, val);
 
+	KASSERT(sc->sc_state == EC_STATE_FREE);
+
 	sc->sc_cur_addr = addr;
 	sc->sc_cur_val = val;
 	sc->sc_state = EC_STATE_WRITE;
@@ -899,6 +903,8 @@ loop:
 
 	DPRINTF(ACPIEC_DEBUG_QUERY, sc, "SCI query\n");
 
+	KASSERT(sc->sc_state == EC_STATE_FREE);
+
 	/* The Query command can always be issued, so be defensive here. */
 	sc->sc_got_sci = false;
 	sc->sc_state = EC_STATE_QUERY;



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:03:47 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Sprinkle comments.

Note where this code is abusing cv_wait and needs a loop to handle
spurious wakeups.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.89 -r1.90 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.89 src/sys/dev/acpi/acpi_ec.c:1.90
--- src/sys/dev/acpi/acpi_ec.c:1.89	Tue Jul 18 10:03:35 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:03:46 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.89 2023/07/18 10:03:35 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.90 2023/07/18 10:03:46 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.89 2023/07/18 10:03:35 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.90 2023/07/18 10:03:46 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -694,6 +694,11 @@ acpiec_read(device_t dv, uint8_t addr, u
 			return AE_ERROR;
 		}
 	} else if (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
+		/*
+		 * XXX while (sc->sc_state != EC_STATE_FREE)
+		 *	cv_timedwait(...),
+		 * plus deadline
+		 */
 		mutex_exit(&sc->sc_mtx);
 		acpiec_unlock(dv);
 		aprint_error_dev(dv,
@@ -756,6 +761,11 @@ acpiec_write(device_t dv, uint8_t addr, 
 			return AE_ERROR;
 		}
 	} else if (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
+		/*
+		 * XXX while (sc->sc_state != EC_STATE_FREE)
+		 *	cv_timedwait(...),
+		 * plus deadline
+		 */
 		mutex_exit(&sc->sc_mtx);
 		acpiec_unlock(dv);
 		aprint_error_dev(dv,
@@ -869,13 +879,21 @@ acpiec_gpe_query(void *arg)
 	int i;
 
 loop:
+	/*
+	 * Wait until the EC sends an SCI requesting a query.
+	 *
+	 * XXX This needs to be `while', not `if'.
+	 */
 	mutex_enter(&sc->sc_mtx);
-
 	if (sc->sc_got_sci == false)
 		cv_wait(&sc->sc_cv_sci, &sc->sc_mtx);
 	DPRINTF(ACPIEC_DEBUG_QUERY, sc, "SCI query requested\n");
 	mutex_exit(&sc->sc_mtx);
 
+	/*
+	 * EC wants to submit a query to us.  Exclude concurrent reads
+	 * and writes while we handle it.
+	 */
 	acpiec_lock(dv);
 	mutex_enter(&sc->sc_mtx);
 
@@ -893,6 +911,7 @@ loop:
 	}
 
 	DPRINTF(ACPIEC_DEBUG_QUERY, sc, "SCI polling timeout\n");
+	/* XXX while (sc->sc_state != EC_STATE_FREE) cv_wait(...) */
 	cv_wait(&sc->sc_cv, &sc->sc_mtx);
 
 done:
@@ -951,17 +970,14 @@ acpiec_gpe_state_machine(device_t dv)
 	case EC_STATE_QUERY_VAL:
 		if ((reg & EC_STATUS_OBF) == 0)
 			break; /* Nothing of interest here. */
-
 		sc->sc_cur_val = acpiec_read_data(sc);
 		sc->sc_state = EC_STATE_FREE;
-
 		cv_signal(&sc->sc_cv);
 		break;
 
 	case EC_STATE_READ:
 		if ((reg & EC_STATUS_IBF) != 0)
 			break; /* Nothing of interest here. */
-
 		acpiec_write_command(sc, EC_COMMAND_READ);
 		sc->sc_state = EC_STATE_READ_ADDR;
 		break;
@@ -969,7 +985,6 @@ acpiec_gpe_state_machine(device_t dv)
 	case EC_STATE_READ_ADDR:
 		if ((reg & EC_STATUS_IBF) != 0)
 			break; /* Nothing of interest here. */
-
 		acpiec_write_data(sc, sc->sc_cur_addr);
 		sc->sc_state = EC_STATE_READ_VAL;
 		break;
@@ -979,14 +994,12 @@ acpiec_gpe_state_machine(device_t dv)
 			break; /* Nothing of interest here. */
 		sc->sc_cur_val = acpiec_read_data(sc);
 		sc->sc_state = EC_STATE_FREE;
-
 		cv_signal(&sc->sc_cv);
 		break;
 
 	case EC_STATE_WRITE:
 		if ((reg & EC_STATUS_IBF) != 0)
 			break; /* Nothing of interest here. */
-
 		acpiec_write_command(sc, EC_COMMAND_WRITE);
 		sc->sc_state = EC_STATE_WRITE_ADDR;
 		break;
@@ -1003,7 +1016,6 @@ acpiec_gpe_state_machine(device_t dv)
 			break; /* Nothing of interest here. */
 		sc->sc_state = EC_STATE_FREE;
 		cv_signal(&sc->sc_cv);
-
 		acpiec_write_data(sc, sc->sc_cur_val);
 		break;
 
@@ -1014,10 +1026,15 @@ acpiec_gpe_state_machine(device_t dv)
 			cv_signal(&sc->sc_cv_sci);
 		}
 		break;
+
 	default:
 		panic("invalid state");
 	}
 
+	/*
+	 * In case GPE interrupts are broken, poll once per tick for EC
+	 * status updates while a transaction is still pending.
+	 */
 	if (sc->sc_state != EC_STATE_FREE) {
 		DPRINTF(ACPIEC_DEBUG_INTR, sc, "schedule callout\n");
 		callout_schedule(&sc->sc_pseudo_intr, 1);



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:03:47 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Sprinkle comments.

Note where this code is abusing cv_wait and needs a loop to handle
spurious wakeups.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.89 -r1.90 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:03:35 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Clarify lock order and sprinkle lock assertions.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.88 -r1.89 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.88 src/sys/dev/acpi/acpi_ec.c:1.89
--- src/sys/dev/acpi/acpi_ec.c:1.88	Tue Jul 18 10:02:25 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:03:35 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.88 2023/07/18 10:02:25 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.89 2023/07/18 10:03:35 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -34,14 +34,12 @@
  * - read and write access from ASL, e.g. to read battery state
  * - notification of ASL of System Control Interrupts.
  *
- * Access to the EC is serialised by sc_access_mtx and optionally the
- * ACPI global mutex.  Both locks are held until the request is fulfilled.
- * All access to the softc has to hold sc_mtx to serialise against the GPE
- * handler and the callout.  sc_mtx is also used for wakeup conditions.
+ * Lock order:
+ *	sc_access_mtx (serializes EC transactions -- read, write, or SCI)
+ *	-> ACPI global lock (excludes other ACPI access during EC transaction)
+ *	-> sc_mtx (serializes state machine transitions and waits)
  *
- * SCIs are processed in a kernel thread. Handling gets a bit complicated
- * by the lock order (sc_mtx must be acquired after sc_access_mtx and the
- * ACPI global mutex).
+ * SCIs are processed in a kernel thread.
  *
  * Read and write requests spin around for a short time as many requests
  * can be handled instantly by the EC.  During normal processing interrupt
@@ -59,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.88 2023/07/18 10:02:25 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.89 2023/07/18 10:03:35 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -566,6 +564,8 @@ acpiec_read_data(struct acpiec_softc *sc
 {
 	uint8_t x;
 
+	KASSERT(mutex_owned(&sc->sc_mtx));
+
 	x = bus_space_read_1(sc->sc_data_st, sc->sc_data_sh, 0);
 	DPRINTF(ACPIEC_DEBUG_REG, sc, "read data=0x%"PRIx8"\n", x);
 
@@ -576,6 +576,8 @@ static void
 acpiec_write_data(struct acpiec_softc *sc, uint8_t val)
 {
 
+	KASSERT(mutex_owned(&sc->sc_mtx));
+
 	DPRINTF(ACPIEC_DEBUG_REG, sc, "write data=0x%"PRIx8"\n", val);
 	bus_space_write_1(sc->sc_data_st, sc->sc_data_sh, 0, val);
 }
@@ -585,6 +587,8 @@ acpiec_read_status(struct acpiec_softc *
 {
 	uint8_t x;
 
+	KASSERT(mutex_owned(&sc->sc_mtx));
+
 	x = bus_space_read_1(sc->sc_csr_st, sc->sc_csr_sh, 0);
 	DPRINTF(ACPIEC_DEBUG_REG, sc, "read status=0x%"PRIx8"\n", x);
 
@@ -595,6 +599,8 @@ static void
 acpiec_write_command(struct acpiec_softc *sc, uint8_t cmd)
 {
 
+	KASSERT(mutex_owned(&sc->sc_mtx));
+
 	DPRINTF(ACPIEC_DEBUG_REG, sc, "write command=0x%"PRIx8"\n", cmd);
 	bus_space_write_1(sc->sc_csr_st, sc->sc_csr_sh, 0, cmd);
 }
@@ -918,6 +924,8 @@ acpiec_gpe_state_machine(device_t dv)
 	struct acpiec_softc *sc = device_private(dv);
 	uint8_t reg;
 
+	KASSERT(mutex_owned(&sc->sc_mtx));
+
 	reg = acpiec_read_status(sc);
 
 #ifdef ACPIEC_DEBUG



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:03:35 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Clarify lock order and sprinkle lock assertions.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.88 -r1.89 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:02:25 UTC 2023

Modified Files:
src/sys/arch/amd64/conf: ALL
src/sys/arch/i386/conf: ALL
src/sys/dev/acpi: acpi_ec.c files.acpi

Log Message:
acpiec(4): New ACPIEC_DEBUG option.

Value is bit mask of debug messages to enable.

Enable in x86/ALL kernels.

No functional change intended when the option is off.


To generate a diff of this commit:
cvs rdiff -u -r1.178 -r1.179 src/sys/arch/amd64/conf/ALL
cvs rdiff -u -r1.510 -r1.511 src/sys/arch/i386/conf/ALL
cvs rdiff -u -r1.87 -r1.88 src/sys/dev/acpi/acpi_ec.c
cvs rdiff -u -r1.127 -r1.128 src/sys/dev/acpi/files.acpi

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/conf/ALL
diff -u src/sys/arch/amd64/conf/ALL:1.178 src/sys/arch/amd64/conf/ALL:1.179
--- src/sys/arch/amd64/conf/ALL:1.178	Fri Jul  7 12:46:50 2023
+++ src/sys/arch/amd64/conf/ALL	Tue Jul 18 10:02:25 2023
@@ -1,4 +1,4 @@
-# $NetBSD: ALL,v 1.178 2023/07/07 12:46:50 riastradh Exp $
+# $NetBSD: ALL,v 1.179 2023/07/18 10:02:25 riastradh Exp $
 # From NetBSD: GENERIC,v 1.787 2006/10/01 18:37:54 bouyer Exp
 #
 # ALL machine description file
@@ -17,7 +17,7 @@ include 	"arch/amd64/conf/std.amd64"
 
 options 	INCLUDE_CONFIG_FILE	# embed config file in kernel binary
 
-#ident		"ALL-$Revision: 1.178 $"
+#ident		"ALL-$Revision: 1.179 $"
 
 maxusers	64		# estimated number of users
 
@@ -370,6 +370,7 @@ acpibut*	at acpi?		# ACPI Button
 acpidalb*	at acpi?		# ACPI Direct Application Launch Button
 acpiec* 	at acpi?		# ACPI Embedded Controller (late)
 acpiecdt*	at acpi?		# ACPI Embedded Controller (early)
+options 	ACPIEC_DEBUG=-1
 acpifan*	at acpi?		# ACPI Fan
 acpilid*	at acpi?		# ACPI Lid Switch
 acpipmtr*	at acpi?		# ACPI Power Meter (experimental)

Index: src/sys/arch/i386/conf/ALL
diff -u src/sys/arch/i386/conf/ALL:1.510 src/sys/arch/i386/conf/ALL:1.511
--- src/sys/arch/i386/conf/ALL:1.510	Sun Jul 16 10:20:07 2023
+++ src/sys/arch/i386/conf/ALL	Tue Jul 18 10:02:25 2023
@@ -1,4 +1,4 @@
-# $NetBSD: ALL,v 1.510 2023/07/16 10:20:07 riastradh Exp $
+# $NetBSD: ALL,v 1.511 2023/07/18 10:02:25 riastradh Exp $
 # From NetBSD: GENERIC,v 1.787 2006/10/01 18:37:54 bouyer Exp
 #
 # ALL machine description file
@@ -17,7 +17,7 @@ include 	"arch/i386/conf/std.i386"
 
 options 	INCLUDE_CONFIG_FILE	# embed config file in kernel binary
 
-#ident		"ALL-$Revision: 1.510 $"
+#ident		"ALL-$Revision: 1.511 $"
 
 maxusers	64		# estimated number of users
 
@@ -357,6 +357,7 @@ acpibut*	at acpi?		# ACPI Button
 acpidalb*	at acpi?		# ACPI Direct Application Launch Button
 acpiec* 	at acpi?		# ACPI Embedded Controller (late)
 acpiecdt*	at acpi?		# ACPI Embedded Controller (early)
+options 	ACPIEC_DEBUG=-1
 acpifan*	at acpi?		# ACPI Fan
 acpilid*	at acpi?		# ACPI Lid Switch
 acpipmtr*	at acpi?		# ACPI Power Meter (experimental)

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.87 src/sys/dev/acpi/acpi_ec.c:1.88
--- src/sys/dev/acpi/acpi_ec.c:1.87	Tue Jul 18 10:02:09 2023
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:02:25 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.87 2023/07/18 10:02:09 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.88 2023/07/18 10:02:25 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -59,7 +59,11 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.87 2023/07/18 10:02:09 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.88 2023/07/18 10:02:25 riastradh Exp $");
+
+#ifdef _KERNEL_OPT
+#include "opt_acpi_ec.h"
+#endif
 
 #include 
 #include 
@@ -101,22 +105,38 @@ ACPI_MODULE_NAME("acpi_ec")
 #define	EC_STATUS_SCI		0x20
 #define	EC_STATUS_SMI		0x40
 
+#define	EC_STATUS_FMT			  \
+	"\x10\10IGN7\7SMI\6SCI\5BURST\4CMD\3IGN2\2IBF\1OBF"
+
 static const struct device_compatible_entry compat_data[] = {
 	{ .compat = "PNP0C09" },
 	DEVICE_COMPAT_EOL
 };
 
+#define	EC_STATE_ENUM(F)		  \
+	F(EC_STATE_QUERY, "QUERY")	  \
+	F(EC_STATE_QUERY_VAL, "QUERY_VAL")  \
+	F(EC_STATE_READ, "READ")	  \
+	F(EC_STATE_READ_ADDR, "READ_ADDR")  \
+	F(EC_STATE_READ_VAL, "READ_VAL")  \
+	F(EC_STATE_WRITE, "WRITE")	  \
+	F(EC_STATE_WRITE_ADDR, "WRITE_ADDR")  \
+	F(EC_STATE_WRITE_VAL, "WRITE_VAL")  \
+	F(EC_STATE_FREE, "FREE")	  \
+
 enum ec_state_t {
-	EC_STATE_QUERY,
-	EC_STATE_QUERY_VAL,
-	EC_STATE_READ,
-	EC_STATE_READ_ADDR,
-	EC_STATE_READ_VAL,
-	EC_STATE_WRITE,
-	EC_STATE_WRITE_ADDR,
-	EC_STATE_WRITE_VAL,
-	EC_STATE_FREE
+#define	F(N, S)	N,
+	EC_STATE_ENUM(F)
+#undef F
+};
+
+#ifdef ACPIEC_DEBUG
+static const char *const acpiec_state_names[] = {
+#define F(N, S)	[N] = S,
+	EC_STATE_ENUM(F)
+#undef F
 };
+#endif
 
 struct acpiec_softc {
 	device_t sc_dev;
@@ -144,6 +164,55 @@ struct acpiec_softc {
 	uint8_t sc_cur_addr, sc_cur_val;
 };
 
+#ifdef ACPIEC_DEBUG
+
+#define	ACPIEC_

CVS commit: src/sys

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:02:25 UTC 2023

Modified Files:
src/sys/arch/amd64/conf: ALL
src/sys/arch/i386/conf: ALL
src/sys/dev/acpi: acpi_ec.c files.acpi

Log Message:
acpiec(4): New ACPIEC_DEBUG option.

Value is bit mask of debug messages to enable.

Enable in x86/ALL kernels.

No functional change intended when the option is off.


To generate a diff of this commit:
cvs rdiff -u -r1.178 -r1.179 src/sys/arch/amd64/conf/ALL
cvs rdiff -u -r1.510 -r1.511 src/sys/arch/i386/conf/ALL
cvs rdiff -u -r1.87 -r1.88 src/sys/dev/acpi/acpi_ec.c
cvs rdiff -u -r1.127 -r1.128 src/sys/dev/acpi/files.acpi

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:02:09 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Record device_t self.

Not used yet, to be used soon for device_printf and to allow making
some of the internal functions a little more type-safe later.


To generate a diff of this commit:
cvs rdiff -u -r1.86 -r1.87 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_ec.c
diff -u src/sys/dev/acpi/acpi_ec.c:1.86 src/sys/dev/acpi/acpi_ec.c:1.87
--- src/sys/dev/acpi/acpi_ec.c:1.86	Fri Dec 31 17:22:25 2021
+++ src/sys/dev/acpi/acpi_ec.c	Tue Jul 18 10:02:09 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_ec.c,v 1.86 2021/12/31 17:22:25 riastradh Exp $	*/
+/*	$NetBSD: acpi_ec.c,v 1.87 2023/07/18 10:02:09 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger .
@@ -59,7 +59,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.86 2021/12/31 17:22:25 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.87 2023/07/18 10:02:09 riastradh Exp $");
 
 #include 
 #include 
@@ -119,6 +119,8 @@ enum ec_state_t {
 };
 
 struct acpiec_softc {
+	device_t sc_dev;
+
 	ACPI_HANDLE sc_ech;
 
 	ACPI_HANDLE sc_gpeh;
@@ -313,6 +315,8 @@ acpiec_common_attach(device_t parent, de
 	ACPI_STATUS rv;
 	ACPI_INTEGER val;
 
+	sc->sc_dev = self;
+
 	sc->sc_csr_st = cmdt;
 	sc->sc_data_st = datat;
 



CVS commit: src/sys/dev/acpi

2023-07-18 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Jul 18 10:02:09 UTC 2023

Modified Files:
src/sys/dev/acpi: acpi_ec.c

Log Message:
acpiec(4): Record device_t self.

Not used yet, to be used soon for device_printf and to allow making
some of the internal functions a little more type-safe later.


To generate a diff of this commit:
cvs rdiff -u -r1.86 -r1.87 src/sys/dev/acpi/acpi_ec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.