vlock.S 2.2 KB

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