xref: /netbsd-src/sys/external/bsd/drm2/linux/linux_notifier.c (revision ce75f96de2191447fcce622e03a39bc963b40069)
1*ce75f96dSriastradh /*	$NetBSD: linux_notifier.c,v 1.2 2021/12/19 11:47:08 riastradh Exp $	*/
22e25b933Sriastradh 
32e25b933Sriastradh /*-
42e25b933Sriastradh  * Copyright (c) 2021 The NetBSD Foundation, Inc.
52e25b933Sriastradh  * All rights reserved.
62e25b933Sriastradh  *
72e25b933Sriastradh  * This code is derived from software contributed to The NetBSD Foundation
82e25b933Sriastradh  * by Taylor R. Campbell.
92e25b933Sriastradh  *
102e25b933Sriastradh  * Redistribution and use in source and binary forms, with or without
112e25b933Sriastradh  * modification, are permitted provided that the following conditions
122e25b933Sriastradh  * are met:
132e25b933Sriastradh  * 1. Redistributions of source code must retain the above copyright
142e25b933Sriastradh  *    notice, this list of conditions and the following disclaimer.
152e25b933Sriastradh  * 2. Redistributions in binary form must reproduce the above copyright
162e25b933Sriastradh  *    notice, this list of conditions and the following disclaimer in the
172e25b933Sriastradh  *    documentation and/or other materials provided with the distribution.
182e25b933Sriastradh  *
192e25b933Sriastradh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
202e25b933Sriastradh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212e25b933Sriastradh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222e25b933Sriastradh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232e25b933Sriastradh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242e25b933Sriastradh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252e25b933Sriastradh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262e25b933Sriastradh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272e25b933Sriastradh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282e25b933Sriastradh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292e25b933Sriastradh  * POSSIBILITY OF SUCH DAMAGE.
302e25b933Sriastradh  */
312e25b933Sriastradh 
322e25b933Sriastradh #include <sys/cdefs.h>
33*ce75f96dSriastradh __KERNEL_RCSID(0, "$NetBSD: linux_notifier.c,v 1.2 2021/12/19 11:47:08 riastradh Exp $");
342e25b933Sriastradh 
352e25b933Sriastradh #include <sys/types.h>
362e25b933Sriastradh 
372e25b933Sriastradh #include <sys/pserialize.h>
382e25b933Sriastradh #include <sys/xcall.h>
392e25b933Sriastradh 
402e25b933Sriastradh #include <linux/notifier.h>
412e25b933Sriastradh #include <linux/spinlock.h>
422e25b933Sriastradh 
432e25b933Sriastradh void
ATOMIC_INIT_NOTIFIER_HEAD(struct atomic_notifier_head * H)442e25b933Sriastradh ATOMIC_INIT_NOTIFIER_HEAD(struct atomic_notifier_head *H)
452e25b933Sriastradh {
462e25b933Sriastradh 
472e25b933Sriastradh 	spin_lock_init(&H->anh_lock);
482e25b933Sriastradh 	PSLIST_INIT(&H->anh_list);
492e25b933Sriastradh }
502e25b933Sriastradh 
512e25b933Sriastradh void
ATOMIC_CLEANUP_NOTIFIER_HEAD(struct atomic_notifier_head * H)522e25b933Sriastradh ATOMIC_CLEANUP_NOTIFIER_HEAD(struct atomic_notifier_head *H)
532e25b933Sriastradh {
542e25b933Sriastradh 
552e25b933Sriastradh 	PSLIST_DESTROY(&H->anh_list);
562e25b933Sriastradh 	spin_lock_destroy(&H->anh_lock);
572e25b933Sriastradh }
582e25b933Sriastradh 
592e25b933Sriastradh void
atomic_notifier_chain_register(struct atomic_notifier_head * H,struct notifier_block * B)602e25b933Sriastradh atomic_notifier_chain_register(struct atomic_notifier_head *H,
612e25b933Sriastradh     struct notifier_block *B)
622e25b933Sriastradh {
632e25b933Sriastradh 
642e25b933Sriastradh 	spin_lock(&H->anh_lock);
652e25b933Sriastradh 	PSLIST_WRITER_INSERT_HEAD(&H->anh_list, B, nb_entry);
662e25b933Sriastradh 	spin_unlock(&H->anh_lock);
672e25b933Sriastradh }
682e25b933Sriastradh 
692e25b933Sriastradh void
atomic_notifier_chain_unregister(struct atomic_notifier_head * H,struct notifier_block * B)702e25b933Sriastradh atomic_notifier_chain_unregister(struct atomic_notifier_head *H,
712e25b933Sriastradh     struct notifier_block *B)
722e25b933Sriastradh {
732e25b933Sriastradh 
742e25b933Sriastradh 	spin_lock(&H->anh_lock);
752e25b933Sriastradh 	PSLIST_WRITER_REMOVE(B, nb_entry);
762e25b933Sriastradh 	spin_unlock(&H->anh_lock);
772e25b933Sriastradh 
782e25b933Sriastradh 	xc_barrier(0);
792e25b933Sriastradh }
802e25b933Sriastradh 
812e25b933Sriastradh void
atomic_notifier_call_chain(struct atomic_notifier_head * H,unsigned long arg0,void * arg1)82*ce75f96dSriastradh atomic_notifier_call_chain(struct atomic_notifier_head *H,
832e25b933Sriastradh     unsigned long arg0, void *arg1)
842e25b933Sriastradh {
852e25b933Sriastradh 	struct notifier_block *B;
862e25b933Sriastradh 	int s;
872e25b933Sriastradh 
882e25b933Sriastradh 	s = pserialize_read_enter();
892e25b933Sriastradh 	PSLIST_READER_FOREACH(B, &H->anh_list, struct notifier_block, nb_entry)
902e25b933Sriastradh 	{
912e25b933Sriastradh 		if ((*B->notifier_call)(B, arg0, arg1) == NOTIFY_DONE)
922e25b933Sriastradh 			break;
932e25b933Sriastradh 	}
942e25b933Sriastradh 	pserialize_read_exit(s);
952e25b933Sriastradh }
96