14ec735bfSmiod #ifndef _M88K_LOCK_H_
24ec735bfSmiod #define _M88K_LOCK_H_
3*c7ee6196Sdlg /* $OpenBSD: lock.h,v 1.11 2015/02/11 00:14:11 dlg Exp $ */
44ec735bfSmiod
54ec735bfSmiod /*
64ec735bfSmiod * Copyright (c) 2005, Miodrag Vallat.
74ec735bfSmiod *
84ec735bfSmiod * Redistribution and use in source and binary forms, with or without
94ec735bfSmiod * modification, are permitted provided that the following conditions
104ec735bfSmiod * are met:
114ec735bfSmiod * 1. Redistributions of source code must retain the above copyright
124ec735bfSmiod * notice, this list of conditions and the following disclaimer.
134ec735bfSmiod * 2. Redistributions in binary form must reproduce the above copyright
144ec735bfSmiod * notice, this list of conditions and the following disclaimer in the
154ec735bfSmiod * documentation and/or other materials provided with the distribution.
164ec735bfSmiod *
174ec735bfSmiod * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
184ec735bfSmiod * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
194ec735bfSmiod * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
204ec735bfSmiod * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
214ec735bfSmiod * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
224ec735bfSmiod * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
234ec735bfSmiod * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
244ec735bfSmiod * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
254ec735bfSmiod * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
264ec735bfSmiod * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
274ec735bfSmiod * POSSIBILITY OF SUCH DAMAGE.
284ec735bfSmiod */
294ec735bfSmiod
304ec735bfSmiod #include <m88k/asm.h>
314ec735bfSmiod
324ec735bfSmiod typedef volatile u_int __cpu_simple_lock_t;
334ec735bfSmiod
344ec735bfSmiod /* do not change these - code below assumes r0 == __SIMPLELOCK_UNLOCKED */
354ec735bfSmiod #define __SIMPLELOCK_LOCKED 1
364ec735bfSmiod #define __SIMPLELOCK_UNLOCKED 0
374ec735bfSmiod
384ec735bfSmiod static __inline__ void
__cpu_simple_lock_init(__cpu_simple_lock_t * l)394ec735bfSmiod __cpu_simple_lock_init(__cpu_simple_lock_t *l)
404ec735bfSmiod {
414ec735bfSmiod *l = __SIMPLELOCK_UNLOCKED;
424ec735bfSmiod }
434ec735bfSmiod
444ec735bfSmiod static __inline__ int
__cpu_simple_lock_try(__cpu_simple_lock_t * l)454ec735bfSmiod __cpu_simple_lock_try(__cpu_simple_lock_t *l)
464ec735bfSmiod {
470e7425bdSmiod /*
480e7425bdSmiod * The local __cpu_simple_lock_t is not declared volatile, so that
490e7425bdSmiod * there are not pipeline synchronization around stores to it.
500e7425bdSmiod * xmem will do the right thing regardless of the volatile qualifier.
510e7425bdSmiod */
520e7425bdSmiod u_int old = __SIMPLELOCK_LOCKED;
534ec735bfSmiod
542df76cc2Sguenther __asm__ volatile
550b514a07Smiod ("xmem %0, %2, %%r0" : "+r"(old), "+m"(*l) : "r"(l));
564ec735bfSmiod
574ec735bfSmiod return (old == __SIMPLELOCK_UNLOCKED);
584ec735bfSmiod }
594ec735bfSmiod
604ec735bfSmiod static __inline__ void
__cpu_simple_lock(__cpu_simple_lock_t * l)617571c0e1Smiod __cpu_simple_lock(__cpu_simple_lock_t *l)
627571c0e1Smiod {
637571c0e1Smiod for (;;) {
647571c0e1Smiod if (__cpu_simple_lock_try(l) != 0)
657571c0e1Smiod break;
667571c0e1Smiod while (*l != __SIMPLELOCK_UNLOCKED)
677571c0e1Smiod ; /* spin without exclusive bus access */
687571c0e1Smiod }
697571c0e1Smiod }
707571c0e1Smiod
717571c0e1Smiod static __inline__ void
__cpu_simple_unlock(__cpu_simple_lock_t * l)724ec735bfSmiod __cpu_simple_unlock(__cpu_simple_lock_t *l)
734ec735bfSmiod {
74da4e928fSmiod *l = __SIMPLELOCK_UNLOCKED;
754ec735bfSmiod }
764ec735bfSmiod
774ec735bfSmiod #endif /* _M88K_LOCK_H_ */
78