10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51369Sdduvall * Common Development and Distribution License (the "License").
61369Sdduvall * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211369Sdduvall
220Sstevel@tonic-gate /*
231369Sdduvall * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
292675Szh199473 #include "bge_impl.h"
300Sstevel@tonic-gate
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate * Atomically decrement a counter, but only if it will remain
330Sstevel@tonic-gate * strictly positive (greater than zero) afterwards. We return
340Sstevel@tonic-gate * the decremented value if so, otherwise zero (in which case
350Sstevel@tonic-gate * the counter is unchanged).
360Sstevel@tonic-gate *
370Sstevel@tonic-gate * This is used for keeping track of available resources such
380Sstevel@tonic-gate * as transmit ring slots ...
390Sstevel@tonic-gate */
400Sstevel@tonic-gate uint64_t
bge_atomic_reserve(uint64_t * count_p,uint64_t n)410Sstevel@tonic-gate bge_atomic_reserve(uint64_t *count_p, uint64_t n)
420Sstevel@tonic-gate {
430Sstevel@tonic-gate uint64_t oldval;
440Sstevel@tonic-gate uint64_t newval;
450Sstevel@tonic-gate
460Sstevel@tonic-gate /* ATOMICALLY */
470Sstevel@tonic-gate do {
480Sstevel@tonic-gate oldval = *count_p;
490Sstevel@tonic-gate newval = oldval - n;
500Sstevel@tonic-gate if (oldval <= n)
510Sstevel@tonic-gate return (0); /* no resources left */
520Sstevel@tonic-gate } while (cas64(count_p, oldval, newval) != oldval);
530Sstevel@tonic-gate
540Sstevel@tonic-gate return (newval);
550Sstevel@tonic-gate }
560Sstevel@tonic-gate
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate * Atomically increment a counter
590Sstevel@tonic-gate */
600Sstevel@tonic-gate void
bge_atomic_renounce(uint64_t * count_p,uint64_t n)610Sstevel@tonic-gate bge_atomic_renounce(uint64_t *count_p, uint64_t n)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate uint64_t oldval;
640Sstevel@tonic-gate uint64_t newval;
650Sstevel@tonic-gate
660Sstevel@tonic-gate /* ATOMICALLY */
670Sstevel@tonic-gate do {
680Sstevel@tonic-gate oldval = *count_p;
690Sstevel@tonic-gate newval = oldval + n;
700Sstevel@tonic-gate } while (cas64(count_p, oldval, newval) != oldval);
710Sstevel@tonic-gate }
720Sstevel@tonic-gate
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate * Atomically claim a slot in a descriptor ring
750Sstevel@tonic-gate */
760Sstevel@tonic-gate uint64_t
bge_atomic_claim(uint64_t * count_p,uint64_t limit)770Sstevel@tonic-gate bge_atomic_claim(uint64_t *count_p, uint64_t limit)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate uint64_t oldval;
800Sstevel@tonic-gate uint64_t newval;
810Sstevel@tonic-gate
820Sstevel@tonic-gate /* ATOMICALLY */
830Sstevel@tonic-gate do {
840Sstevel@tonic-gate oldval = *count_p;
850Sstevel@tonic-gate newval = NEXT(oldval, limit);
860Sstevel@tonic-gate } while (cas64(count_p, oldval, newval) != oldval);
870Sstevel@tonic-gate
880Sstevel@tonic-gate return (oldval);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
910Sstevel@tonic-gate /*
92*3334Sgs150176 * Atomically NEXT a 64-bit integer, returning the
93*3334Sgs150176 * value it had *before* the NEXT was applied
94*3334Sgs150176 */
95*3334Sgs150176 uint64_t
bge_atomic_next(uint64_t * sp,uint64_t limit)96*3334Sgs150176 bge_atomic_next(uint64_t *sp, uint64_t limit)
97*3334Sgs150176 {
98*3334Sgs150176 uint64_t oldval;
99*3334Sgs150176 uint64_t newval;
100*3334Sgs150176
101*3334Sgs150176 /* ATOMICALLY */
102*3334Sgs150176 do {
103*3334Sgs150176 oldval = *sp;
104*3334Sgs150176 newval = NEXT(oldval, limit);
105*3334Sgs150176 } while (cas64(sp, oldval, newval) != oldval);
106*3334Sgs150176
107*3334Sgs150176 return (oldval);
108*3334Sgs150176 }
109*3334Sgs150176
110*3334Sgs150176 /*
111*3334Sgs150176 * Atomically decrement a counter
112*3334Sgs150176 */
113*3334Sgs150176 void
bge_atomic_sub64(uint64_t * count_p,uint64_t n)114*3334Sgs150176 bge_atomic_sub64(uint64_t *count_p, uint64_t n)
115*3334Sgs150176 {
116*3334Sgs150176 uint64_t oldval;
117*3334Sgs150176 uint64_t newval;
118*3334Sgs150176
119*3334Sgs150176 /* ATOMICALLY */
120*3334Sgs150176 do {
121*3334Sgs150176 oldval = *count_p;
122*3334Sgs150176 newval = oldval - n;
123*3334Sgs150176 } while (cas64(count_p, oldval, newval) != oldval);
124*3334Sgs150176 }
125*3334Sgs150176
126*3334Sgs150176 /*
1270Sstevel@tonic-gate * Atomically clear bits in a 64-bit word, returning
1280Sstevel@tonic-gate * the value it had *before* the bits were cleared.
1290Sstevel@tonic-gate */
1300Sstevel@tonic-gate uint64_t
bge_atomic_clr64(uint64_t * sp,uint64_t bits)1310Sstevel@tonic-gate bge_atomic_clr64(uint64_t *sp, uint64_t bits)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate uint64_t oldval;
1340Sstevel@tonic-gate uint64_t newval;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate /* ATOMICALLY */
1370Sstevel@tonic-gate do {
1380Sstevel@tonic-gate oldval = *sp;
1390Sstevel@tonic-gate newval = oldval & ~bits;
1400Sstevel@tonic-gate } while (cas64(sp, oldval, newval) != oldval);
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate return (oldval);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate * Atomically shift a 32-bit word left, returning
1470Sstevel@tonic-gate * the value it had *before* the shift was applied
1480Sstevel@tonic-gate */
1490Sstevel@tonic-gate uint32_t
bge_atomic_shl32(uint32_t * sp,uint_t count)1500Sstevel@tonic-gate bge_atomic_shl32(uint32_t *sp, uint_t count)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate uint32_t oldval;
1530Sstevel@tonic-gate uint32_t newval;
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate /* ATOMICALLY */
1560Sstevel@tonic-gate do {
1570Sstevel@tonic-gate oldval = *sp;
1580Sstevel@tonic-gate newval = oldval << count;
1590Sstevel@tonic-gate } while (cas32(sp, oldval, newval) != oldval);
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate return (oldval);
1620Sstevel@tonic-gate }
163