lock.h revision 1.2
11.2Sbjh21/*	$NetBSD: lock.h,v 1.2 2002/10/07 23:23:53 bjh21 Exp $	*/
21.1Sreinoud
31.2Sbjh21/*-
41.2Sbjh21 * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc.
51.2Sbjh21 * All rights reserved.
61.2Sbjh21 *
71.2Sbjh21 * This code is derived from software contributed to The NetBSD Foundation
81.2Sbjh21 * by Jason R. Thorpe.
91.2Sbjh21 *
101.2Sbjh21 * Redistribution and use in source and binary forms, with or without
111.2Sbjh21 * modification, are permitted provided that the following conditions
121.2Sbjh21 * are met:
131.2Sbjh21 * 1. Redistributions of source code must retain the above copyright
141.2Sbjh21 *    notice, this list of conditions and the following disclaimer.
151.2Sbjh21 * 2. Redistributions in binary form must reproduce the above copyright
161.2Sbjh21 *    notice, this list of conditions and the following disclaimer in the
171.2Sbjh21 *    documentation and/or other materials provided with the distribution.
181.2Sbjh21 * 3. All advertising materials mentioning features or use of this software
191.2Sbjh21 *    must display the following acknowledgement:
201.2Sbjh21 *	This product includes software developed by the NetBSD
211.2Sbjh21 *	Foundation, Inc. and its contributors.
221.2Sbjh21 * 4. Neither the name of The NetBSD Foundation nor the names of its
231.2Sbjh21 *    contributors may be used to endorse or promote products derived
241.2Sbjh21 *    from this software without specific prior written permission.
251.2Sbjh21 *
261.2Sbjh21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.2Sbjh21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.2Sbjh21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.2Sbjh21 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.2Sbjh21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.2Sbjh21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.2Sbjh21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.2Sbjh21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.2Sbjh21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.2Sbjh21 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.2Sbjh21 * POSSIBILITY OF SUCH DAMAGE.
371.2Sbjh21 */
381.2Sbjh21
391.2Sbjh21#ifndef _ACORN32_LOCK_H_
401.2Sbjh21#define _ACORN32_LOCK_H_
411.2Sbjh21
421.2Sbjh21#ifdef _KERNEL_OPT
431.2Sbjh21#include "opt_multiprocessor.h"
441.2Sbjh21#endif
451.2Sbjh21
461.2Sbjh21#if defined(_KERNEL) && defined(MULTIPROCESSOR)
471.2Sbjh21
481.2Sbjh21#include <arm/cpufunc.h>
491.2Sbjh21
501.2Sbjh21typedef	__volatile int __cpu_simple_lock_t;
511.2Sbjh21
521.2Sbjh21#define	__SIMPLELOCK_LOCKED	1
531.2Sbjh21#define	__SIMPLELOCK_UNLOCKED	0
541.2Sbjh21
551.2Sbjh21static __inline int
561.2Sbjh21__swp(int __val, __volatile int *__ptr)
571.2Sbjh21{
581.2Sbjh21
591.2Sbjh21	__asm __volatile("swp %0, %1, [%2]"
601.2Sbjh21	    : "=r" (__val) : "r" (__val), "r" (__ptr) : "memory");
611.2Sbjh21	return __val;
621.2Sbjh21}
631.2Sbjh21
641.2Sbjh21static __inline void __attribute__((__unused__))
651.2Sbjh21__cpu_simple_lock_init(__cpu_simple_lock_t *alp)
661.2Sbjh21{
671.2Sbjh21
681.2Sbjh21	*alp = __SIMPLELOCK_UNLOCKED;
691.2Sbjh21}
701.2Sbjh21
711.2Sbjh21static __inline void __attribute__((__unused__))
721.2Sbjh21__cpu_simple_lock(__cpu_simple_lock_t *alp)
731.2Sbjh21{
741.2Sbjh21
751.2Sbjh21	while (__swp(__SIMPLELOCK_LOCKED, alp) != __SIMPLELOCK_UNLOCKED)
761.2Sbjh21		continue;
771.2Sbjh21	cpu_idcache_wbinv_all();
781.2Sbjh21}
791.2Sbjh21
801.2Sbjh21static __inline int __attribute__((__unused__))
811.2Sbjh21__cpu_simple_lock_try(__cpu_simple_lock_t *alp)
821.2Sbjh21{
831.2Sbjh21	int __result;
841.2Sbjh21
851.2Sbjh21	__result = __swp(__SIMPLELOCK_LOCKED, alp) == __SIMPLELOCK_UNLOCKED;
861.2Sbjh21	if (__result)
871.2Sbjh21		cpu_idcache_wbinv_all();
881.2Sbjh21	return __result;
891.2Sbjh21}
901.2Sbjh21
911.2Sbjh21static __inline void __attribute__((__unused__))
921.2Sbjh21__cpu_simple_unlock(__cpu_simple_lock_t *alp)
931.2Sbjh21{
941.2Sbjh21
951.2Sbjh21	*alp = __SIMPLELOCK_UNLOCKED;
961.2Sbjh21}
971.2Sbjh21
981.2Sbjh21#else /* !(_KERNEL && MULTIPROCESSOR) */
991.1Sreinoud#include <arm/lock.h>
1001.2Sbjh21#endif /* !(_KERNEL && MULTIPROCESSOR) */
1011.2Sbjh21#endif
102