mutex.h revision 1.24
11.24Sskrll/*	$NetBSD: mutex.h,v 1.24 2020/12/01 14:52:36 skrll Exp $	*/
21.1Smatt
31.1Smatt/*-
41.1Smatt * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc.
51.1Smatt * All rights reserved.
61.1Smatt *
71.1Smatt * This code is derived from software contributed to The NetBSD Foundation
81.1Smatt * by Jason R. Thorpe and Andrew Doran.
91.1Smatt *
101.1Smatt * Redistribution and use in source and binary forms, with or without
111.1Smatt * modification, are permitted provided that the following conditions
121.1Smatt * are met:
131.1Smatt * 1. Redistributions of source code must retain the above copyright
141.1Smatt *    notice, this list of conditions and the following disclaimer.
151.1Smatt * 2. Redistributions in binary form must reproduce the above copyright
161.1Smatt *    notice, this list of conditions and the following disclaimer in the
171.1Smatt *    documentation and/or other materials provided with the distribution.
181.1Smatt *
191.1Smatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Smatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Smatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Smatt * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Smatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Smatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Smatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Smatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Smatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Smatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Smatt * POSSIBILITY OF SUCH DAMAGE.
301.1Smatt */
311.1Smatt
321.1Smatt#ifndef _ARM_MUTEX_H_
331.1Smatt#define	_ARM_MUTEX_H_
341.1Smatt
351.1Smatt/*
361.5Sthorpej * The ARM mutex implementation is troublesome, because pre-v6 ARM lacks a
371.7Sthorpej * compare-and-swap operation.  However, there aren't any MP pre-v6 ARM
381.17Sskrll * systems to speak of.
391.4Sthorpej *
401.17Sskrll * ARMv6 and later, however, does have ldrex/strex, and can thus implement an
411.17Sskrll * MP-safe compare-and-swap.
421.4Sthorpej *
431.17Sskrll * So, what we have done is implement simple mutexes using a compare-and-swap.
441.9Sad * We support pre-ARMv6 by implementing CAS as a restartable atomic sequence
451.17Sskrll * that is checked by the IRQ vector.
461.24Sskrll *
471.1Smatt */
481.1Smatt
491.1Smattstruct kmutex {
501.7Sthorpej	union {
511.7Sthorpej		/* Adaptive mutex */
521.7Sthorpej		volatile uintptr_t	mtxa_owner;	/* 0-3 */
531.7Sthorpej
541.23Sriastrad#ifdef _KERNEL
551.7Sthorpej		/* Spin mutex */
561.7Sthorpej		struct {
571.13Smatt			/*
581.18Sskrll			 * Since the low bit of mtxa_owner is used to flag this
591.13Smatt			 * mutex as a spin mutex, we can't use the first byte
601.13Smatt			 * or the last byte to store the ipl or lock values.
611.13Smatt			 */
621.7Sthorpej			volatile uint8_t	mtxs_dummy;
631.7Sthorpej			ipl_cookie_t		mtxs_ipl;
641.13Smatt			__cpu_simple_lock_t	mtxs_lock;
651.12Smatt			volatile uint8_t	mtxs_unused;
661.7Sthorpej		} s;
671.23Sriastrad#endif
681.7Sthorpej	} u;
691.1Smatt};
701.1Smatt
711.22Sriastrad#ifdef __MUTEX_PRIVATE
721.22Sriastrad
731.7Sthorpej#define	mtx_owner		u.mtxa_owner
741.7Sthorpej#define	mtx_ipl			u.s.mtxs_ipl
751.7Sthorpej#define	mtx_lock		u.s.mtxs_lock
761.7Sthorpej
771.1Smatt#if 0
781.7Sthorpej#define	__HAVE_MUTEX_STUBS		1
791.7Sthorpej#define	__HAVE_SPIN_MUTEX_STUBS		1
801.1Smatt#endif
811.7Sthorpej#define	__HAVE_SIMPLE_MUTEXES		1
821.7Sthorpej
831.9Sad#define	MUTEX_CAS(p, o, n)		\
841.11Smatt    (atomic_cas_ulong((volatile unsigned long *)(p), (o), (n)) == (o))
851.11Smatt#ifdef MULTIPROCESSOR
861.11Smatt#define	MUTEX_SMT_PAUSE()		__asm __volatile("wfe")
871.11Smatt#define	MUTEX_SMT_WAKE()		__asm __volatile("sev")
881.11Smatt#endif
891.1Smatt
901.1Smatt#endif	/* __MUTEX_PRIVATE */
911.1Smatt
921.1Smatt#endif /* _ARM_MUTEX_H_ */
93