1/*	$NetBSD: atomic_cas_generic.c,v 1.1 2009/01/12 02:22:55 pooka Exp $	*/
2
3/*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: atomic_cas_generic.c,v 1.1 2009/01/12 02:22:55 pooka Exp $");
31
32/*
33 * This is basically common/lib/libc/atomic/atomic_init_testset.c
34 * which always assumes MP and never uses RAS.  This is used only on
35 * some archs.  See Makefile.rumpkern for more information.
36 */
37
38#include "atomic_op_namespace.h"
39
40#include <sys/types.h>
41#include <sys/atomic.h>
42#include <sys/lock.h>
43
44#define	I2	__SIMPLELOCK_UNLOCKED, __SIMPLELOCK_UNLOCKED,
45#define	I16	I2 I2 I2 I2 I2 I2 I2 I2
46#define	I128	I16 I16 I16 I16 I16 I16 I16 I16
47
48static __cpu_simple_lock_t atomic_locks[128] = { I128 };
49
50uint32_t
51_atomic_cas_32(volatile uint32_t *ptr, uint32_t old, uint32_t new)
52{
53	__cpu_simple_lock_t *lock;
54	uint32_t ret;
55
56	lock = &atomic_locks[((uintptr_t)ptr >> 3) & 127];
57	__cpu_simple_lock(lock);
58	ret = *ptr;
59	if (__predict_true(ret == old)) {
60		*ptr = new;
61	}
62	__cpu_simple_unlock(lock);
63
64	return ret;
65}
66
67#undef atomic_cas_32
68#undef atomic_cas_uint
69#undef atomic_cas_ulong
70#undef atomic_cas_ptr
71
72atomic_op_alias(atomic_cas_32,_atomic_cas_32)
73atomic_op_alias(atomic_cas_uint,_atomic_cas_32)
74__strong_alias(_atomic_cas_uint,_atomic_cas_32)
75atomic_op_alias(atomic_cas_ulong,_atomic_cas_32)
76__strong_alias(_atomic_cas_ulong,_atomic_cas_32)
77atomic_op_alias(atomic_cas_ptr,_atomic_cas_32)
78__strong_alias(_atomic_cas_ptr,_atomic_cas_32)
79
80atomic_op_alias(atomic_cas_32_ni,_atomic_cas_32)
81__strong_alias(_atomic_cas_32_ni,_atomic_cas_32)
82atomic_op_alias(atomic_cas_uint_ni,_atomic_cas_32)
83__strong_alias(_atomic_cas_uint_ni,_atomic_cas_32)
84atomic_op_alias(atomic_cas_ulong_ni,_atomic_cas_32)
85__strong_alias(_atomic_cas_ulong_ni,_atomic_cas_32)
86atomic_op_alias(atomic_cas_ptr_ni,_atomic_cas_32)
87__strong_alias(_atomic_cas_ptr_ni,_atomic_cas_32)
88