summaryrefslogtreecommitdiffstats
path: root/MdePkg/Library/BaseSynchronizationLib/RiscV64/Synchronization.S
blob: bac80d687168866a58f2a7d0665d5ee47e29cd2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//------------------------------------------------------------------------------
//
// RISC-V synchronization functions.
//
// Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
//------------------------------------------------------------------------------
#include <Base.h>

.data

.text
.align 3

.global ASM_PFX(InternalSyncCompareExchange32)
.global ASM_PFX(InternalSyncCompareExchange64)
.global ASM_PFX(InternalSyncIncrement)
.global ASM_PFX(InternalSyncDecrement)

//
// ompare and xchange a 32-bit value.
//
// @param a0 : Pointer to 32-bit value.
// @param a1 : Compare value.
// @param a2 : Exchange value.
//
ASM_PFX (InternalSyncCompareExchange32):
    lr.w  a3, (a0)        // Load the value from a0 and make
                          // the reservation of address.
    bne   a3, a1, exit
    sc.w  a3, a2, (a0)    // Write the value back to the address.
    mv    a3, a1
exit:
    mv    a0, a3
    ret

.global ASM_PFX(InternalSyncCompareExchange64)

//
// Compare and xchange a 64-bit value.
//
// @param a0 : Pointer to 64-bit value.
// @param a1 : Compare value.
// @param a2 : Exchange value.
//
ASM_PFX (SyncCompareExchange64):
    lr.d  a3, (a0)       // Load the value from a0 and make
                         // the reservation of address.
    bne   a3, a1, exit
    sc.d  a3, a2, (a0)   // Write the value back to the address.
    mv    a3, a1
exit2:
    mv    a0, a3
    ret

//
// Performs an atomic increment of an 32-bit unsigned integer.
//
// @param a0 : Pointer to 32-bit value.
//
ASM_PFX (InternalSyncIncrement):
    li  a1, 1
    amoadd.w  a2, a1, (a0)
    mv  a0, a2
    ret

//
// Performs an atomic decrement of an 32-bit unsigned integer.
//
// @param a0 : Pointer to 32-bit value.
//
ASM_PFX (InternalSyncDecrement):
    li  a1, -1
    amoadd.w  a2, a1, (a0)
    mv  a0, a2
    ret