17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 562387023Sdduvall * Common Development and Distribution License (the "License"). 662387023Sdduvall * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 2162387023Sdduvall 227c478bd9Sstevel@tonic-gate /* 2362387023Sdduvall * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 27f724721bSzh199473 #include "bge_impl.h" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * Atomically decrement a counter, but only if it will remain 317c478bd9Sstevel@tonic-gate * strictly positive (greater than zero) afterwards. We return 327c478bd9Sstevel@tonic-gate * the decremented value if so, otherwise zero (in which case 337c478bd9Sstevel@tonic-gate * the counter is unchanged). 347c478bd9Sstevel@tonic-gate * 357c478bd9Sstevel@tonic-gate * This is used for keeping track of available resources such 367c478bd9Sstevel@tonic-gate * as transmit ring slots ... 377c478bd9Sstevel@tonic-gate */ 387c478bd9Sstevel@tonic-gate uint64_t 397c478bd9Sstevel@tonic-gate bge_atomic_reserve(uint64_t *count_p, uint64_t n) 407c478bd9Sstevel@tonic-gate { 417c478bd9Sstevel@tonic-gate uint64_t oldval; 427c478bd9Sstevel@tonic-gate uint64_t newval; 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate /* ATOMICALLY */ 457c478bd9Sstevel@tonic-gate do { 467c478bd9Sstevel@tonic-gate oldval = *count_p; 477c478bd9Sstevel@tonic-gate newval = oldval - n; 487c478bd9Sstevel@tonic-gate if (oldval <= n) 497c478bd9Sstevel@tonic-gate return (0); /* no resources left */ 50*75d94465SJosef 'Jeff' Sipek } while (atomic_cas_64(count_p, oldval, newval) != oldval); 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate return (newval); 537c478bd9Sstevel@tonic-gate } 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate /* 567c478bd9Sstevel@tonic-gate * Atomically increment a counter 577c478bd9Sstevel@tonic-gate */ 587c478bd9Sstevel@tonic-gate void 597c478bd9Sstevel@tonic-gate bge_atomic_renounce(uint64_t *count_p, uint64_t n) 607c478bd9Sstevel@tonic-gate { 617c478bd9Sstevel@tonic-gate uint64_t oldval; 627c478bd9Sstevel@tonic-gate uint64_t newval; 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate /* ATOMICALLY */ 657c478bd9Sstevel@tonic-gate do { 667c478bd9Sstevel@tonic-gate oldval = *count_p; 677c478bd9Sstevel@tonic-gate newval = oldval + n; 68*75d94465SJosef 'Jeff' Sipek } while (atomic_cas_64(count_p, oldval, newval) != oldval); 697c478bd9Sstevel@tonic-gate } 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate /* 727c478bd9Sstevel@tonic-gate * Atomically claim a slot in a descriptor ring 737c478bd9Sstevel@tonic-gate */ 747c478bd9Sstevel@tonic-gate uint64_t 757c478bd9Sstevel@tonic-gate bge_atomic_claim(uint64_t *count_p, uint64_t limit) 767c478bd9Sstevel@tonic-gate { 777c478bd9Sstevel@tonic-gate uint64_t oldval; 787c478bd9Sstevel@tonic-gate uint64_t newval; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate /* ATOMICALLY */ 817c478bd9Sstevel@tonic-gate do { 827c478bd9Sstevel@tonic-gate oldval = *count_p; 837c478bd9Sstevel@tonic-gate newval = NEXT(oldval, limit); 84*75d94465SJosef 'Jeff' Sipek } while (atomic_cas_64(count_p, oldval, newval) != oldval); 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate return (oldval); 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate /* 90931dca7dSgs150176 * Atomically NEXT a 64-bit integer, returning the 91931dca7dSgs150176 * value it had *before* the NEXT was applied 92931dca7dSgs150176 */ 93931dca7dSgs150176 uint64_t 94931dca7dSgs150176 bge_atomic_next(uint64_t *sp, uint64_t limit) 95931dca7dSgs150176 { 96931dca7dSgs150176 uint64_t oldval; 97931dca7dSgs150176 uint64_t newval; 98931dca7dSgs150176 99931dca7dSgs150176 /* ATOMICALLY */ 100931dca7dSgs150176 do { 101931dca7dSgs150176 oldval = *sp; 102931dca7dSgs150176 newval = NEXT(oldval, limit); 103*75d94465SJosef 'Jeff' Sipek } while (atomic_cas_64(sp, oldval, newval) != oldval); 104931dca7dSgs150176 105931dca7dSgs150176 return (oldval); 106931dca7dSgs150176 } 107931dca7dSgs150176 108931dca7dSgs150176 /* 109931dca7dSgs150176 * Atomically decrement a counter 110931dca7dSgs150176 */ 111931dca7dSgs150176 void 112931dca7dSgs150176 bge_atomic_sub64(uint64_t *count_p, uint64_t n) 113931dca7dSgs150176 { 114931dca7dSgs150176 uint64_t oldval; 115931dca7dSgs150176 uint64_t newval; 116931dca7dSgs150176 117931dca7dSgs150176 /* ATOMICALLY */ 118931dca7dSgs150176 do { 119931dca7dSgs150176 oldval = *count_p; 120931dca7dSgs150176 newval = oldval - n; 121*75d94465SJosef 'Jeff' Sipek } while (atomic_cas_64(count_p, oldval, newval) != oldval); 122931dca7dSgs150176 } 123931dca7dSgs150176 124931dca7dSgs150176 /* 1257c478bd9Sstevel@tonic-gate * Atomically clear bits in a 64-bit word, returning 1267c478bd9Sstevel@tonic-gate * the value it had *before* the bits were cleared. 1277c478bd9Sstevel@tonic-gate */ 1287c478bd9Sstevel@tonic-gate uint64_t 1297c478bd9Sstevel@tonic-gate bge_atomic_clr64(uint64_t *sp, uint64_t bits) 1307c478bd9Sstevel@tonic-gate { 1317c478bd9Sstevel@tonic-gate uint64_t oldval; 1327c478bd9Sstevel@tonic-gate uint64_t newval; 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate /* ATOMICALLY */ 1357c478bd9Sstevel@tonic-gate do { 1367c478bd9Sstevel@tonic-gate oldval = *sp; 1377c478bd9Sstevel@tonic-gate newval = oldval & ~bits; 138*75d94465SJosef 'Jeff' Sipek } while (atomic_cas_64(sp, oldval, newval) != oldval); 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate return (oldval); 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate /* 1447c478bd9Sstevel@tonic-gate * Atomically shift a 32-bit word left, returning 1457c478bd9Sstevel@tonic-gate * the value it had *before* the shift was applied 1467c478bd9Sstevel@tonic-gate */ 1477c478bd9Sstevel@tonic-gate uint32_t 1487c478bd9Sstevel@tonic-gate bge_atomic_shl32(uint32_t *sp, uint_t count) 1497c478bd9Sstevel@tonic-gate { 1507c478bd9Sstevel@tonic-gate uint32_t oldval; 1517c478bd9Sstevel@tonic-gate uint32_t newval; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate /* ATOMICALLY */ 1547c478bd9Sstevel@tonic-gate do { 1557c478bd9Sstevel@tonic-gate oldval = *sp; 1567c478bd9Sstevel@tonic-gate newval = oldval << count; 157*75d94465SJosef 'Jeff' Sipek } while (atomic_cas_32(sp, oldval, newval) != oldval); 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate return (oldval); 1607c478bd9Sstevel@tonic-gate } 161