2 * vlock.S - simple voting lock implementation for ARM
4 * Created by: Dave Martin, 2012-08-16
5 * Copyright: (C) 2012-2013 Linaro Limited
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 * This algorithm is described in more detail in
18 * Documentation/arm/vlocks.txt.
21 #include <linux/linkage.h>
24 /* Select different code if voting flags can fit in a single word. */
25 #if VLOCK_VOTING_SIZE > 4
33 @ voting lock for first-man coordination
35 .macro voting_begin rbase:req, rcpu:req, rscratch:req
37 strb \rscratch, [\rbase, \rcpu]
41 .macro voting_end rbase:req, rcpu:req, rscratch:req
44 strb \rscratch, [\rbase, \rcpu]
50 * The vlock structure must reside in Strongly-Ordered or Device memory.
51 * This implementation deliberately eliminates most of the barriers which
52 * would be required for other memory types, and assumes that independent
53 * writes to neighbouring locations within a cacheline do not interfere
57 @ r0: lock structure base
58 @ r1: CPU ID (0-based index within cluster)
60 add r1, r1, #VLOCK_VOTING_OFFSET
62 voting_begin r0, r1, r2
64 ldrb r2, [r0, #VLOCK_OWNER_OFFSET] @ check whether lock is held
65 cmp r2, #VLOCK_OWNER_NONE
66 bne trylock_fail @ fail if so
68 @ Control dependency implies strb not observable before previous ldrb.
70 strb r1, [r0, #VLOCK_OWNER_OFFSET] @ submit my vote
72 voting_end r0, r1, r2 @ implies DMB
74 @ Wait for the current round of voting to finish:
76 MANY( mov r3, #VLOCK_VOTING_OFFSET )
78 MANY( ldr r2, [r0, r3] )
79 FEW( ldr r2, [r0, #VLOCK_VOTING_OFFSET] )
83 MANY( add r3, r3, #4 )
84 MANY( cmp r3, #VLOCK_VOTING_OFFSET + VLOCK_VOTING_SIZE )
90 ldrb r2, [r0, #VLOCK_OWNER_OFFSET]
91 eor r0, r1, r2 @ zero if I won, else nonzero
96 mov r0, #1 @ nonzero indicates that I lost
98 ENDPROC(vlock_trylock)
100 @ r0: lock structure base
103 mov r1, #VLOCK_OWNER_NONE
104 strb r1, [r0, #VLOCK_OWNER_OFFSET]
108 ENDPROC(vlock_unlock)