xref: /openbmc/qemu/util/atomic64.c (revision e0bd743b)
1 /*
2  * Copyright (C) 2018, Emilio G. Cota <cota@braap.org>
3  *
4  * License: GNU GPL, version 2 or later.
5  *   See the COPYING file in the top-level directory.
6  */
7 #include "qemu/osdep.h"
8 #include "qemu/atomic.h"
9 #include "qemu/thread.h"
10 #include "qemu/cacheinfo.h"
11 
12 #ifdef CONFIG_ATOMIC64
13 #error This file must only be compiled if !CONFIG_ATOMIC64
14 #endif
15 
16 /*
17  * When !CONFIG_ATOMIC64, we serialize both reads and writes with spinlocks.
18  * We use an array of spinlocks, with padding computed at run-time based on
19  * the host's dcache line size.
20  * We point to the array with a void * to simplify the padding's computation.
21  * Each spinlock is located every lock_size bytes.
22  */
23 static void *lock_array;
24 static size_t lock_size;
25 
26 /*
27  * Systems without CONFIG_ATOMIC64 are unlikely to have many cores, so we use a
28  * small array of locks.
29  */
30 #define NR_LOCKS 16
31 
32 static QemuSpin *addr_to_lock(const void *addr)
33 {
34     uintptr_t a = (uintptr_t)addr;
35     uintptr_t idx;
36 
37     idx = a >> qemu_dcache_linesize_log;
38     idx ^= (idx >> 8) ^ (idx >> 16);
39     idx &= NR_LOCKS - 1;
40     return lock_array + idx * lock_size;
41 }
42 
43 #define GEN_READ(name, type)                    \
44     type name(const type *ptr)                  \
45     {                                           \
46         QemuSpin *lock = addr_to_lock(ptr);     \
47         type ret;                               \
48                                                 \
49         qemu_spin_lock(lock);                   \
50         ret = *ptr;                             \
51         qemu_spin_unlock(lock);                 \
52         return ret;                             \
53     }
54 
55 GEN_READ(qatomic_read_i64, int64_t)
56 GEN_READ(qatomic_read_u64, uint64_t)
57 #undef GEN_READ
58 
59 #define GEN_SET(name, type)                     \
60     void name(type *ptr, type val)              \
61     {                                           \
62         QemuSpin *lock = addr_to_lock(ptr);     \
63                                                 \
64         qemu_spin_lock(lock);                   \
65         *ptr = val;                             \
66         qemu_spin_unlock(lock);                 \
67     }
68 
69 GEN_SET(qatomic_set_i64, int64_t)
70 GEN_SET(qatomic_set_u64, uint64_t)
71 #undef GEN_SET
72 
73 void qatomic64_init(void)
74 {
75     int i;
76 
77     lock_size = ROUND_UP(sizeof(QemuSpin), qemu_dcache_linesize);
78     lock_array = qemu_memalign(qemu_dcache_linesize, lock_size * NR_LOCKS);
79     for (i = 0; i < NR_LOCKS; i++) {
80         QemuSpin *lock = lock_array + i * lock_size;
81 
82         qemu_spin_init(lock);
83     }
84 }
85