1 /* $NetBSD: acpi_func.h,v 1.2 2010/03/08 12:35:08 jruoho Exp $ */ 2 3 /*- 4 * Copyright (c) 2000 Michael Smith 5 * Copyright (c) 2000 BSDi 6 * Copyright (c) 2007-2009 Jung-uk Kim <jkim@FreeBSD.org> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <machine/cpufunc.h> 32 33 #include <sys/atomic.h> 34 35 #define GL_ACQUIRED (-1) 36 #define GL_BUSY 0 37 #define GL_BIT_PENDING 1 38 #define GL_BIT_OWNED 2 39 #define GL_BIT_MASK (GL_BIT_PENDING | GL_BIT_OWNED) 40 41 #define ACPI_ACQUIRE_GLOBAL_LOCK(GLptr, Acq) \ 42 do { \ 43 (Acq) = acpi_acquire_global_lock(&((GLptr)->GlobalLock)); \ 44 } while (0) 45 46 #define ACPI_RELEASE_GLOBAL_LOCK(GLptr, Acq) \ 47 do { \ 48 (Acq) = acpi_release_global_lock(&((GLptr)->GlobalLock)); \ 49 } while (0) 50 51 static inline int 52 acpi_acquire_global_lock(uint32_t *lock) 53 { 54 uint32_t new, old, val; 55 56 do { 57 old = *lock; 58 new = ((old & ~GL_BIT_MASK) | GL_BIT_OWNED) | 59 ((old >> 1) & GL_BIT_PENDING); 60 val = atomic_cas_32(lock, old, new); 61 } while (__predict_false(val != old)); 62 63 return ((new < GL_BIT_MASK) ? GL_ACQUIRED : GL_BUSY); 64 } 65 66 static inline int 67 acpi_release_global_lock(uint32_t *lock) 68 { 69 uint32_t new, old, val; 70 71 do { 72 old = *lock; 73 new = old & ~GL_BIT_MASK; 74 val = atomic_cas_32(lock, old, new); 75 } while (__predict_false(val != old)); 76 77 return old & GL_BIT_PENDING; 78 } 79 80 #define ACPI_FLUSH_CPU_CACHE() wbinvd() 81