vlock.S 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * vlock.S - simple voting lock implementation for ARM
  3. *
  4. * Created by: Dave Martin, 2012-08-16
  5. * Copyright: (C) 2012-2013 Linaro Limited
  6. *
  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.
  10. *
  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.
  15. *
  16. *
  17. * This algorithm is described in more detail in
  18. * Documentation/arm/vlocks.txt.
  19. */
  20. #include <linux/linkage.h>
  21. #include "vlock.h"
  22. /* Select different code if voting flags can fit in a single word. */
  23. #if VLOCK_VOTING_SIZE > 4
  24. #define FEW(x...)
  25. #define MANY(x...) x
  26. #else
  27. #define FEW(x...) x
  28. #define MANY(x...)
  29. #endif
  30. @ voting lock for first-man coordination
  31. .macro voting_begin rbase:req, rcpu:req, rscratch:req
  32. mov \rscratch, #1
  33. strb \rscratch, [\rbase, \rcpu]
  34. dmb
  35. .endm
  36. .macro voting_end rbase:req, rcpu:req, rscratch:req
  37. dmb
  38. mov \rscratch, #0
  39. strb \rscratch, [\rbase, \rcpu]
  40. dsb st
  41. sev
  42. .endm
  43. /*
  44. * The vlock structure must reside in Strongly-Ordered or Device memory.
  45. * This implementation deliberately eliminates most of the barriers which
  46. * would be required for other memory types, and assumes that independent
  47. * writes to neighbouring locations within a cacheline do not interfere
  48. * with one another.
  49. */
  50. @ r0: lock structure base
  51. @ r1: CPU ID (0-based index within cluster)
  52. ENTRY(vlock_trylock)
  53. add r1, r1, #VLOCK_VOTING_OFFSET
  54. voting_begin r0, r1, r2
  55. ldrb r2, [r0, #VLOCK_OWNER_OFFSET] @ check whether lock is held
  56. cmp r2, #VLOCK_OWNER_NONE
  57. bne trylock_fail @ fail if so
  58. @ Control dependency implies strb not observable before previous ldrb.
  59. strb r1, [r0, #VLOCK_OWNER_OFFSET] @ submit my vote
  60. voting_end r0, r1, r2 @ implies DMB
  61. @ Wait for the current round of voting to finish:
  62. MANY( mov r3, #VLOCK_VOTING_OFFSET )
  63. 0:
  64. MANY( ldr r2, [r0, r3] )
  65. FEW( ldr r2, [r0, #VLOCK_VOTING_OFFSET] )
  66. cmp r2, #0
  67. wfene
  68. bne 0b
  69. MANY( add r3, r3, #4 )
  70. MANY( cmp r3, #VLOCK_VOTING_OFFSET + VLOCK_VOTING_SIZE )
  71. MANY( bne 0b )
  72. @ Check who won:
  73. dmb
  74. ldrb r2, [r0, #VLOCK_OWNER_OFFSET]
  75. eor r0, r1, r2 @ zero if I won, else nonzero
  76. bx lr
  77. trylock_fail:
  78. voting_end r0, r1, r2
  79. mov r0, #1 @ nonzero indicates that I lost
  80. bx lr
  81. ENDPROC(vlock_trylock)
  82. @ r0: lock structure base
  83. ENTRY(vlock_unlock)
  84. dmb
  85. mov r1, #VLOCK_OWNER_NONE
  86. strb r1, [r0, #VLOCK_OWNER_OFFSET]
  87. dsb st
  88. sev
  89. bx lr
  90. ENDPROC(vlock_unlock)