xref: /illumos-gate/usr/src/uts/common/io/bge/bge_atomic.c (revision 4de2612967d06c4fdbf524a62556a1e8118a006f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "sys/bge_impl.h"
30 
31 /*
32  * Atomically decrement a counter, but only if it will remain
33  * strictly positive (greater than zero) afterwards.  We return
34  * the decremented value if so, otherwise zero (in which case
35  * the counter is unchanged).
36  *
37  * This is used for keeping track of available resources such
38  * as transmit ring slots ...
39  */
40 uint64_t
41 bge_atomic_reserve(uint64_t *count_p, uint64_t n)
42 {
43 	uint64_t oldval;
44 	uint64_t newval;
45 
46 	/* ATOMICALLY */
47 	do {
48 		oldval = *count_p;
49 		newval = oldval - n;
50 		if (oldval <= n)
51 			return (0);		/* no resources left	*/
52 	} while (cas64(count_p, oldval, newval) != oldval);
53 
54 	return (newval);
55 }
56 
57 /*
58  * Atomically increment a counter
59  */
60 void
61 bge_atomic_renounce(uint64_t *count_p, uint64_t n)
62 {
63 	uint64_t oldval;
64 	uint64_t newval;
65 
66 	/* ATOMICALLY */
67 	do {
68 		oldval = *count_p;
69 		newval = oldval + n;
70 	} while (cas64(count_p, oldval, newval) != oldval);
71 }
72 
73 /*
74  * Atomically claim a slot in a descriptor ring
75  */
76 uint64_t
77 bge_atomic_claim(uint64_t *count_p, uint64_t limit)
78 {
79 	uint64_t oldval;
80 	uint64_t newval;
81 
82 	/* ATOMICALLY */
83 	do {
84 		oldval = *count_p;
85 		newval = NEXT(oldval, limit);
86 	} while (cas64(count_p, oldval, newval) != oldval);
87 
88 	return (oldval);
89 }
90 
91 /*
92  * Atomically clear bits in a 64-bit word, returning
93  * the value it had *before* the bits were cleared.
94  */
95 uint64_t
96 bge_atomic_clr64(uint64_t *sp, uint64_t bits)
97 {
98 	uint64_t oldval;
99 	uint64_t newval;
100 
101 	/* ATOMICALLY */
102 	do {
103 		oldval = *sp;
104 		newval = oldval & ~bits;
105 	} while (cas64(sp, oldval, newval) != oldval);
106 
107 	return (oldval);
108 }
109 
110 /*
111  * Atomically shift a 32-bit word left, returning
112  * the value it had *before* the shift was applied
113  */
114 uint32_t
115 bge_atomic_shl32(uint32_t *sp, uint_t count)
116 {
117 	uint32_t oldval;
118 	uint32_t newval;
119 
120 	/* ATOMICALLY */
121 	do {
122 		oldval = *sp;
123 		newval = oldval << count;
124 	} while (cas32(sp, oldval, newval) != oldval);
125 
126 	return (oldval);
127 }
128