1 /* MECHANICALLY GENERATED, DO NOT EDIT!!! */
6 * common.h: Common Linux kernel-isms.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; but version 2 of the License only due
11 * to code included from the Linux kernel.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * Copyright (c) 2006 Paul E. McKenney, IBM.
24 * Much code taken from the Linux kernel. For such code, the option
25 * to redistribute under later versions of GPL might not be available.
28 #ifndef __always_inline
29 #define __always_inline inline
32 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
33 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
36 # define stringify_in_c(...) __VA_ARGS__
37 # define ASM_CONST(x) x
39 /* This version of stringify will deal with commas... */
40 # define __stringify_in_c(...) #__VA_ARGS__
41 # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
42 # define __ASM_CONST(x) x##UL
43 # define ASM_CONST(x) __ASM_CONST(x)
48 * arch-ppc64.h: Expose PowerPC atomic instructions.
50 * This program is free software; you can redistribute it and/or modify
51 * it under the terms of the GNU General Public License as published by
52 * the Free Software Foundation; but version 2 of the License only due
53 * to code included from the Linux kernel.
55 * This program is distributed in the hope that it will be useful,
56 * but WITHOUT ANY WARRANTY; without even the implied warranty of
57 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58 * GNU General Public License for more details.
60 * You should have received a copy of the GNU General Public License
61 * along with this program; if not, write to the Free Software
62 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
64 * Copyright (c) 2006 Paul E. McKenney, IBM.
66 * Much code taken from the Linux kernel. For such code, the option
67 * to redistribute under later versions of GPL might not be available.
77 #define CACHE_LINE_SIZE 128
78 #define ____cacheline_internodealigned_in_smp \
79 __attribute__((__aligned__(1 << 7)))
82 * Atomic data structure, initialization, and access.
85 typedef struct { volatile int counter
; } atomic_t
;
87 #define ATOMIC_INIT(i) { (i) }
89 #define atomic_read(v) ((v)->counter)
90 #define atomic_set(v, i) (((v)->counter) = (i))
97 #define PPC405_ERR77(ra,rb)
99 # define LWSYNC_ON_SMP stringify_in_c(LWSYNC) "\n"
100 # define ISYNC_ON_SMP "\n\tisync\n"
102 # define LWSYNC_ON_SMP
103 # define ISYNC_ON_SMP
110 * Changes the memory location '*ptr' to be val and returns
111 * the previous value stored there.
113 static __always_inline
unsigned long
114 __xchg_u32(volatile void *p
, unsigned long val
)
118 __asm__
__volatile__(
120 "1: lwarx %0,0,%2 \n"
125 : "=&r" (prev
), "+m" (*(volatile unsigned int *)p
)
135 * Changes the memory location '*ptr' to be val and returns
136 * the previous value stored there.
138 static __always_inline
unsigned long
139 __xchg_u32_local(volatile void *p
, unsigned long val
)
143 __asm__
__volatile__(
144 "1: lwarx %0,0,%2 \n"
148 : "=&r" (prev
), "+m" (*(volatile unsigned int *)p
)
156 static __always_inline
unsigned long
157 __xchg_u64(volatile void *p
, unsigned long val
)
161 __asm__
__volatile__(
163 "1: ldarx %0,0,%2 \n"
168 : "=&r" (prev
), "+m" (*(volatile unsigned long *)p
)
175 static __always_inline
unsigned long
176 __xchg_u64_local(volatile void *p
, unsigned long val
)
180 __asm__
__volatile__(
181 "1: ldarx %0,0,%2 \n"
185 : "=&r" (prev
), "+m" (*(volatile unsigned long *)p
)
194 * This function doesn't exist, so you'll get a linker error
195 * if something tries to do an invalid xchg().
197 extern void __xchg_called_with_bad_pointer(void);
199 static __always_inline
unsigned long
200 __xchg(volatile void *ptr
, unsigned long x
, unsigned int size
)
204 return __xchg_u32(ptr
, x
);
207 return __xchg_u64(ptr
, x
);
210 __xchg_called_with_bad_pointer();
214 static __always_inline
unsigned long
215 __xchg_local(volatile void *ptr
, unsigned long x
, unsigned int size
)
219 return __xchg_u32_local(ptr
, x
);
222 return __xchg_u64_local(ptr
, x
);
225 __xchg_called_with_bad_pointer();
228 #define xchg(ptr,x) \
230 __typeof__(*(ptr)) _x_ = (x); \
231 (__typeof__(*(ptr))) __xchg((ptr), (unsigned long)_x_, sizeof(*(ptr))); \
234 #define xchg_local(ptr,x) \
236 __typeof__(*(ptr)) _x_ = (x); \
237 (__typeof__(*(ptr))) __xchg_local((ptr), \
238 (unsigned long)_x_, sizeof(*(ptr))); \
242 * Compare and exchange - if *p == old, set it to new,
243 * and return the old value of *p.
245 #define __HAVE_ARCH_CMPXCHG 1
247 static __always_inline
unsigned long
248 __cmpxchg_u32(volatile unsigned int *p
, unsigned long old
, unsigned long new)
252 __asm__
__volatile__ (
254 "1: lwarx %0,0,%2 # __cmpxchg_u32\n\
263 : "=&r" (prev
), "+m" (*p
)
264 : "r" (p
), "r" (old
), "r" (new)
270 static __always_inline
unsigned long
271 __cmpxchg_u32_local(volatile unsigned int *p
, unsigned long old
,
276 __asm__
__volatile__ (
277 "1: lwarx %0,0,%2 # __cmpxchg_u32\n\
285 : "=&r" (prev
), "+m" (*p
)
286 : "r" (p
), "r" (old
), "r" (new)
293 static __always_inline
unsigned long
294 __cmpxchg_u64(volatile unsigned long *p
, unsigned long old
, unsigned long new)
298 __asm__
__volatile__ (
300 "1: ldarx %0,0,%2 # __cmpxchg_u64\n\
308 : "=&r" (prev
), "+m" (*p
)
309 : "r" (p
), "r" (old
), "r" (new)
315 static __always_inline
unsigned long
316 __cmpxchg_u64_local(volatile unsigned long *p
, unsigned long old
,
321 __asm__
__volatile__ (
322 "1: ldarx %0,0,%2 # __cmpxchg_u64\n\
329 : "=&r" (prev
), "+m" (*p
)
330 : "r" (p
), "r" (old
), "r" (new)
337 /* This function doesn't exist, so you'll get a linker error
338 if something tries to do an invalid cmpxchg(). */
339 extern void __cmpxchg_called_with_bad_pointer(void);
341 static __always_inline
unsigned long
342 __cmpxchg(volatile void *ptr
, unsigned long old
, unsigned long new,
347 return __cmpxchg_u32(ptr
, old
, new);
350 return __cmpxchg_u64(ptr
, old
, new);
353 __cmpxchg_called_with_bad_pointer();
357 static __always_inline
unsigned long
358 __cmpxchg_local(volatile void *ptr
, unsigned long old
, unsigned long new,
363 return __cmpxchg_u32_local(ptr
, old
, new);
366 return __cmpxchg_u64_local(ptr
, old
, new);
369 __cmpxchg_called_with_bad_pointer();
373 #define cmpxchg(ptr, o, n) \
375 __typeof__(*(ptr)) _o_ = (o); \
376 __typeof__(*(ptr)) _n_ = (n); \
377 (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
378 (unsigned long)_n_, sizeof(*(ptr))); \
382 #define cmpxchg_local(ptr, o, n) \
384 __typeof__(*(ptr)) _o_ = (o); \
385 __typeof__(*(ptr)) _n_ = (n); \
386 (__typeof__(*(ptr))) __cmpxchg_local((ptr), (unsigned long)_o_, \
387 (unsigned long)_n_, sizeof(*(ptr))); \
392 * We handle most unaligned accesses in hardware. On the other hand
393 * unaligned DMA can be very expensive on some ppc64 IO chips (it does
394 * powers of 2 writes until it reaches sufficient alignment).
396 * Based on this we disable the IP header alignment in network drivers.
397 * We also modify NET_SKB_PAD to be a cacheline in size, thus maintaining
398 * cacheline alignment of buffers.
400 #define NET_IP_ALIGN 0
401 #define NET_SKB_PAD L1_CACHE_BYTES
403 #define cmpxchg64(ptr, o, n) \
405 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
406 cmpxchg((ptr), (o), (n)); \
408 #define cmpxchg64_local(ptr, o, n) \
410 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
411 cmpxchg_local((ptr), (o), (n)); \
415 #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
416 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
419 * atomic_add - add integer to atomic variable
420 * @i: integer value to add
421 * @v: pointer of type atomic_t
423 * Atomically adds @a to @v.
425 static __inline__
void atomic_add(int a
, atomic_t
*v
)
429 __asm__
__volatile__(
430 "1: lwarx %0,0,%3 # atomic_add\n\
434 : "=&r" (t
), "+m" (v
->counter
)
435 : "r" (a
), "r" (&v
->counter
)
440 * atomic_sub - subtract the atomic variable
441 * @i: integer value to subtract
442 * @v: pointer of type atomic_t
444 * Atomically subtracts @a from @v.
446 static __inline__
void atomic_sub(int a
, atomic_t
*v
)
450 __asm__
__volatile__(
451 "1: lwarx %0,0,%3 # atomic_sub \n\
455 : "=&r" (t
), "+m" (v
->counter
)
456 : "r" (a
), "r" (&v
->counter
)
460 static __inline__
atomic_sub_return(int a
, atomic_t
*v
)
464 __asm__
__volatile__(
466 1: lwarx %0,0,%2 # atomic_sub_return\n\
472 : "r" (a
), "r" (&v
->counter
)
479 * atomic_sub_and_test - subtract value from variable and test result
480 * @i: integer value to subtract
481 * @v: pointer of type atomic_t
483 * Atomically subtracts @i from @v and returns
484 * true if the result is zero, or false for all
487 static __inline__
int atomic_sub_and_test(int a
, atomic_t
*v
)
489 return atomic_sub_return(a
, v
) == 0;
493 * atomic_inc - increment atomic variable
494 * @v: pointer of type atomic_t
496 * Atomically increments @v by 1.
498 static __inline__
void atomic_inc(atomic_t
*v
)
504 * atomic_dec - decrement atomic variable
505 * @v: pointer of type atomic_t
507 * Atomically decrements @v by 1.
509 static __inline__
void atomic_dec(atomic_t
*v
)
515 * atomic_dec_and_test - decrement and test
516 * @v: pointer of type atomic_t
518 * Atomically decrements @v by 1 and
519 * returns true if the result is 0, or false for all other
522 static __inline__
int atomic_dec_and_test(atomic_t
*v
)
524 return atomic_sub_and_test(1, v
);
528 * atomic_inc_and_test - increment and test
529 * @v: pointer of type atomic_t
531 * Atomically increments @v by 1
532 * and returns true if the result is zero, or false for all
535 static __inline__
int atomic_inc_and_test(atomic_t
*v
)
537 return atomic_inc_return(v
);
541 * atomic_add_return - add and return
542 * @v: pointer of type atomic_t
543 * @i: integer value to add
545 * Atomically adds @i to @v and returns @i + @v
547 static __inline__
int atomic_add_return(int a
, atomic_t
*v
)
551 __asm__
__volatile__(
553 1: lwarx %0,0,%2 # atomic_add_return \n\
559 : "r" (a
), "r" (&v
->counter
)
566 * atomic_add_negative - add and test if negative
567 * @v: pointer of type atomic_t
568 * @i: integer value to add
570 * Atomically adds @i to @v and returns true
571 * if the result is negative, or false when
572 * result is greater than or equal to zero.
574 static __inline__
int atomic_add_negative(int a
, atomic_t
*v
)
576 return atomic_add_return(a
, v
) < 0;
580 * atomic_add_unless - add unless the number is a given value
581 * @v: pointer of type atomic_t
582 * @a: the amount to add to v...
583 * @u: ...unless v is equal to u.
585 * Atomically adds @a to @v, so long as it was not @u.
586 * Returns non-zero if @v was not @u, and zero otherwise.
588 static __inline__
int atomic_add_unless(atomic_t
*v
, int a
, int u
)
592 __asm__
__volatile__(
594 1: lwarx %0,0,%1 # atomic_add_unless\n\
604 : "r" (&v
->counter
), "r" (a
), "r" (u
)
610 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
612 #define atomic_inc_return(v) (atomic_add_return(1,v))
613 #define atomic_dec_return(v) (atomic_sub_return(1,v))
615 /* Atomic operations are already serializing on x86 */
616 #define smp_mb__before_atomic_dec() smp_mb()
617 #define smp_mb__after_atomic_dec() smp_mb()
618 #define smp_mb__before_atomic_inc() smp_mb()
619 #define smp_mb__after_atomic_inc() smp_mb()
621 #define smp_mb() __asm__ __volatile__("sync" : : : "memory")
625 * Generate 64-bit timestamp.
628 static unsigned long long get_timestamp(void)
630 unsigned int __a
,__d
;
632 asm volatile("mftbl %0" : "=r" (__a
));
633 asm volatile("mftbu %0" : "=r" (__d
));
634 return ((long long)__a
) | (((long long)__d
)<<32);
638 * api_pthreads.h: API mapping to pthreads environment.
640 * This program is free software; you can redistribute it and/or modify
641 * it under the terms of the GNU General Public License as published by
642 * the Free Software Foundation; either version 2 of the License, or
643 * (at your option) any later version. However, please note that much
644 * of the code in this file derives from the Linux kernel, and that such
645 * code may not be available except under GPLv2.
647 * This program is distributed in the hope that it will be useful,
648 * but WITHOUT ANY WARRANTY; without even the implied warranty of
649 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
650 * GNU General Public License for more details.
652 * You should have received a copy of the GNU General Public License
653 * along with this program; if not, write to the Free Software
654 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
656 * Copyright (c) 2006 Paul E. McKenney, IBM.
663 #include <sys/types.h>
667 #include <sys/param.h>
668 /* #include "atomic.h" */
673 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
674 #define container_of(ptr, type, member) ({ \
675 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
676 (type *)( (char *)__mptr - offsetof(type,member) );})
677 #define barrier() __asm__ __volatile__("": : :"memory")
680 * Default machine parameters.
683 #ifndef CACHE_LINE_SIZE
684 #define CACHE_LINE_SIZE 128
685 #endif /* #ifndef CACHE_LINE_SIZE */
688 * Exclusive locking primitives.
691 typedef pthread_mutex_t spinlock_t
;
693 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
694 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
696 static void spin_lock_init(spinlock_t
*sp
)
698 if (pthread_mutex_init(sp
, NULL
) != 0) {
699 perror("spin_lock_init:pthread_mutex_init");
704 static void spin_lock(spinlock_t
*sp
)
706 if (pthread_mutex_lock(sp
) != 0) {
707 perror("spin_lock:pthread_mutex_lock");
712 static int spin_trylock(spinlock_t
*sp
)
716 if ((retval
= pthread_mutex_trylock(sp
)) == 0)
720 perror("spin_trylock:pthread_mutex_trylock");
724 static void spin_unlock(spinlock_t
*sp
)
726 if (pthread_mutex_unlock(sp
) != 0) {
727 perror("spin_unlock:pthread_mutex_unlock");
732 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
733 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
735 #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
736 #define unlikely(x) x
738 #define prefetch(x) x
741 * Thread creation/destruction primitives.
744 typedef pthread_t thread_id_t
;
746 #define NR_THREADS 128
748 #define __THREAD_ID_MAP_EMPTY 0
749 #define __THREAD_ID_MAP_WAITING 1
750 thread_id_t __thread_id_map
[NR_THREADS
];
751 spinlock_t __thread_id_map_mutex
;
753 #define for_each_thread(t) \
754 for (t = 0; t < NR_THREADS; t++)
756 #define for_each_running_thread(t) \
757 for (t = 0; t < NR_THREADS; t++) \
758 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
759 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
761 #define for_each_tid(t, tid) \
762 for (t = 0; t < NR_THREADS; t++) \
763 if ((((tid) = __thread_id_map[t]) != __THREAD_ID_MAP_EMPTY) && \
764 ((tid) != __THREAD_ID_MAP_WAITING))
766 pthread_key_t thread_id_key
;
768 static int __smp_thread_id(void)
771 thread_id_t tid
= pthread_self();
773 for (i
= 0; i
< NR_THREADS
; i
++) {
774 if (__thread_id_map
[i
] == tid
) {
775 long v
= i
+ 1; /* must be non-NULL. */
777 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
778 perror("pthread_setspecific");
784 spin_lock(&__thread_id_map_mutex
);
785 for (i
= 0; i
< NR_THREADS
; i
++) {
786 if (__thread_id_map
[i
] == tid
)
787 spin_unlock(&__thread_id_map_mutex
);
790 spin_unlock(&__thread_id_map_mutex
);
791 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %d(%#x)\n", tid
, tid
);
795 static int smp_thread_id(void)
799 id
= pthread_getspecific(thread_id_key
);
801 return __smp_thread_id();
802 return (long)(id
- 1);
805 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
810 spin_lock(&__thread_id_map_mutex
);
811 for (i
= 0; i
< NR_THREADS
; i
++) {
812 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
815 if (i
>= NR_THREADS
) {
816 spin_unlock(&__thread_id_map_mutex
);
817 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
820 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
821 spin_unlock(&__thread_id_map_mutex
);
822 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
823 perror("create_thread:pthread_create");
826 __thread_id_map
[i
] = tid
;
830 static void *wait_thread(thread_id_t tid
)
835 for (i
= 0; i
< NR_THREADS
; i
++) {
836 if (__thread_id_map
[i
] == tid
)
839 if (i
>= NR_THREADS
){
840 fprintf(stderr
, "wait_thread: bad tid = %d(%#x)\n", tid
, tid
);
843 if (pthread_join(tid
, &vp
) != 0) {
844 perror("wait_thread:pthread_join");
847 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
851 static void wait_all_threads(void)
856 for (i
= 1; i
< NR_THREADS
; i
++) {
857 tid
= __thread_id_map
[i
];
858 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
859 tid
!= __THREAD_ID_MAP_WAITING
)
860 (void)wait_thread(tid
);
864 static void run_on(int cpu
)
870 sched_setaffinity(0, sizeof(mask
), &mask
);
874 * timekeeping -- very crude -- should use MONOTONIC...
877 long long get_microseconds(void)
881 if (gettimeofday(&tv
, NULL
) != 0)
883 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
887 * Per-thread variables.
890 #define DEFINE_PER_THREAD(type, name) \
893 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
894 } __per_thread_##name[NR_THREADS];
895 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
897 #define per_thread(name, thread) __per_thread_##name[thread].v
898 #define __get_thread_var(name) per_thread(name, smp_thread_id())
900 #define init_per_thread(name, v) \
903 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
904 per_thread(name, __i_p_t_i) = v; \
908 * CPU traversal primitives.
913 #endif /* #ifndef NR_CPUS */
915 #define for_each_possible_cpu(cpu) \
916 for (cpu = 0; cpu < NR_CPUS; cpu++)
917 #define for_each_online_cpu(cpu) \
918 for (cpu = 0; cpu < NR_CPUS; cpu++)
924 #define DEFINE_PER_CPU(type, name) \
927 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
928 } __per_cpu_##name[NR_CPUS]
929 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
931 DEFINE_PER_THREAD(int, smp_processor_id
);
933 static int smp_processor_id(void)
935 return __get_thread_var(smp_processor_id
);
938 static void set_smp_processor_id(int cpu
)
940 __get_thread_var(smp_processor_id
) = cpu
;
943 #define per_cpu(name, thread) __per_cpu_##name[thread].v
944 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
946 #define init_per_cpu(name, v) \
949 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
950 per_cpu(name, __i_p_c_i) = v; \
954 * CPU state checking (crowbarred).
957 #define idle_cpu(cpu) 0
958 #define in_softirq() 1
959 #define hardirq_count() 0
960 #define PREEMPT_SHIFT 0
961 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
962 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
963 #define PREEMPT_BITS 8
964 #define SOFTIRQ_BITS 8
970 struct notifier_block
{
971 int (*notifier_call
)(struct notifier_block
*, unsigned long, void *);
972 struct notifier_block
*next
;
976 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
977 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
978 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
979 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
980 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
981 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
982 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
983 * not handling interrupts, soon dead */
984 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
987 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
988 * operation in progress
990 #define CPU_TASKS_FROZEN 0x0010
992 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
993 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
994 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
995 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
996 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
997 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
998 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
1000 /* Hibernation and suspend events */
1001 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
1002 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
1003 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
1004 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
1005 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
1006 #define PM_POST_RESTORE 0x0006 /* Restore failed */
1008 #define NOTIFY_DONE 0x0000 /* Don't care */
1009 #define NOTIFY_OK 0x0001 /* Suits me */
1010 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
1011 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
1012 /* Bad/Veto action */
1014 * Clean way to return from the notifier and stop further calls.
1016 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
1022 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
1025 * Initialization -- Must be called before calling any primitives.
1028 static void smp_init(void)
1032 spin_lock_init(&__thread_id_map_mutex
);
1033 __thread_id_map
[0] = pthread_self();
1034 for (i
= 1; i
< NR_THREADS
; i
++)
1035 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
1036 init_per_thread(smp_processor_id
, 0);
1037 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
1038 perror("pthread_key_create");
1043 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
1045 #ifndef _LINUX_LIST_H
1046 #define _LINUX_LIST_H
1048 #define LIST_POISON1 ((void *) 0x00100100)
1049 #define LIST_POISON2 ((void *) 0x00200200)
1051 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
1052 #define container_of(ptr, type, member) ({ \
1053 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
1054 (type *)( (char *)__mptr - offsetof(type,member) );})
1057 * Simple doubly linked list implementation.
1059 * Some of the internal functions ("__xxx") are useful when
1060 * manipulating whole lists rather than single entries, as
1061 * sometimes we already know the next/prev entries and we can
1062 * generate better code by using them directly rather than
1063 * using the generic single-entry routines.
1067 struct list_head
*next
, *prev
;
1070 #define LIST_HEAD_INIT(name) { &(name), &(name) }
1072 #define LIST_HEAD(name) \
1073 struct list_head name = LIST_HEAD_INIT(name)
1075 static inline void INIT_LIST_HEAD(struct list_head
*list
)
1082 * Insert a new entry between two known consecutive entries.
1084 * This is only for internal list manipulation where we know
1085 * the prev/next entries already!
1087 #ifndef CONFIG_DEBUG_LIST
1088 static inline void __list_add(struct list_head
*new,
1089 struct list_head
*prev
,
1090 struct list_head
*next
)
1098 extern void __list_add(struct list_head
*new,
1099 struct list_head
*prev
,
1100 struct list_head
*next
);
1104 * list_add - add a new entry
1105 * @new: new entry to be added
1106 * @head: list head to add it after
1108 * Insert a new entry after the specified head.
1109 * This is good for implementing stacks.
1111 static inline void list_add(struct list_head
*new, struct list_head
*head
)
1113 __list_add(new, head
, head
->next
);
1118 * list_add_tail - add a new entry
1119 * @new: new entry to be added
1120 * @head: list head to add it before
1122 * Insert a new entry before the specified head.
1123 * This is useful for implementing queues.
1125 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
1127 __list_add(new, head
->prev
, head
);
1131 * Delete a list entry by making the prev/next entries
1132 * point to each other.
1134 * This is only for internal list manipulation where we know
1135 * the prev/next entries already!
1137 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
1144 * list_del - deletes entry from list.
1145 * @entry: the element to delete from the list.
1146 * Note: list_empty() on entry does not return true after this, the entry is
1147 * in an undefined state.
1149 #ifndef CONFIG_DEBUG_LIST
1150 static inline void list_del(struct list_head
*entry
)
1152 __list_del(entry
->prev
, entry
->next
);
1153 entry
->next
= LIST_POISON1
;
1154 entry
->prev
= LIST_POISON2
;
1157 extern void list_del(struct list_head
*entry
);
1161 * list_replace - replace old entry by new one
1162 * @old : the element to be replaced
1163 * @new : the new element to insert
1165 * If @old was empty, it will be overwritten.
1167 static inline void list_replace(struct list_head
*old
,
1168 struct list_head
*new)
1170 new->next
= old
->next
;
1171 new->next
->prev
= new;
1172 new->prev
= old
->prev
;
1173 new->prev
->next
= new;
1176 static inline void list_replace_init(struct list_head
*old
,
1177 struct list_head
*new)
1179 list_replace(old
, new);
1180 INIT_LIST_HEAD(old
);
1184 * list_del_init - deletes entry from list and reinitialize it.
1185 * @entry: the element to delete from the list.
1187 static inline void list_del_init(struct list_head
*entry
)
1189 __list_del(entry
->prev
, entry
->next
);
1190 INIT_LIST_HEAD(entry
);
1194 * list_move - delete from one list and add as another's head
1195 * @list: the entry to move
1196 * @head: the head that will precede our entry
1198 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
1200 __list_del(list
->prev
, list
->next
);
1201 list_add(list
, head
);
1205 * list_move_tail - delete from one list and add as another's tail
1206 * @list: the entry to move
1207 * @head: the head that will follow our entry
1209 static inline void list_move_tail(struct list_head
*list
,
1210 struct list_head
*head
)
1212 __list_del(list
->prev
, list
->next
);
1213 list_add_tail(list
, head
);
1217 * list_is_last - tests whether @list is the last entry in list @head
1218 * @list: the entry to test
1219 * @head: the head of the list
1221 static inline int list_is_last(const struct list_head
*list
,
1222 const struct list_head
*head
)
1224 return list
->next
== head
;
1228 * list_empty - tests whether a list is empty
1229 * @head: the list to test.
1231 static inline int list_empty(const struct list_head
*head
)
1233 return head
->next
== head
;
1237 * list_empty_careful - tests whether a list is empty and not being modified
1238 * @head: the list to test
1241 * tests whether a list is empty _and_ checks that no other CPU might be
1242 * in the process of modifying either member (next or prev)
1244 * NOTE: using list_empty_careful() without synchronization
1245 * can only be safe if the only activity that can happen
1246 * to the list entry is list_del_init(). Eg. it cannot be used
1247 * if another CPU could re-list_add() it.
1249 static inline int list_empty_careful(const struct list_head
*head
)
1251 struct list_head
*next
= head
->next
;
1252 return (next
== head
) && (next
== head
->prev
);
1256 * list_is_singular - tests whether a list has just one entry.
1257 * @head: the list to test.
1259 static inline int list_is_singular(const struct list_head
*head
)
1261 return !list_empty(head
) && (head
->next
== head
->prev
);
1264 static inline void __list_cut_position(struct list_head
*list
,
1265 struct list_head
*head
, struct list_head
*entry
)
1267 struct list_head
*new_first
= entry
->next
;
1268 list
->next
= head
->next
;
1269 list
->next
->prev
= list
;
1272 head
->next
= new_first
;
1273 new_first
->prev
= head
;
1277 * list_cut_position - cut a list into two
1278 * @list: a new list to add all removed entries
1279 * @head: a list with entries
1280 * @entry: an entry within head, could be the head itself
1281 * and if so we won't cut the list
1283 * This helper moves the initial part of @head, up to and
1284 * including @entry, from @head to @list. You should
1285 * pass on @entry an element you know is on @head. @list
1286 * should be an empty list or a list you do not care about
1290 static inline void list_cut_position(struct list_head
*list
,
1291 struct list_head
*head
, struct list_head
*entry
)
1293 if (list_empty(head
))
1295 if (list_is_singular(head
) &&
1296 (head
->next
!= entry
&& head
!= entry
))
1299 INIT_LIST_HEAD(list
);
1301 __list_cut_position(list
, head
, entry
);
1304 static inline void __list_splice(const struct list_head
*list
,
1305 struct list_head
*prev
,
1306 struct list_head
*next
)
1308 struct list_head
*first
= list
->next
;
1309 struct list_head
*last
= list
->prev
;
1319 * list_splice - join two lists, this is designed for stacks
1320 * @list: the new list to add.
1321 * @head: the place to add it in the first list.
1323 static inline void list_splice(const struct list_head
*list
,
1324 struct list_head
*head
)
1326 if (!list_empty(list
))
1327 __list_splice(list
, head
, head
->next
);
1331 * list_splice_tail - join two lists, each list being a queue
1332 * @list: the new list to add.
1333 * @head: the place to add it in the first list.
1335 static inline void list_splice_tail(struct list_head
*list
,
1336 struct list_head
*head
)
1338 if (!list_empty(list
))
1339 __list_splice(list
, head
->prev
, head
);
1343 * list_splice_init - join two lists and reinitialise the emptied list.
1344 * @list: the new list to add.
1345 * @head: the place to add it in the first list.
1347 * The list at @list is reinitialised
1349 static inline void list_splice_init(struct list_head
*list
,
1350 struct list_head
*head
)
1352 if (!list_empty(list
)) {
1353 __list_splice(list
, head
, head
->next
);
1354 INIT_LIST_HEAD(list
);
1359 * list_splice_tail_init - join two lists and reinitialise the emptied list
1360 * @list: the new list to add.
1361 * @head: the place to add it in the first list.
1363 * Each of the lists is a queue.
1364 * The list at @list is reinitialised
1366 static inline void list_splice_tail_init(struct list_head
*list
,
1367 struct list_head
*head
)
1369 if (!list_empty(list
)) {
1370 __list_splice(list
, head
->prev
, head
);
1371 INIT_LIST_HEAD(list
);
1376 * list_entry - get the struct for this entry
1377 * @ptr: the &struct list_head pointer.
1378 * @type: the type of the struct this is embedded in.
1379 * @member: the name of the list_struct within the struct.
1381 #define list_entry(ptr, type, member) \
1382 container_of(ptr, type, member)
1385 * list_first_entry - get the first element from a list
1386 * @ptr: the list head to take the element from.
1387 * @type: the type of the struct this is embedded in.
1388 * @member: the name of the list_struct within the struct.
1390 * Note, that list is expected to be not empty.
1392 #define list_first_entry(ptr, type, member) \
1393 list_entry((ptr)->next, type, member)
1396 * list_for_each - iterate over a list
1397 * @pos: the &struct list_head to use as a loop cursor.
1398 * @head: the head for your list.
1400 #define list_for_each(pos, head) \
1401 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
1405 * __list_for_each - iterate over a list
1406 * @pos: the &struct list_head to use as a loop cursor.
1407 * @head: the head for your list.
1409 * This variant differs from list_for_each() in that it's the
1410 * simplest possible list iteration code, no prefetching is done.
1411 * Use this for code that knows the list to be very short (empty
1412 * or 1 entry) most of the time.
1414 #define __list_for_each(pos, head) \
1415 for (pos = (head)->next; pos != (head); pos = pos->next)
1418 * list_for_each_prev - iterate over a list backwards
1419 * @pos: the &struct list_head to use as a loop cursor.
1420 * @head: the head for your list.
1422 #define list_for_each_prev(pos, head) \
1423 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
1427 * list_for_each_safe - iterate over a list safe against removal of list entry
1428 * @pos: the &struct list_head to use as a loop cursor.
1429 * @n: another &struct list_head to use as temporary storage
1430 * @head: the head for your list.
1432 #define list_for_each_safe(pos, n, head) \
1433 for (pos = (head)->next, n = pos->next; pos != (head); \
1434 pos = n, n = pos->next)
1437 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
1438 * @pos: the &struct list_head to use as a loop cursor.
1439 * @n: another &struct list_head to use as temporary storage
1440 * @head: the head for your list.
1442 #define list_for_each_prev_safe(pos, n, head) \
1443 for (pos = (head)->prev, n = pos->prev; \
1444 prefetch(pos->prev), pos != (head); \
1445 pos = n, n = pos->prev)
1448 * list_for_each_entry - iterate over list of given type
1449 * @pos: the type * to use as a loop cursor.
1450 * @head: the head for your list.
1451 * @member: the name of the list_struct within the struct.
1453 #define list_for_each_entry(pos, head, member) \
1454 for (pos = list_entry((head)->next, typeof(*pos), member); \
1455 prefetch(pos->member.next), &pos->member != (head); \
1456 pos = list_entry(pos->member.next, typeof(*pos), member))
1459 * list_for_each_entry_reverse - iterate backwards over list of given type.
1460 * @pos: the type * to use as a loop cursor.
1461 * @head: the head for your list.
1462 * @member: the name of the list_struct within the struct.
1464 #define list_for_each_entry_reverse(pos, head, member) \
1465 for (pos = list_entry((head)->prev, typeof(*pos), member); \
1466 prefetch(pos->member.prev), &pos->member != (head); \
1467 pos = list_entry(pos->member.prev, typeof(*pos), member))
1470 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1471 * @pos: the type * to use as a start point
1472 * @head: the head of the list
1473 * @member: the name of the list_struct within the struct.
1475 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1477 #define list_prepare_entry(pos, head, member) \
1478 ((pos) ? : list_entry(head, typeof(*pos), member))
1481 * list_for_each_entry_continue - continue iteration over list of given type
1482 * @pos: the type * to use as a loop cursor.
1483 * @head: the head for your list.
1484 * @member: the name of the list_struct within the struct.
1486 * Continue to iterate over list of given type, continuing after
1487 * the current position.
1489 #define list_for_each_entry_continue(pos, head, member) \
1490 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
1491 prefetch(pos->member.next), &pos->member != (head); \
1492 pos = list_entry(pos->member.next, typeof(*pos), member))
1495 * list_for_each_entry_continue_reverse - iterate backwards from the given point
1496 * @pos: the type * to use as a loop cursor.
1497 * @head: the head for your list.
1498 * @member: the name of the list_struct within the struct.
1500 * Start to iterate over list of given type backwards, continuing after
1501 * the current position.
1503 #define list_for_each_entry_continue_reverse(pos, head, member) \
1504 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
1505 prefetch(pos->member.prev), &pos->member != (head); \
1506 pos = list_entry(pos->member.prev, typeof(*pos), member))
1509 * list_for_each_entry_from - iterate over list of given type from the current point
1510 * @pos: the type * to use as a loop cursor.
1511 * @head: the head for your list.
1512 * @member: the name of the list_struct within the struct.
1514 * Iterate over list of given type, continuing from current position.
1516 #define list_for_each_entry_from(pos, head, member) \
1517 for (; prefetch(pos->member.next), &pos->member != (head); \
1518 pos = list_entry(pos->member.next, typeof(*pos), member))
1521 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1522 * @pos: the type * to use as a loop cursor.
1523 * @n: another type * to use as temporary storage
1524 * @head: the head for your list.
1525 * @member: the name of the list_struct within the struct.
1527 #define list_for_each_entry_safe(pos, n, head, member) \
1528 for (pos = list_entry((head)->next, typeof(*pos), member), \
1529 n = list_entry(pos->member.next, typeof(*pos), member); \
1530 &pos->member != (head); \
1531 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1534 * list_for_each_entry_safe_continue
1535 * @pos: the type * to use as a loop cursor.
1536 * @n: another type * to use as temporary storage
1537 * @head: the head for your list.
1538 * @member: the name of the list_struct within the struct.
1540 * Iterate over list of given type, continuing after current point,
1541 * safe against removal of list entry.
1543 #define list_for_each_entry_safe_continue(pos, n, head, member) \
1544 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
1545 n = list_entry(pos->member.next, typeof(*pos), member); \
1546 &pos->member != (head); \
1547 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1550 * list_for_each_entry_safe_from
1551 * @pos: the type * to use as a loop cursor.
1552 * @n: another type * to use as temporary storage
1553 * @head: the head for your list.
1554 * @member: the name of the list_struct within the struct.
1556 * Iterate over list of given type from current point, safe against
1557 * removal of list entry.
1559 #define list_for_each_entry_safe_from(pos, n, head, member) \
1560 for (n = list_entry(pos->member.next, typeof(*pos), member); \
1561 &pos->member != (head); \
1562 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1565 * list_for_each_entry_safe_reverse
1566 * @pos: the type * to use as a loop cursor.
1567 * @n: another type * to use as temporary storage
1568 * @head: the head for your list.
1569 * @member: the name of the list_struct within the struct.
1571 * Iterate backwards over list of given type, safe against removal
1574 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
1575 for (pos = list_entry((head)->prev, typeof(*pos), member), \
1576 n = list_entry(pos->member.prev, typeof(*pos), member); \
1577 &pos->member != (head); \
1578 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
1581 * Double linked lists with a single pointer list head.
1582 * Mostly useful for hash tables where the two pointer list head is
1584 * You lose the ability to access the tail in O(1).
1588 struct hlist_node
*first
;
1592 struct hlist_node
*next
, **pprev
;
1595 #define HLIST_HEAD_INIT { .first = NULL }
1596 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
1597 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1598 static inline void INIT_HLIST_NODE(struct hlist_node
*h
)
1604 static inline int hlist_unhashed(const struct hlist_node
*h
)
1609 static inline int hlist_empty(const struct hlist_head
*h
)
1614 static inline void __hlist_del(struct hlist_node
*n
)
1616 struct hlist_node
*next
= n
->next
;
1617 struct hlist_node
**pprev
= n
->pprev
;
1620 next
->pprev
= pprev
;
1623 static inline void hlist_del(struct hlist_node
*n
)
1626 n
->next
= LIST_POISON1
;
1627 n
->pprev
= LIST_POISON2
;
1630 static inline void hlist_del_init(struct hlist_node
*n
)
1632 if (!hlist_unhashed(n
)) {
1638 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
1640 struct hlist_node
*first
= h
->first
;
1643 first
->pprev
= &n
->next
;
1645 n
->pprev
= &h
->first
;
1648 /* next must be != NULL */
1649 static inline void hlist_add_before(struct hlist_node
*n
,
1650 struct hlist_node
*next
)
1652 n
->pprev
= next
->pprev
;
1654 next
->pprev
= &n
->next
;
1658 static inline void hlist_add_after(struct hlist_node
*n
,
1659 struct hlist_node
*next
)
1661 next
->next
= n
->next
;
1663 next
->pprev
= &n
->next
;
1666 next
->next
->pprev
= &next
->next
;
1670 * Move a list from one list head to another. Fixup the pprev
1671 * reference of the first entry if it exists.
1673 static inline void hlist_move_list(struct hlist_head
*old
,
1674 struct hlist_head
*new)
1676 new->first
= old
->first
;
1678 new->first
->pprev
= &new->first
;
1682 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1684 #define hlist_for_each(pos, head) \
1685 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1688 #define hlist_for_each_safe(pos, n, head) \
1689 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1693 * hlist_for_each_entry - iterate over list of given type
1694 * @tpos: the type * to use as a loop cursor.
1695 * @pos: the &struct hlist_node to use as a loop cursor.
1696 * @head: the head for your list.
1697 * @member: the name of the hlist_node within the struct.
1699 #define hlist_for_each_entry(tpos, pos, head, member) \
1700 for (pos = (head)->first; \
1701 pos && ({ prefetch(pos->next); 1;}) && \
1702 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1706 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1707 * @tpos: the type * to use as a loop cursor.
1708 * @pos: the &struct hlist_node to use as a loop cursor.
1709 * @member: the name of the hlist_node within the struct.
1711 #define hlist_for_each_entry_continue(tpos, pos, member) \
1712 for (pos = (pos)->next; \
1713 pos && ({ prefetch(pos->next); 1;}) && \
1714 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1718 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1719 * @tpos: the type * to use as a loop cursor.
1720 * @pos: the &struct hlist_node to use as a loop cursor.
1721 * @member: the name of the hlist_node within the struct.
1723 #define hlist_for_each_entry_from(tpos, pos, member) \
1724 for (; pos && ({ prefetch(pos->next); 1;}) && \
1725 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1729 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1730 * @tpos: the type * to use as a loop cursor.
1731 * @pos: the &struct hlist_node to use as a loop cursor.
1732 * @n: another &struct hlist_node to use as temporary storage
1733 * @head: the head for your list.
1734 * @member: the name of the hlist_node within the struct.
1736 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1737 for (pos = (head)->first; \
1738 pos && ({ n = pos->next; 1; }) && \
1739 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \