1/*	$NetBSD: acpi_func.h,v 1.3 2010/07/24 09:35:36 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#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) 				\
45do { 									\
46	(Acq) = acpi_acquire_global_lock(&((GLptr)->GlobalLock));	\
47} while (0)
48
49#define ACPI_RELEASE_GLOBAL_LOCK(GLptr, Acq) 				\
50do {									\
51	(Acq) = acpi_release_global_lock(&((GLptr)->GlobalLock));	\
52} while (0)
53
54static inline int
55acpi_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
66	return ((new < GL_BIT_MASK) ? GL_ACQUIRED : GL_BUSY);
67}
68
69static inline int
70acpi_release_global_lock(uint32_t *lock)
71{
72	uint32_t new, old, val;
73
74	do {
75		old = *lock;
76		new = old & ~GL_BIT_MASK;
77		val = atomic_cas_32(lock, old, new);
78	} while (__predict_false(val != old));
79
80	return old & GL_BIT_PENDING;
81}
82
83/*
84 * XXX: Should be in a MD header.
85 */
86#ifndef __ia64__
87#define	ACPI_FLUSH_CPU_CACHE()	wbinvd()
88#else
89#define ACPI_FLUSH_CPU_CACHE()	/* XXX: ia64_fc()? */
90#endif
91
92#endif /* !_SYS_DEV_ACPI_ACPICA_ACPI_FUNC_H */
93