Home | History | Annotate | Line # | Download | only in acpica
      1 /*	$NetBSD: acpi_func.h,v 1.7 2023/05/10 01:23:27 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Michael Smith
      5  * Copyright (c) 2000 BSDi
      6  * Copyright (c) 2007-2009 Jung-uk Kim <jkim (at) 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 #ifndef _SYS_DEV_ACPI_ACPICA_ACPI_FUNC_H
     32 #define _SYS_DEV_ACPI_ACPICA_ACPI_FUNC_H
     33 
     34 #include <machine/cpufunc.h>
     35 
     36 #include <sys/atomic.h>
     37 
     38 #define GL_ACQUIRED	(-1)
     39 #define GL_BUSY		0
     40 #define GL_BIT_PENDING	1
     41 #define GL_BIT_OWNED	2
     42 #define GL_BIT_MASK	(GL_BIT_PENDING | GL_BIT_OWNED)
     43 
     44 #define ACPI_ACQUIRE_GLOBAL_LOCK(GLptr, Acq) 				\
     45 do { 									\
     46 	(Acq) = acpi_acquire_global_lock(&((GLptr)->GlobalLock));	\
     47 } while (0)
     48 
     49 #define ACPI_RELEASE_GLOBAL_LOCK(GLptr, Acq) 				\
     50 do {									\
     51 	(Acq) = acpi_release_global_lock(&((GLptr)->GlobalLock));	\
     52 } while (0)
     53 
     54 static __inline int
     55 acpi_acquire_global_lock(uint32_t *lock)
     56 {
     57 	uint32_t new, old, val;
     58 
     59 	do {
     60 		old = *lock;
     61 		new = ((old & ~GL_BIT_MASK) | GL_BIT_OWNED) |
     62 		    ((old >> 1) & GL_BIT_PENDING);
     63 		val = atomic_cas_32(lock, old, new);
     64 	} while (__predict_false(val != old));
     65 	membar_acquire();
     66 
     67 	return ((new < GL_BIT_MASK) ? GL_ACQUIRED : GL_BUSY);
     68 }
     69 
     70 static __inline int
     71 acpi_release_global_lock(uint32_t *lock)
     72 {
     73 	uint32_t new, old, val;
     74 
     75 	membar_release();
     76 	do {
     77 		old = *lock;
     78 		new = old & ~GL_BIT_MASK;
     79 		val = atomic_cas_32(lock, old, new);
     80 	} while (__predict_false(val != old));
     81 
     82 	return old & GL_BIT_PENDING;
     83 }
     84 
     85 /*
     86  * XXX: Should be in a MD header.
     87  */
     88 #if defined(__ia64__)
     89 #define	ACPI_FLUSH_CPU_CACHE()	/* XXX: ia64_fc()? */
     90 #elif defined(__aarch64__)
     91 #define	ACPI_FLUSH_CPU_CACHE()	cpu_dcache_wbinv_all()
     92 #else
     93 #define	ACPI_FLUSH_CPU_CACHE()	wbinvd()
     94 #endif
     95 
     96 #endif /* !_SYS_DEV_ACPI_ACPICA_ACPI_FUNC_H */
     97