vlock.S 2.2 KB

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