1*2c53affbSjmc /* $OpenBSD: _atomic_lock.c,v 1.2 2022/12/27 17:10:06 jmc Exp $ */
27ecb72feSdrahn /*
37ecb72feSdrahn * Copyright (c) 2020 Mars Li <mengshi.li.mars@gmail.com>
47ecb72feSdrahn *
57ecb72feSdrahn * Permission to use, copy, modify, and distribute this software for any
67ecb72feSdrahn * purpose with or without fee is hereby granted, provided that the above
77ecb72feSdrahn * copyright notice and this permission notice appear in all copies.
87ecb72feSdrahn *
97ecb72feSdrahn * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
107ecb72feSdrahn * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
117ecb72feSdrahn * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
127ecb72feSdrahn * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
137ecb72feSdrahn * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
147ecb72feSdrahn * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
157ecb72feSdrahn * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167ecb72feSdrahn */
177ecb72feSdrahn
187ecb72feSdrahn /*
197ecb72feSdrahn * Atomic lock for riscv64
207ecb72feSdrahn */
217ecb72feSdrahn
227ecb72feSdrahn #include <sys/types.h>
237ecb72feSdrahn #include <machine/spinlock.h>
247ecb72feSdrahn
257ecb72feSdrahn /*
26*2c53affbSjmc * Spinlock does not have Acquire/Release semantics, so amoswap.w.aq
277ecb72feSdrahn * should not used here.
287ecb72feSdrahn */
297ecb72feSdrahn int
_atomic_lock(volatile _atomic_lock_t * lock)307ecb72feSdrahn _atomic_lock(volatile _atomic_lock_t *lock)
317ecb72feSdrahn {
327ecb72feSdrahn _atomic_lock_t old;
337ecb72feSdrahn
347ecb72feSdrahn /*
357ecb72feSdrahn * Use the amoswap instruction to swap the lock value with
367ecb72feSdrahn * a local variable containing the locked state.
377ecb72feSdrahn */
387ecb72feSdrahn __asm__("amoswap.w %0, %1, (%2)"
397ecb72feSdrahn : "=r" (old)
407ecb72feSdrahn : "r" (_ATOMIC_LOCK_LOCKED), "r" (lock) : "memory");
417ecb72feSdrahn
427ecb72feSdrahn return (old != _ATOMIC_LOCK_UNLOCKED);
437ecb72feSdrahn }
44