1 /* $NetBSD: lock.h,v 1.19 2022/02/13 13:41:53 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gregory McGarry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Machine-dependent spin lock operations.
34 */
35
36 #ifndef _SH3_LOCK_H_
37 #define _SH3_LOCK_H_
38
39 static __inline void __cpu_simple_lock_init(__cpu_simple_lock_t *)
40 __attribute__((__unused__));
41 static __inline void __cpu_simple_lock(__cpu_simple_lock_t *)
42 __attribute__((__unused__));
43 static __inline int __cpu_simple_lock_try(__cpu_simple_lock_t *)
44 __attribute__((__unused__));
45 static __inline void __cpu_simple_unlock(__cpu_simple_lock_t *)
46 __attribute__((__unused__));
47
48 static __inline int
__SIMPLELOCK_LOCKED_P(const __cpu_simple_lock_t * __ptr)49 __SIMPLELOCK_LOCKED_P(const __cpu_simple_lock_t *__ptr)
50 {
51 return *__ptr == __SIMPLELOCK_LOCKED;
52 }
53
54 static __inline int
__SIMPLELOCK_UNLOCKED_P(const __cpu_simple_lock_t * __ptr)55 __SIMPLELOCK_UNLOCKED_P(const __cpu_simple_lock_t *__ptr)
56 {
57 return *__ptr == __SIMPLELOCK_UNLOCKED;
58 }
59
60 static __inline void
__cpu_simple_lock_clear(__cpu_simple_lock_t * __ptr)61 __cpu_simple_lock_clear(__cpu_simple_lock_t *__ptr)
62 {
63 *__ptr = __SIMPLELOCK_UNLOCKED;
64 }
65
66 static __inline void
__cpu_simple_lock_set(__cpu_simple_lock_t * __ptr)67 __cpu_simple_lock_set(__cpu_simple_lock_t *__ptr)
68 {
69 *__ptr = __SIMPLELOCK_LOCKED;
70 }
71
72 static __inline void
__cpu_simple_lock_init(__cpu_simple_lock_t * alp)73 __cpu_simple_lock_init(__cpu_simple_lock_t *alp)
74 {
75
76 *alp = __SIMPLELOCK_UNLOCKED;
77 }
78
79 static __inline void
__cpu_simple_lock(__cpu_simple_lock_t * alp)80 __cpu_simple_lock(__cpu_simple_lock_t *alp)
81 {
82
83 __asm volatile(
84 "1: tas.b @%0 \n"
85 " bf 1b \n"
86 : /* no outputs */
87 : "r" (alp)
88 : "cc", "memory");
89 }
90
91 static __inline int
__cpu_simple_lock_try(__cpu_simple_lock_t * alp)92 __cpu_simple_lock_try(__cpu_simple_lock_t *alp)
93 {
94 int __rv;
95
96 __asm volatile(
97 " tas.b @%1 \n"
98 " movt %0 \n"
99 : "=r" (__rv)
100 : "r" (alp)
101 : "cc", "memory");
102
103 return (__rv);
104 }
105
106 static __inline void
__cpu_simple_unlock(__cpu_simple_lock_t * alp)107 __cpu_simple_unlock(__cpu_simple_lock_t *alp)
108 {
109
110 __insn_barrier();
111 *alp = __SIMPLELOCK_UNLOCKED;
112 }
113
114 #endif /* !_SH3_LOCK_H_ */
115