1 /* -------------------------------------------------------------- */
2 /* (C)Copyright 2001,2007, */
3 /* International Business Machines Corporation, */
4 /* Sony Computer Entertainment, Incorporated, */
5 /* Toshiba Corporation, */
7 /* All Rights Reserved. */
9 /* Redistribution and use in source and binary forms, with or */
10 /* without modification, are permitted provided that the */
11 /* following conditions are met: */
13 /* - Redistributions of source code must retain the above copyright*/
14 /* notice, this list of conditions and the following disclaimer. */
16 /* - Redistributions in binary form must reproduce the above */
17 /* copyright notice, this list of conditions and the following */
18 /* disclaimer in the documentation and/or other materials */
19 /* provided with the distribution. */
21 /* - Neither the name of IBM Corporation nor the names of its */
22 /* contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written */
26 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND */
27 /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */
28 /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
29 /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
30 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR */
31 /* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
32 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT */
33 /* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
34 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) */
35 /* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN */
36 /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR */
37 /* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
38 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
39 /* -------------------------------------------------------------- */
40 /* PROLOG END TAG zYx */
42 #define _SPU_MUTEX_H_ 1
44 #include "sync_utils.h"
46 #include <spu_mfcio.h>
48 typedef eaddr_t mutex_ea_t;
50 #define DECL_MUTEX_VARS() \
52 char *tmp = (char *) ALIGN(_tmp, 128); \
53 volatile s32 *buf = (volatile s32 *) &tmp[0]; \
54 u32 size = 128, tagid = 0; \
59 * Macro implementing the test and set primitive.
61 * RAW_TEST_AND_SET(==, 1) used by spin_try_lock()
62 * RAW_TEST_AND_SET(>=, 1) used by read_try_lock()
63 * RAW_TEST_AND_SET(==, -1) used by write_try_lock()
65 #define RAW_TEST_AND_SET(_RELOP_, _val) \
66 ea64.ull = ALIGN128_EA(ea); \
67 offset = OFFSET128_EA_U32(ea); \
69 MFC_DMA(buf, ea64, size, tagid, MFC_GETLLAR_CMD); \
70 spu_readch(MFC_RdAtomicStat); \
72 if (likely(buf[offset] _RELOP_ 0)) { \
73 buf[offset] += _val; \
74 MFC_DMA(buf, ea64, size, tagid, MFC_PUTLLC_CMD); \
75 status = spu_readch(MFC_RdAtomicStat); \
78 MFC_DMA(buf, ea64, size, tagid, MFC_PUTLLC_CMD); \
79 spu_readch(MFC_RdAtomicStat); \
80 status = ret_val = 0; \
86 * Macro implementing the spinlock primitive.
88 * RAW_SPINLOCK(==, 1) used by spin_lock()
89 * RAW_SPINLOCK(>=, 1) used by read_lock()
90 * RAW_SPINLOCK(==, -1) used by write_lock()
94 #define RAW_SPINLOCK(_RELOP_, _val) \
95 ea64.ull = ALIGN128_EA(ea); \
96 offset = OFFSET128_EA_U32(ea); \
98 MFC_DMA(buf, ea64, size, tagid, MFC_GETLLAR_CMD); \
99 spu_readch(MFC_RdAtomicStat); \
102 if (likely(buf[offset] _RELOP_ 0)) { \
103 buf[offset] += _val; \
104 MFC_DMA(buf, ea64, size, tagid, MFC_PUTLLC_CMD); \
105 status = spu_readch(MFC_RdAtomicStat); \
107 } while (status != 0);
110 static inline void _lock_init(eaddr_t ea)
113 u32 oldtmask, tagmask;
115 tagid = mfc_tag_reserve();
117 tagmask = 1 << (tagid & 31);
120 * Internal routine to initialize a spinlock or
121 * reader/writer lock.
123 ea64.ull = ALIGN128_EA(ea);
124 offset = OFFSET128_EA_U32(ea);
125 MFC_DMA(buf, ea64, size, tagid & 31, MFC_GET_CMD);
126 oldtmask = spu_readch(MFC_RdTagMask);
127 spu_writech(MFC_WrTagMask, tagmask);
128 spu_writech(MFC_WrTagUpdate, MFC_TAG_UPDATE_ANY);
129 spu_readch(MFC_RdTagStat);
132 MFC_DMA(buf, ea64, size, tagid & 31, MFC_PUT_CMD);
133 spu_writech(MFC_WrTagMask, tagmask);
134 spu_writech(MFC_WrTagUpdate, MFC_TAG_UPDATE_ANY);
135 spu_readch(MFC_RdTagStat);
136 spu_writech(MFC_WrTagMask, oldtmask);
137 mfc_tag_release(tagid);
140 static inline void _spin_lock(eaddr_t ea)
146 * Internal routine to acquire spinlock.
148 /* non trace - no hit/miss indicator */
154 static inline s32 _spin_lock_trace(eaddr_t ea)
158 /* trace - need hit/miss indicator */
160 #define SET_HIT if (status == 0) hit = buf[offset]
164 /* _spin_lock_trace for trace
165 * Internal routine to acquire spinlock.
175 #endif /* LIBSYNC_TRACE */
178 #endif /* SPU_MUTEX_H */