Skip to content

Commit

Permalink
Merge "refactor(lib/granule): redefine granule structure" into integr…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
soby-mathew authored and TrustedFirmware Code Review committed Jun 21, 2024
2 parents 31a9f62 + 745499d commit e7f8432
Show file tree
Hide file tree
Showing 20 changed files with 975 additions and 620 deletions.
46 changes: 46 additions & 0 deletions lib/arch/include/aarch64/atomics.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ static inline void atomic_add_16(uint16_t *loc, uint16_t val)
: "memory");
}

/*
* Atomically adds @val to the 16-bit value stored at memory location @loc.
* Returns the old value.
*/
static inline uint16_t atomic_load_add_16(uint16_t *loc, uint16_t val)
{
uint16_t old_val;

/* To avoid misra-c2012-2.7 warnings */
(void)loc;
(void)val;

asm volatile(
" ldaddh %w[val], %w[old_val], %[loc]\n"
: [loc] "+Q" (*loc),
[old_val] "=r" (old_val)
: [val] "r" (val)
: "memory");

return old_val;
}

/*
* Atomically adds @val to the 16-bit value stored at memory location @loc.
* Stores to memory with release semantics.
Expand Down Expand Up @@ -172,4 +194,28 @@ static inline bool atomic_bit_set_acquire_release_64(uint64_t *loc, unsigned int
return ((val & mask) != 0UL);
}

/*
* Atomically performs exclusive-OR with @val on the 16-bit value stored at memory
* location @loc and stores the result back to memory.
* Returns the old value.
*/
static inline uint16_t atomic_eor_16(uint16_t *loc, uint16_t val)
{
uint16_t old_val;

/* To avoid misra-c2012-2.7 warnings */
(void)loc;
(void)val;

asm volatile(
" ldeorh %w[val], %w[old_val], %[loc]\n"
: [loc] "+Q" (*loc),
[old_val] "=r" (old_val)
: [val] "r" (val)
: "memory"
);

return old_val;
}

#endif /* ATOMICS_H */
37 changes: 31 additions & 6 deletions lib/arch/include/fake_host/atomics.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
static inline void atomic_add_64(uint64_t *loc, uint64_t val)
{
*loc = *loc + val;
*loc += val;
}

/*
Expand All @@ -26,7 +26,7 @@ static inline uint64_t atomic_load_add_release_64(uint64_t *loc, uint64_t val)
{
uint64_t old_val = *loc;

*loc = *loc + val;
*loc += val;
return old_val;
}

Expand All @@ -35,7 +35,19 @@ static inline uint64_t atomic_load_add_release_64(uint64_t *loc, uint64_t val)
*/
static inline void atomic_add_16(uint16_t *loc, uint16_t val)
{
*loc = *loc + val;
*loc += val;
}

/*
* Atomically adds @val to the 16-bit value stored at memory location @loc.
* Returns the old value.
*/
static inline uint16_t atomic_load_add_16(uint16_t *loc, uint16_t val)
{
uint16_t old_val = *loc;

*loc += val;
return old_val;
}

/*
Expand All @@ -47,7 +59,7 @@ static inline uint16_t atomic_load_add_release_16(uint16_t *loc, uint16_t val)
{
uint16_t old_val = *loc;

*loc = *loc + val;
*loc += val;
return old_val;
}

Expand All @@ -58,7 +70,7 @@ static inline void atomic_bit_set_release_64(uint64_t *loc, unsigned int bit)
{
uint64_t mask = (1UL << bit);

*loc = *loc | mask;
*loc |= mask;
}

/*
Expand All @@ -68,7 +80,7 @@ static inline void atomic_bit_clear_release_64(uint64_t *loc, unsigned int bit)
{
uint64_t mask = ~((uint64_t)(1UL << bit));

*loc = *loc & mask;
*loc &= mask;
}

/*
Expand Down Expand Up @@ -96,4 +108,17 @@ static inline bool atomic_bit_set_acquire_release_64(uint64_t *loc, unsigned int
return (old_val != 0UL);
}

/*
* Atomically performs exclusive-OR with @val on the 16-bit value stored at memory
* location @loc and stores the result back to memory.
* Returns the old value.
*/
static inline uint16_t atomic_eor_16(uint16_t *loc, uint16_t val)
{
uint16_t old_val = *loc;

*loc ^= val;
return old_val;
}

#endif /* ATOMICS_H */
3 changes: 2 additions & 1 deletion lib/granule/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ target_link_libraries(rmm-lib-granule
PRIVATE rmm-lib-debug)

target_include_directories(rmm-lib-granule
PUBLIC "include")
PUBLIC "include"
"include/${RMM_ARCH}")

target_sources(rmm-lib-granule
PRIVATE "src/granule.c")
Expand Down
48 changes: 48 additions & 0 deletions lib/granule/include/aarch64/granule_lock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* SPDX-License-Identifier: BSD-3-Clause
* SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
*/

#ifndef GRANULE_LOCK_H
#define GRANULE_LOCK_H

#include <granule_types.h>

static inline void granule_bitlock_acquire(struct granule *g)
{
/* To avoid misra-c2012-2.7 warnings */
(void)g;
uint32_t tmp;
uint32_t mask = GRN_LOCK_BIT;

asm volatile(
"1: ldsetah %w[mask], %w[tmp], %[lock]\n"
" tbz %w[tmp], #%c[bit], 2f\n"
" ldxrh %w[tmp], %[lock]\n"
" tbz %w[tmp], #%c[bit], 1b\n"
" wfe\n"
" b 1b\n"
"2:\n"
: [lock] "+Q" (g->descriptor),
[tmp] "=&r" (tmp)
: [mask] "r" (mask),
[bit] "i" (GRN_LOCK_SHIFT)
: "memory"
);
}

static inline void granule_bitlock_release(struct granule *g)
{
/* To avoid misra-c2012-2.7 warnings */
(void)g;
uint32_t mask = GRN_LOCK_BIT;

asm volatile(
" stclrlh %w[mask], %[lock]\n"
: [lock] "+Q" (g->descriptor)
: [mask] "r" (mask)
: "memory"
);
}

#endif /* GRANULE_LOCK_H */
28 changes: 28 additions & 0 deletions lib/granule/include/fake_host/granule_lock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* SPDX-License-Identifier: BSD-3-Clause
* SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
*/

#ifndef GRANULE_LOCK_H
#define GRANULE_LOCK_H

#include <assert.h>
#include <granule_types.h>

static inline void granule_bitlock_acquire(struct granule *g)
{
/*
* The fake_host architecture is single threaded and we do not expect
* the lock to be already acquired in properly implemented locking
* sequence.
*/
assert((g->descriptor & GRN_LOCK_BIT) == 0);
g->descriptor |= GRN_LOCK_BIT;
}

static inline void granule_bitlock_release(struct granule *g)
{
g->descriptor &= ~GRN_LOCK_BIT;
}

#endif /* GRANULE_LOCK_H */
Loading

0 comments on commit e7f8432

Please sign in to comment.