xref: /titanic_50/usr/src/uts/sparc/v9/sys/mutex_impl.h (revision 575a742678105d588b7c8e1653b57a7e3d78440b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*575a7426Spt157919  * Common Development and Distribution License (the "License").
6*575a7426Spt157919  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*575a7426Spt157919  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #ifndef _SYS_MUTEX_IMPL_H
277c478bd9Sstevel@tonic-gate #define	_SYS_MUTEX_IMPL_H
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #ifndef	_ASM
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/machlock.h>
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
377c478bd9Sstevel@tonic-gate extern "C" {
387c478bd9Sstevel@tonic-gate #endif
397c478bd9Sstevel@tonic-gate 
40*575a7426Spt157919 #define	MUTEX_THREAD	(-0x8)
417c478bd9Sstevel@tonic-gate #ifndef	_ASM
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * mutex_enter() assumes that the mutex is adaptive and tries to grab the
457c478bd9Sstevel@tonic-gate  * lock by doing a cas on the first word of the mutex.  If the cas fails,
467c478bd9Sstevel@tonic-gate  * it means that either (1) the lock is a spin lock, or (2) the lock is
477c478bd9Sstevel@tonic-gate  * adaptive but already held.  mutex_vector_enter() distinguishes these
487c478bd9Sstevel@tonic-gate  * cases by looking at the mutex type, which is encoded in the low-order
497c478bd9Sstevel@tonic-gate  * bits of the owner field.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate typedef union mutex_impl {
527c478bd9Sstevel@tonic-gate 	/*
537c478bd9Sstevel@tonic-gate 	 * Adaptive mutex.
547c478bd9Sstevel@tonic-gate 	 */
557c478bd9Sstevel@tonic-gate 	struct adaptive_mutex {
567c478bd9Sstevel@tonic-gate 		uintptr_t _m_owner;	/* 0-3/0-7 owner and waiters bit */
577c478bd9Sstevel@tonic-gate 	} m_adaptive;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	/*
607c478bd9Sstevel@tonic-gate 	 * Spin Mutex.
617c478bd9Sstevel@tonic-gate 	 */
627c478bd9Sstevel@tonic-gate 	struct spin_mutex {
637c478bd9Sstevel@tonic-gate 		ushort_t m_oldspl;	/* 0-1	old %pil value */
647c478bd9Sstevel@tonic-gate 		ushort_t m_minspl;	/* 2-3	min %pil val if lock held */
657c478bd9Sstevel@tonic-gate 		ushort_t m_filler;	/* 4-5	unused */
667c478bd9Sstevel@tonic-gate 		lock_t	m_spinlock;	/* 6	real lock */
677c478bd9Sstevel@tonic-gate 		lock_t	m_dummylock;	/* 7	dummy lock (always set) */
687c478bd9Sstevel@tonic-gate 	} m_spin;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate } mutex_impl_t;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	m_owner	m_adaptive._m_owner
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #define	MUTEX_WAITERS		0x1
757c478bd9Sstevel@tonic-gate #define	MUTEX_DEAD		0x6
767c478bd9Sstevel@tonic-gate #define	MUTEX_THREAD		(-0x8)
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	MUTEX_OWNER(lp)		((kthread_id_t)((lp)->m_owner & MUTEX_THREAD))
797c478bd9Sstevel@tonic-gate #define	MUTEX_NO_OWNER		((kthread_id_t)NULL)
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #define	MUTEX_SET_WAITERS(lp)						\
827c478bd9Sstevel@tonic-gate {									\
837c478bd9Sstevel@tonic-gate 	uintptr_t old;							\
847c478bd9Sstevel@tonic-gate 	while ((old = (lp)->m_owner) != 0 &&				\
857c478bd9Sstevel@tonic-gate 	    casip(&(lp)->m_owner, old, old | MUTEX_WAITERS) != old)	\
867c478bd9Sstevel@tonic-gate 		continue;						\
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #define	MUTEX_HAS_WAITERS(lp)			((lp)->m_owner & MUTEX_WAITERS)
907c478bd9Sstevel@tonic-gate #define	MUTEX_CLEAR_LOCK_AND_WAITERS(lp)	(lp)->m_owner = 0
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #define	MUTEX_SET_TYPE(lp, type)
937c478bd9Sstevel@tonic-gate #define	MUTEX_TYPE_ADAPTIVE(lp)	(((lp)->m_owner & MUTEX_DEAD) == 0)
947c478bd9Sstevel@tonic-gate #define	MUTEX_TYPE_SPIN(lp)	((lp)->m_spin.m_dummylock == LOCK_HELD_VALUE)
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define	MUTEX_DESTROY(lp)	\
977c478bd9Sstevel@tonic-gate 	(lp)->m_owner = ((uintptr_t)curthread | MUTEX_DEAD)
987c478bd9Sstevel@tonic-gate 
99*575a7426Spt157919 #define	MUTEX_BACKOFF_BASE	1
100*575a7426Spt157919 #define	MUTEX_BACKOFF_SHIFT	1
101*575a7426Spt157919 #define	MUTEX_CAP_FACTOR	8
102*575a7426Spt157919 #define	MUTEX_DELAY()	{ \
103*575a7426Spt157919 				mutex_delay(); \
104*575a7426Spt157919 			}
105*575a7426Spt157919 
106*575a7426Spt157919 /* low-overhead clock read */
107*575a7426Spt157919 extern u_longlong_t gettick(void);
108*575a7426Spt157919 #define	MUTEX_GETTICK()	gettick()
109*575a7426Spt157919 extern void null_xcall(void);
110*575a7426Spt157919 #define	MUTEX_SYNC()	xc_all((xcfunc_t *)null_xcall, 0, 0)
111*575a7426Spt157919 
112*575a7426Spt157919 extern void cas_delay(void *);
113*575a7426Spt157919 extern void rdccr_delay(void);
1147c478bd9Sstevel@tonic-gate extern int mutex_adaptive_tryenter(mutex_impl_t *);
115*575a7426Spt157919 extern void *mutex_owner_running(mutex_impl_t *);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #endif	/* _ASM */
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate #endif
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #endif	/* _SYS_MUTEX_IMPL_H */
124