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 29*2675Szh199473 #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 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 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 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 /* 920Sstevel@tonic-gate * Atomically clear bits in a 64-bit word, returning 930Sstevel@tonic-gate * the value it had *before* the bits were cleared. 940Sstevel@tonic-gate */ 950Sstevel@tonic-gate uint64_t 960Sstevel@tonic-gate bge_atomic_clr64(uint64_t *sp, uint64_t bits) 970Sstevel@tonic-gate { 980Sstevel@tonic-gate uint64_t oldval; 990Sstevel@tonic-gate uint64_t newval; 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate /* ATOMICALLY */ 1020Sstevel@tonic-gate do { 1030Sstevel@tonic-gate oldval = *sp; 1040Sstevel@tonic-gate newval = oldval & ~bits; 1050Sstevel@tonic-gate } while (cas64(sp, oldval, newval) != oldval); 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate return (oldval); 1080Sstevel@tonic-gate } 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate /* 1110Sstevel@tonic-gate * Atomically shift a 32-bit word left, returning 1120Sstevel@tonic-gate * the value it had *before* the shift was applied 1130Sstevel@tonic-gate */ 1140Sstevel@tonic-gate uint32_t 1150Sstevel@tonic-gate bge_atomic_shl32(uint32_t *sp, uint_t count) 1160Sstevel@tonic-gate { 1170Sstevel@tonic-gate uint32_t oldval; 1180Sstevel@tonic-gate uint32_t newval; 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate /* ATOMICALLY */ 1210Sstevel@tonic-gate do { 1220Sstevel@tonic-gate oldval = *sp; 1230Sstevel@tonic-gate newval = oldval << count; 1240Sstevel@tonic-gate } while (cas32(sp, oldval, newval) != oldval); 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate return (oldval); 1270Sstevel@tonic-gate } 128