1 /* MECHANICALLY GENERATED, DO NOT EDIT!!! */
7 * common.h: Common Linux kernel-isms.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; but version 2 of the License only due
12 * to code included from the Linux kernel.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * Copyright (c) 2006 Paul E. McKenney, IBM.
25 * Much code taken from the Linux kernel. For such code, the option
26 * to redistribute under later versions of GPL might not be available.
29 #include <urcu/arch.h>
31 #ifndef __always_inline
32 #define __always_inline inline
35 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
36 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
39 # define stringify_in_c(...) __VA_ARGS__
40 # define ASM_CONST(x) x
42 /* This version of stringify will deal with commas... */
43 # define __stringify_in_c(...) #__VA_ARGS__
44 # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
45 # define __ASM_CONST(x) x##UL
46 # define ASM_CONST(x) __ASM_CONST(x)
51 * arch-ppc64.h: Expose PowerPC atomic instructions.
53 * This program is free software; you can redistribute it and/or modify
54 * it under the terms of the GNU General Public License as published by
55 * the Free Software Foundation; but version 2 of the License only due
56 * to code included from the Linux kernel.
58 * This program is distributed in the hope that it will be useful,
59 * but WITHOUT ANY WARRANTY; without even the implied warranty of
60 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
61 * GNU General Public License for more details.
63 * You should have received a copy of the GNU General Public License
64 * along with this program; if not, write to the Free Software
65 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
67 * Copyright (c) 2006 Paul E. McKenney, IBM.
69 * Much code taken from the Linux kernel. For such code, the option
70 * to redistribute under later versions of GPL might not be available.
79 /*#define CACHE_LINE_SIZE 128 */
80 #define ____cacheline_internodealigned_in_smp \
81 __attribute__((__aligned__(1 << 7)))
83 #if 0 /* duplicate with arch_atomic.h */
86 * Atomic data structure, initialization, and access.
89 typedef struct { volatile int counter
; } atomic_t
;
91 #define ATOMIC_INIT(i) { (i) }
93 #define atomic_read(v) ((v)->counter)
94 #define atomic_set(v, i) (((v)->counter) = (i))
100 #define LWSYNC lwsync
101 #define PPC405_ERR77(ra,rb)
103 # define LWSYNC_ON_SMP stringify_in_c(LWSYNC) "\n"
104 # define ISYNC_ON_SMP "\n\tisync\n"
106 # define LWSYNC_ON_SMP
107 # define ISYNC_ON_SMP
113 * Changes the memory location '*ptr' to be val and returns
114 * the previous value stored there.
116 static __always_inline
unsigned long
117 __xchg_u32(volatile void *p
, unsigned long val
)
121 __asm__
__volatile__(
123 "1: lwarx %0,0,%2 \n"
128 : "=&r" (prev
), "+m" (*(volatile unsigned int *)p
)
138 * Changes the memory location '*ptr' to be val and returns
139 * the previous value stored there.
141 static __always_inline
unsigned long
142 __xchg_u32_local(volatile void *p
, unsigned long val
)
146 __asm__
__volatile__(
147 "1: lwarx %0,0,%2 \n"
151 : "=&r" (prev
), "+m" (*(volatile unsigned int *)p
)
159 static __always_inline
unsigned long
160 __xchg_u64(volatile void *p
, unsigned long val
)
164 __asm__
__volatile__(
166 "1: ldarx %0,0,%2 \n"
171 : "=&r" (prev
), "+m" (*(volatile unsigned long *)p
)
178 static __always_inline
unsigned long
179 __xchg_u64_local(volatile void *p
, unsigned long val
)
183 __asm__
__volatile__(
184 "1: ldarx %0,0,%2 \n"
188 : "=&r" (prev
), "+m" (*(volatile unsigned long *)p
)
197 * This function doesn't exist, so you'll get a linker error
198 * if something tries to do an invalid xchg().
200 extern void __xchg_called_with_bad_pointer(void);
202 static __always_inline
unsigned long
203 __xchg(volatile void *ptr
, unsigned long x
, unsigned int size
)
207 return __xchg_u32(ptr
, x
);
210 return __xchg_u64(ptr
, x
);
213 __xchg_called_with_bad_pointer();
217 static __always_inline
unsigned long
218 __xchg_local(volatile void *ptr
, unsigned long x
, unsigned int size
)
222 return __xchg_u32_local(ptr
, x
);
225 return __xchg_u64_local(ptr
, x
);
228 __xchg_called_with_bad_pointer();
231 #define xchg(ptr,x) \
233 __typeof__(*(ptr)) _x_ = (x); \
234 (__typeof__(*(ptr))) __xchg((ptr), (unsigned long)_x_, sizeof(*(ptr))); \
237 #define xchg_local(ptr,x) \
239 __typeof__(*(ptr)) _x_ = (x); \
240 (__typeof__(*(ptr))) __xchg_local((ptr), \
241 (unsigned long)_x_, sizeof(*(ptr))); \
245 * Compare and exchange - if *p == old, set it to new,
246 * and return the old value of *p.
248 #define __HAVE_ARCH_CMPXCHG 1
250 static __always_inline
unsigned long
251 __cmpxchg_u32(volatile unsigned int *p
, unsigned long old
, unsigned long new)
255 __asm__
__volatile__ (
257 "1: lwarx %0,0,%2 # __cmpxchg_u32\n\
266 : "=&r" (prev
), "+m" (*p
)
267 : "r" (p
), "r" (old
), "r" (new)
273 static __always_inline
unsigned long
274 __cmpxchg_u32_local(volatile unsigned int *p
, unsigned long old
,
279 __asm__
__volatile__ (
280 "1: lwarx %0,0,%2 # __cmpxchg_u32\n\
288 : "=&r" (prev
), "+m" (*p
)
289 : "r" (p
), "r" (old
), "r" (new)
296 static __always_inline
unsigned long
297 __cmpxchg_u64(volatile unsigned long *p
, unsigned long old
, unsigned long new)
301 __asm__
__volatile__ (
303 "1: ldarx %0,0,%2 # __cmpxchg_u64\n\
311 : "=&r" (prev
), "+m" (*p
)
312 : "r" (p
), "r" (old
), "r" (new)
318 static __always_inline
unsigned long
319 __cmpxchg_u64_local(volatile unsigned long *p
, unsigned long old
,
324 __asm__
__volatile__ (
325 "1: ldarx %0,0,%2 # __cmpxchg_u64\n\
332 : "=&r" (prev
), "+m" (*p
)
333 : "r" (p
), "r" (old
), "r" (new)
340 /* This function doesn't exist, so you'll get a linker error
341 if something tries to do an invalid cmpxchg(). */
342 extern void __cmpxchg_called_with_bad_pointer(void);
344 static __always_inline
unsigned long
345 __cmpxchg(volatile void *ptr
, unsigned long old
, unsigned long new,
350 return __cmpxchg_u32(ptr
, old
, new);
353 return __cmpxchg_u64(ptr
, old
, new);
356 __cmpxchg_called_with_bad_pointer();
360 static __always_inline
unsigned long
361 __cmpxchg_local(volatile void *ptr
, unsigned long old
, unsigned long new,
366 return __cmpxchg_u32_local(ptr
, old
, new);
369 return __cmpxchg_u64_local(ptr
, old
, new);
372 __cmpxchg_called_with_bad_pointer();
376 #define cmpxchg(ptr, o, n) \
378 __typeof__(*(ptr)) _o_ = (o); \
379 __typeof__(*(ptr)) _n_ = (n); \
380 (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
381 (unsigned long)_n_, sizeof(*(ptr))); \
385 #define cmpxchg_local(ptr, o, n) \
387 __typeof__(*(ptr)) _o_ = (o); \
388 __typeof__(*(ptr)) _n_ = (n); \
389 (__typeof__(*(ptr))) __cmpxchg_local((ptr), (unsigned long)_o_, \
390 (unsigned long)_n_, sizeof(*(ptr))); \
395 * We handle most unaligned accesses in hardware. On the other hand
396 * unaligned DMA can be very expensive on some ppc64 IO chips (it does
397 * powers of 2 writes until it reaches sufficient alignment).
399 * Based on this we disable the IP header alignment in network drivers.
400 * We also modify NET_SKB_PAD to be a cacheline in size, thus maintaining
401 * cacheline alignment of buffers.
403 #define NET_IP_ALIGN 0
404 #define NET_SKB_PAD L1_CACHE_BYTES
406 #define cmpxchg64(ptr, o, n) \
408 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
409 cmpxchg((ptr), (o), (n)); \
411 #define cmpxchg64_local(ptr, o, n) \
413 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
414 cmpxchg_local((ptr), (o), (n)); \
418 #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
419 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
422 * atomic_add - add integer to atomic variable
423 * @i: integer value to add
424 * @v: pointer of type atomic_t
426 * Atomically adds @a to @v.
428 static __inline__
void atomic_add(int a
, atomic_t
*v
)
432 __asm__
__volatile__(
433 "1: lwarx %0,0,%3 # atomic_add\n\
437 : "=&r" (t
), "+m" (v
->counter
)
438 : "r" (a
), "r" (&v
->counter
)
443 * atomic_sub - subtract the atomic variable
444 * @i: integer value to subtract
445 * @v: pointer of type atomic_t
447 * Atomically subtracts @a from @v.
449 static __inline__
void atomic_sub(int a
, atomic_t
*v
)
453 __asm__
__volatile__(
454 "1: lwarx %0,0,%3 # atomic_sub \n\
458 : "=&r" (t
), "+m" (v
->counter
)
459 : "r" (a
), "r" (&v
->counter
)
463 static __inline__
atomic_sub_return(int a
, atomic_t
*v
)
467 __asm__
__volatile__(
469 1: lwarx %0,0,%2 # atomic_sub_return\n\
475 : "r" (a
), "r" (&v
->counter
)
482 * atomic_sub_and_test - subtract value from variable and test result
483 * @i: integer value to subtract
484 * @v: pointer of type atomic_t
486 * Atomically subtracts @i from @v and returns
487 * true if the result is zero, or false for all
490 static __inline__
int atomic_sub_and_test(int a
, atomic_t
*v
)
492 return atomic_sub_return(a
, v
) == 0;
496 * atomic_inc - increment atomic variable
497 * @v: pointer of type atomic_t
499 * Atomically increments @v by 1.
501 static __inline__
void atomic_inc(atomic_t
*v
)
507 * atomic_dec - decrement atomic variable
508 * @v: pointer of type atomic_t
510 * Atomically decrements @v by 1.
512 static __inline__
void atomic_dec(atomic_t
*v
)
518 * atomic_dec_and_test - decrement and test
519 * @v: pointer of type atomic_t
521 * Atomically decrements @v by 1 and
522 * returns true if the result is 0, or false for all other
525 static __inline__
int atomic_dec_and_test(atomic_t
*v
)
527 return atomic_sub_and_test(1, v
);
531 * atomic_inc_and_test - increment and test
532 * @v: pointer of type atomic_t
534 * Atomically increments @v by 1
535 * and returns true if the result is zero, or false for all
538 static __inline__
int atomic_inc_and_test(atomic_t
*v
)
540 return atomic_inc_return(v
);
544 * atomic_add_return - add and return
545 * @v: pointer of type atomic_t
546 * @i: integer value to add
548 * Atomically adds @i to @v and returns @i + @v
550 static __inline__
int atomic_add_return(int a
, atomic_t
*v
)
554 __asm__
__volatile__(
556 1: lwarx %0,0,%2 # atomic_add_return \n\
562 : "r" (a
), "r" (&v
->counter
)
569 * atomic_add_negative - add and test if negative
570 * @v: pointer of type atomic_t
571 * @i: integer value to add
573 * Atomically adds @i to @v and returns true
574 * if the result is negative, or false when
575 * result is greater than or equal to zero.
577 static __inline__
int atomic_add_negative(int a
, atomic_t
*v
)
579 return atomic_add_return(a
, v
) < 0;
583 * atomic_add_unless - add unless the number is a given value
584 * @v: pointer of type atomic_t
585 * @a: the amount to add to v...
586 * @u: ...unless v is equal to u.
588 * Atomically adds @a to @v, so long as it was not @u.
589 * Returns non-zero if @v was not @u, and zero otherwise.
591 static __inline__
int atomic_add_unless(atomic_t
*v
, int a
, int u
)
595 __asm__
__volatile__(
597 1: lwarx %0,0,%1 # atomic_add_unless\n\
607 : "r" (&v
->counter
), "r" (a
), "r" (u
)
613 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
615 #define atomic_inc_return(v) (atomic_add_return(1,v))
616 #define atomic_dec_return(v) (atomic_sub_return(1,v))
618 /* Atomic operations are already serializing on x86 */
619 #define smp_mb__before_atomic_dec() smp_mb()
620 #define smp_mb__after_atomic_dec() smp_mb()
621 #define smp_mb__before_atomic_inc() smp_mb()
622 #define smp_mb__after_atomic_inc() smp_mb()
624 #endif //0 /* duplicate with arch_atomic.h */
627 * api_pthreads.h: API mapping to pthreads environment.
629 * This program is free software; you can redistribute it and/or modify
630 * it under the terms of the GNU General Public License as published by
631 * the Free Software Foundation; either version 2 of the License, or
632 * (at your option) any later version. However, please note that much
633 * of the code in this file derives from the Linux kernel, and that such
634 * code may not be available except under GPLv2.
636 * This program is distributed in the hope that it will be useful,
637 * but WITHOUT ANY WARRANTY; without even the implied warranty of
638 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
639 * GNU General Public License for more details.
641 * You should have received a copy of the GNU General Public License
642 * along with this program; if not, write to the Free Software
643 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
645 * Copyright (c) 2006 Paul E. McKenney, IBM.
652 #include <sys/types.h>
656 #include <sys/param.h>
657 /* #include "atomic.h" */
660 * Default machine parameters.
663 #ifndef CACHE_LINE_SIZE
664 /* #define CACHE_LINE_SIZE 128 */
665 #endif /* #ifndef CACHE_LINE_SIZE */
668 * Exclusive locking primitives.
671 typedef pthread_mutex_t spinlock_t
;
673 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
674 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
676 static void spin_lock_init(spinlock_t
*sp
)
678 if (pthread_mutex_init(sp
, NULL
) != 0) {
679 perror("spin_lock_init:pthread_mutex_init");
684 static void spin_lock(spinlock_t
*sp
)
686 if (pthread_mutex_lock(sp
) != 0) {
687 perror("spin_lock:pthread_mutex_lock");
692 static void spin_unlock(spinlock_t
*sp
)
694 if (pthread_mutex_unlock(sp
) != 0) {
695 perror("spin_unlock:pthread_mutex_unlock");
700 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
701 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
704 * Thread creation/destruction primitives.
707 typedef pthread_t thread_id_t
;
709 #define NR_THREADS 128
711 #define __THREAD_ID_MAP_EMPTY 0
712 #define __THREAD_ID_MAP_WAITING 1
713 thread_id_t __thread_id_map
[NR_THREADS
];
714 spinlock_t __thread_id_map_mutex
;
716 #define for_each_thread(t) \
717 for (t = 0; t < NR_THREADS; t++)
719 #define for_each_running_thread(t) \
720 for (t = 0; t < NR_THREADS; t++) \
721 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
722 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
724 #define for_each_tid(t, tid) \
725 for (t = 0; t < NR_THREADS; t++) \
726 if ((((tid) = __thread_id_map[t]) != __THREAD_ID_MAP_EMPTY) && \
727 ((tid) != __THREAD_ID_MAP_WAITING))
729 pthread_key_t thread_id_key
;
731 static int __smp_thread_id(void)
734 thread_id_t tid
= pthread_self();
736 for (i
= 0; i
< NR_THREADS
; i
++) {
737 if (__thread_id_map
[i
] == tid
) {
738 long v
= i
+ 1; /* must be non-NULL. */
740 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
741 perror("pthread_setspecific");
747 spin_lock(&__thread_id_map_mutex
);
748 for (i
= 0; i
< NR_THREADS
; i
++) {
749 if (__thread_id_map
[i
] == tid
)
750 spin_unlock(&__thread_id_map_mutex
);
753 spin_unlock(&__thread_id_map_mutex
);
754 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
759 static int smp_thread_id(void)
763 id
= pthread_getspecific(thread_id_key
);
765 return __smp_thread_id();
766 return (long)(id
- 1);
769 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
774 spin_lock(&__thread_id_map_mutex
);
775 for (i
= 0; i
< NR_THREADS
; i
++) {
776 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
779 if (i
>= NR_THREADS
) {
780 spin_unlock(&__thread_id_map_mutex
);
781 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
784 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
785 spin_unlock(&__thread_id_map_mutex
);
786 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
787 perror("create_thread:pthread_create");
790 __thread_id_map
[i
] = tid
;
794 static void *wait_thread(thread_id_t tid
)
799 for (i
= 0; i
< NR_THREADS
; i
++) {
800 if (__thread_id_map
[i
] == tid
)
803 if (i
>= NR_THREADS
){
804 fprintf(stderr
, "wait_thread: bad tid = %d(%#x)\n",
808 if (pthread_join(tid
, &vp
) != 0) {
809 perror("wait_thread:pthread_join");
812 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
816 static void wait_all_threads(void)
821 for (i
= 1; i
< NR_THREADS
; i
++) {
822 tid
= __thread_id_map
[i
];
823 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
824 tid
!= __THREAD_ID_MAP_WAITING
)
825 (void)wait_thread(tid
);
829 static void run_on(int cpu
)
835 sched_setaffinity(0, sizeof(mask
), &mask
);
839 * timekeeping -- very crude -- should use MONOTONIC...
842 long long get_microseconds(void)
846 if (gettimeofday(&tv
, NULL
) != 0)
848 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
852 * Per-thread variables.
855 #define DEFINE_PER_THREAD(type, name) \
858 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
859 } __per_thread_##name[NR_THREADS];
860 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
862 #define per_thread(name, thread) __per_thread_##name[thread].v
863 #define __get_thread_var(name) per_thread(name, smp_thread_id())
865 #define init_per_thread(name, v) \
868 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
869 per_thread(name, __i_p_t_i) = v; \
873 * CPU traversal primitives.
878 #endif /* #ifndef NR_CPUS */
880 #define for_each_possible_cpu(cpu) \
881 for (cpu = 0; cpu < NR_CPUS; cpu++)
882 #define for_each_online_cpu(cpu) \
883 for (cpu = 0; cpu < NR_CPUS; cpu++)
889 #define DEFINE_PER_CPU(type, name) \
892 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
893 } __per_cpu_##name[NR_CPUS]
894 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
896 DEFINE_PER_THREAD(int, smp_processor_id
);
898 #define per_cpu(name, thread) __per_cpu_##name[thread].v
899 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
901 #define init_per_cpu(name, v) \
904 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
905 per_cpu(name, __i_p_c_i) = v; \
909 * CPU state checking (crowbarred).
912 #define idle_cpu(cpu) 0
913 #define in_softirq() 1
914 #define hardirq_count() 0
915 #define PREEMPT_SHIFT 0
916 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
917 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
918 #define PREEMPT_BITS 8
919 #define SOFTIRQ_BITS 8
925 struct notifier_block
{
926 int (*notifier_call
)(struct notifier_block
*, unsigned long, void *);
927 struct notifier_block
*next
;
931 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
932 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
933 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
934 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
935 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
936 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
937 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
938 * not handling interrupts, soon dead */
939 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
942 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
943 * operation in progress
945 #define CPU_TASKS_FROZEN 0x0010
947 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
948 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
949 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
950 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
951 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
952 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
953 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
955 /* Hibernation and suspend events */
956 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
957 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
958 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
959 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
960 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
961 #define PM_POST_RESTORE 0x0006 /* Restore failed */
963 #define NOTIFY_DONE 0x0000 /* Don't care */
964 #define NOTIFY_OK 0x0001 /* Suits me */
965 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
966 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
967 /* Bad/Veto action */
969 * Clean way to return from the notifier and stop further calls.
971 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
977 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
980 * Initialization -- Must be called before calling any primitives.
983 static void smp_init(void)
987 spin_lock_init(&__thread_id_map_mutex
);
988 __thread_id_map
[0] = pthread_self();
989 for (i
= 1; i
< NR_THREADS
; i
++)
990 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
991 init_per_thread(smp_processor_id
, 0);
992 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
993 perror("pthread_key_create");
998 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
1000 #ifndef _LINUX_LIST_H
1001 #define _LINUX_LIST_H
1003 #define LIST_POISON1 ((void *) 0x00100100)
1004 #define LIST_POISON2 ((void *) 0x00200200)
1009 * Simple doubly linked list implementation.
1011 * Some of the internal functions ("__xxx") are useful when
1012 * manipulating whole lists rather than single entries, as
1013 * sometimes we already know the next/prev entries and we can
1014 * generate better code by using them directly rather than
1015 * using the generic single-entry routines.
1019 struct list_head
*next
, *prev
;
1022 #define LIST_HEAD_INIT(name) { &(name), &(name) }
1024 #define LIST_HEAD(name) \
1025 struct list_head name = LIST_HEAD_INIT(name)
1027 static inline void INIT_LIST_HEAD(struct list_head
*list
)
1034 * Insert a new entry between two known consecutive entries.
1036 * This is only for internal list manipulation where we know
1037 * the prev/next entries already!
1039 #ifndef CONFIG_DEBUG_LIST
1040 static inline void __list_add(struct list_head
*new,
1041 struct list_head
*prev
,
1042 struct list_head
*next
)
1050 extern void __list_add(struct list_head
*new,
1051 struct list_head
*prev
,
1052 struct list_head
*next
);
1056 * list_add - add a new entry
1057 * @new: new entry to be added
1058 * @head: list head to add it after
1060 * Insert a new entry after the specified head.
1061 * This is good for implementing stacks.
1063 static inline void list_add(struct list_head
*new, struct list_head
*head
)
1065 __list_add(new, head
, head
->next
);
1070 * list_add_tail - add a new entry
1071 * @new: new entry to be added
1072 * @head: list head to add it before
1074 * Insert a new entry before the specified head.
1075 * This is useful for implementing queues.
1077 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
1079 __list_add(new, head
->prev
, head
);
1083 * Delete a list entry by making the prev/next entries
1084 * point to each other.
1086 * This is only for internal list manipulation where we know
1087 * the prev/next entries already!
1089 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
1096 * list_del - deletes entry from list.
1097 * @entry: the element to delete from the list.
1098 * Note: list_empty() on entry does not return true after this, the entry is
1099 * in an undefined state.
1101 #ifndef CONFIG_DEBUG_LIST
1102 static inline void list_del(struct list_head
*entry
)
1104 __list_del(entry
->prev
, entry
->next
);
1105 entry
->next
= LIST_POISON1
;
1106 entry
->prev
= LIST_POISON2
;
1109 extern void list_del(struct list_head
*entry
);
1113 * list_replace - replace old entry by new one
1114 * @old : the element to be replaced
1115 * @new : the new element to insert
1117 * If @old was empty, it will be overwritten.
1119 static inline void list_replace(struct list_head
*old
,
1120 struct list_head
*new)
1122 new->next
= old
->next
;
1123 new->next
->prev
= new;
1124 new->prev
= old
->prev
;
1125 new->prev
->next
= new;
1128 static inline void list_replace_init(struct list_head
*old
,
1129 struct list_head
*new)
1131 list_replace(old
, new);
1132 INIT_LIST_HEAD(old
);
1136 * list_del_init - deletes entry from list and reinitialize it.
1137 * @entry: the element to delete from the list.
1139 static inline void list_del_init(struct list_head
*entry
)
1141 __list_del(entry
->prev
, entry
->next
);
1142 INIT_LIST_HEAD(entry
);
1146 * list_move - delete from one list and add as another's head
1147 * @list: the entry to move
1148 * @head: the head that will precede our entry
1150 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
1152 __list_del(list
->prev
, list
->next
);
1153 list_add(list
, head
);
1157 * list_move_tail - delete from one list and add as another's tail
1158 * @list: the entry to move
1159 * @head: the head that will follow our entry
1161 static inline void list_move_tail(struct list_head
*list
,
1162 struct list_head
*head
)
1164 __list_del(list
->prev
, list
->next
);
1165 list_add_tail(list
, head
);
1169 * list_is_last - tests whether @list is the last entry in list @head
1170 * @list: the entry to test
1171 * @head: the head of the list
1173 static inline int list_is_last(const struct list_head
*list
,
1174 const struct list_head
*head
)
1176 return list
->next
== head
;
1180 * list_empty - tests whether a list is empty
1181 * @head: the list to test.
1183 static inline int list_empty(const struct list_head
*head
)
1185 return head
->next
== head
;
1189 * list_empty_careful - tests whether a list is empty and not being modified
1190 * @head: the list to test
1193 * tests whether a list is empty _and_ checks that no other CPU might be
1194 * in the process of modifying either member (next or prev)
1196 * NOTE: using list_empty_careful() without synchronization
1197 * can only be safe if the only activity that can happen
1198 * to the list entry is list_del_init(). Eg. it cannot be used
1199 * if another CPU could re-list_add() it.
1201 static inline int list_empty_careful(const struct list_head
*head
)
1203 struct list_head
*next
= head
->next
;
1204 return (next
== head
) && (next
== head
->prev
);
1208 * list_is_singular - tests whether a list has just one entry.
1209 * @head: the list to test.
1211 static inline int list_is_singular(const struct list_head
*head
)
1213 return !list_empty(head
) && (head
->next
== head
->prev
);
1216 static inline void __list_cut_position(struct list_head
*list
,
1217 struct list_head
*head
, struct list_head
*entry
)
1219 struct list_head
*new_first
= entry
->next
;
1220 list
->next
= head
->next
;
1221 list
->next
->prev
= list
;
1224 head
->next
= new_first
;
1225 new_first
->prev
= head
;
1229 * list_cut_position - cut a list into two
1230 * @list: a new list to add all removed entries
1231 * @head: a list with entries
1232 * @entry: an entry within head, could be the head itself
1233 * and if so we won't cut the list
1235 * This helper moves the initial part of @head, up to and
1236 * including @entry, from @head to @list. You should
1237 * pass on @entry an element you know is on @head. @list
1238 * should be an empty list or a list you do not care about
1242 static inline void list_cut_position(struct list_head
*list
,
1243 struct list_head
*head
, struct list_head
*entry
)
1245 if (list_empty(head
))
1247 if (list_is_singular(head
) &&
1248 (head
->next
!= entry
&& head
!= entry
))
1251 INIT_LIST_HEAD(list
);
1253 __list_cut_position(list
, head
, entry
);
1256 static inline void __list_splice(const struct list_head
*list
,
1257 struct list_head
*prev
,
1258 struct list_head
*next
)
1260 struct list_head
*first
= list
->next
;
1261 struct list_head
*last
= list
->prev
;
1271 * list_splice - join two lists, this is designed for stacks
1272 * @list: the new list to add.
1273 * @head: the place to add it in the first list.
1275 static inline void list_splice(const struct list_head
*list
,
1276 struct list_head
*head
)
1278 if (!list_empty(list
))
1279 __list_splice(list
, head
, head
->next
);
1283 * list_splice_tail - join two lists, each list being a queue
1284 * @list: the new list to add.
1285 * @head: the place to add it in the first list.
1287 static inline void list_splice_tail(struct list_head
*list
,
1288 struct list_head
*head
)
1290 if (!list_empty(list
))
1291 __list_splice(list
, head
->prev
, head
);
1295 * list_splice_init - join two lists and reinitialise the emptied list.
1296 * @list: the new list to add.
1297 * @head: the place to add it in the first list.
1299 * The list at @list is reinitialised
1301 static inline void list_splice_init(struct list_head
*list
,
1302 struct list_head
*head
)
1304 if (!list_empty(list
)) {
1305 __list_splice(list
, head
, head
->next
);
1306 INIT_LIST_HEAD(list
);
1311 * list_splice_tail_init - join two lists and reinitialise the emptied list
1312 * @list: the new list to add.
1313 * @head: the place to add it in the first list.
1315 * Each of the lists is a queue.
1316 * The list at @list is reinitialised
1318 static inline void list_splice_tail_init(struct list_head
*list
,
1319 struct list_head
*head
)
1321 if (!list_empty(list
)) {
1322 __list_splice(list
, head
->prev
, head
);
1323 INIT_LIST_HEAD(list
);
1328 * list_entry - get the struct for this entry
1329 * @ptr: the &struct list_head pointer.
1330 * @type: the type of the struct this is embedded in.
1331 * @member: the name of the list_struct within the struct.
1333 #define list_entry(ptr, type, member) \
1334 container_of(ptr, type, member)
1337 * list_first_entry - get the first element from a list
1338 * @ptr: the list head to take the element from.
1339 * @type: the type of the struct this is embedded in.
1340 * @member: the name of the list_struct within the struct.
1342 * Note, that list is expected to be not empty.
1344 #define list_first_entry(ptr, type, member) \
1345 list_entry((ptr)->next, type, member)
1348 * list_for_each - iterate over a list
1349 * @pos: the &struct list_head to use as a loop cursor.
1350 * @head: the head for your list.
1352 #define list_for_each(pos, head) \
1353 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
1357 * __list_for_each - iterate over a list
1358 * @pos: the &struct list_head to use as a loop cursor.
1359 * @head: the head for your list.
1361 * This variant differs from list_for_each() in that it's the
1362 * simplest possible list iteration code, no prefetching is done.
1363 * Use this for code that knows the list to be very short (empty
1364 * or 1 entry) most of the time.
1366 #define __list_for_each(pos, head) \
1367 for (pos = (head)->next; pos != (head); pos = pos->next)
1370 * list_for_each_prev - iterate over a list backwards
1371 * @pos: the &struct list_head to use as a loop cursor.
1372 * @head: the head for your list.
1374 #define list_for_each_prev(pos, head) \
1375 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
1379 * list_for_each_safe - iterate over a list safe against removal of list entry
1380 * @pos: the &struct list_head to use as a loop cursor.
1381 * @n: another &struct list_head to use as temporary storage
1382 * @head: the head for your list.
1384 #define list_for_each_safe(pos, n, head) \
1385 for (pos = (head)->next, n = pos->next; pos != (head); \
1386 pos = n, n = pos->next)
1389 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
1390 * @pos: the &struct list_head to use as a loop cursor.
1391 * @n: another &struct list_head to use as temporary storage
1392 * @head: the head for your list.
1394 #define list_for_each_prev_safe(pos, n, head) \
1395 for (pos = (head)->prev, n = pos->prev; \
1396 prefetch(pos->prev), pos != (head); \
1397 pos = n, n = pos->prev)
1400 * list_for_each_entry - iterate over list of given type
1401 * @pos: the type * to use as a loop cursor.
1402 * @head: the head for your list.
1403 * @member: the name of the list_struct within the struct.
1405 #define list_for_each_entry(pos, head, member) \
1406 for (pos = list_entry((head)->next, typeof(*pos), member); \
1407 prefetch(pos->member.next), &pos->member != (head); \
1408 pos = list_entry(pos->member.next, typeof(*pos), member))
1411 * list_for_each_entry_reverse - iterate backwards over list of given type.
1412 * @pos: the type * to use as a loop cursor.
1413 * @head: the head for your list.
1414 * @member: the name of the list_struct within the struct.
1416 #define list_for_each_entry_reverse(pos, head, member) \
1417 for (pos = list_entry((head)->prev, typeof(*pos), member); \
1418 prefetch(pos->member.prev), &pos->member != (head); \
1419 pos = list_entry(pos->member.prev, typeof(*pos), member))
1422 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1423 * @pos: the type * to use as a start point
1424 * @head: the head of the list
1425 * @member: the name of the list_struct within the struct.
1427 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1429 #define list_prepare_entry(pos, head, member) \
1430 ((pos) ? : list_entry(head, typeof(*pos), member))
1433 * list_for_each_entry_continue - continue iteration over list of given type
1434 * @pos: the type * to use as a loop cursor.
1435 * @head: the head for your list.
1436 * @member: the name of the list_struct within the struct.
1438 * Continue to iterate over list of given type, continuing after
1439 * the current position.
1441 #define list_for_each_entry_continue(pos, head, member) \
1442 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
1443 prefetch(pos->member.next), &pos->member != (head); \
1444 pos = list_entry(pos->member.next, typeof(*pos), member))
1447 * list_for_each_entry_continue_reverse - iterate backwards from the given point
1448 * @pos: the type * to use as a loop cursor.
1449 * @head: the head for your list.
1450 * @member: the name of the list_struct within the struct.
1452 * Start to iterate over list of given type backwards, continuing after
1453 * the current position.
1455 #define list_for_each_entry_continue_reverse(pos, head, member) \
1456 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
1457 prefetch(pos->member.prev), &pos->member != (head); \
1458 pos = list_entry(pos->member.prev, typeof(*pos), member))
1461 * list_for_each_entry_from - iterate over list of given type from the current point
1462 * @pos: the type * to use as a loop cursor.
1463 * @head: the head for your list.
1464 * @member: the name of the list_struct within the struct.
1466 * Iterate over list of given type, continuing from current position.
1468 #define list_for_each_entry_from(pos, head, member) \
1469 for (; prefetch(pos->member.next), &pos->member != (head); \
1470 pos = list_entry(pos->member.next, typeof(*pos), member))
1473 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1474 * @pos: the type * to use as a loop cursor.
1475 * @n: another type * to use as temporary storage
1476 * @head: the head for your list.
1477 * @member: the name of the list_struct within the struct.
1479 #define list_for_each_entry_safe(pos, n, head, member) \
1480 for (pos = list_entry((head)->next, typeof(*pos), member), \
1481 n = list_entry(pos->member.next, typeof(*pos), member); \
1482 &pos->member != (head); \
1483 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1486 * list_for_each_entry_safe_continue
1487 * @pos: the type * to use as a loop cursor.
1488 * @n: another type * to use as temporary storage
1489 * @head: the head for your list.
1490 * @member: the name of the list_struct within the struct.
1492 * Iterate over list of given type, continuing after current point,
1493 * safe against removal of list entry.
1495 #define list_for_each_entry_safe_continue(pos, n, head, member) \
1496 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
1497 n = list_entry(pos->member.next, typeof(*pos), member); \
1498 &pos->member != (head); \
1499 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1502 * list_for_each_entry_safe_from
1503 * @pos: the type * to use as a loop cursor.
1504 * @n: another type * to use as temporary storage
1505 * @head: the head for your list.
1506 * @member: the name of the list_struct within the struct.
1508 * Iterate over list of given type from current point, safe against
1509 * removal of list entry.
1511 #define list_for_each_entry_safe_from(pos, n, head, member) \
1512 for (n = list_entry(pos->member.next, typeof(*pos), member); \
1513 &pos->member != (head); \
1514 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1517 * list_for_each_entry_safe_reverse
1518 * @pos: the type * to use as a loop cursor.
1519 * @n: another type * to use as temporary storage
1520 * @head: the head for your list.
1521 * @member: the name of the list_struct within the struct.
1523 * Iterate backwards over list of given type, safe against removal
1526 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
1527 for (pos = list_entry((head)->prev, typeof(*pos), member), \
1528 n = list_entry(pos->member.prev, typeof(*pos), member); \
1529 &pos->member != (head); \
1530 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
1535 * Double linked lists with a single pointer list head.
1536 * Mostly useful for hash tables where the two pointer list head is
1538 * You lose the ability to access the tail in O(1).
1542 struct hlist_node
*first
;
1546 struct hlist_node
*next
, **pprev
;
1549 #define HLIST_HEAD_INIT { .first = NULL }
1550 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
1551 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1552 static inline void INIT_HLIST_NODE(struct hlist_node
*h
)
1558 static inline int hlist_unhashed(const struct hlist_node
*h
)
1563 static inline int hlist_empty(const struct hlist_head
*h
)
1568 static inline void __hlist_del(struct hlist_node
*n
)
1570 struct hlist_node
*next
= n
->next
;
1571 struct hlist_node
**pprev
= n
->pprev
;
1574 next
->pprev
= pprev
;
1577 static inline void hlist_del(struct hlist_node
*n
)
1580 n
->next
= LIST_POISON1
;
1581 n
->pprev
= LIST_POISON2
;
1584 static inline void hlist_del_init(struct hlist_node
*n
)
1586 if (!hlist_unhashed(n
)) {
1592 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
1594 struct hlist_node
*first
= h
->first
;
1597 first
->pprev
= &n
->next
;
1599 n
->pprev
= &h
->first
;
1602 /* next must be != NULL */
1603 static inline void hlist_add_before(struct hlist_node
*n
,
1604 struct hlist_node
*next
)
1606 n
->pprev
= next
->pprev
;
1608 next
->pprev
= &n
->next
;
1612 static inline void hlist_add_after(struct hlist_node
*n
,
1613 struct hlist_node
*next
)
1615 next
->next
= n
->next
;
1617 next
->pprev
= &n
->next
;
1620 next
->next
->pprev
= &next
->next
;
1624 * Move a list from one list head to another. Fixup the pprev
1625 * reference of the first entry if it exists.
1627 static inline void hlist_move_list(struct hlist_head
*old
,
1628 struct hlist_head
*new)
1630 new->first
= old
->first
;
1632 new->first
->pprev
= &new->first
;
1636 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1638 #define hlist_for_each(pos, head) \
1639 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1642 #define hlist_for_each_safe(pos, n, head) \
1643 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1647 * hlist_for_each_entry - iterate over list of given type
1648 * @tpos: the type * to use as a loop cursor.
1649 * @pos: the &struct hlist_node to use as a loop cursor.
1650 * @head: the head for your list.
1651 * @member: the name of the hlist_node within the struct.
1653 #define hlist_for_each_entry(tpos, pos, head, member) \
1654 for (pos = (head)->first; \
1655 pos && ({ prefetch(pos->next); 1;}) && \
1656 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1660 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1661 * @tpos: the type * to use as a loop cursor.
1662 * @pos: the &struct hlist_node to use as a loop cursor.
1663 * @member: the name of the hlist_node within the struct.
1665 #define hlist_for_each_entry_continue(tpos, pos, member) \
1666 for (pos = (pos)->next; \
1667 pos && ({ prefetch(pos->next); 1;}) && \
1668 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1672 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1673 * @tpos: the type * to use as a loop cursor.
1674 * @pos: the &struct hlist_node to use as a loop cursor.
1675 * @member: the name of the hlist_node within the struct.
1677 #define hlist_for_each_entry_from(tpos, pos, member) \
1678 for (; pos && ({ prefetch(pos->next); 1;}) && \
1679 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1683 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1684 * @tpos: the type * to use as a loop cursor.
1685 * @pos: the &struct hlist_node to use as a loop cursor.
1686 * @n: another &struct hlist_node to use as temporary storage
1687 * @head: the head for your list.
1688 * @member: the name of the hlist_node within the struct.
1690 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1691 for (pos = (head)->first; \
1692 pos && ({ n = pos->next; 1; }) && \
1693 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \