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