/kernel/2.6.32_froyo_photon_nightly/include/linux/percpu.h

http://photon-android.googlecode.com/ · C++ Header · 246 lines · 158 code · 47 blank · 41 comment · 1 complexity · 9bed373d881e01f983a220290bf6147c MD5 · raw file

  1. #ifndef __LINUX_PERCPU_H
  2. #define __LINUX_PERCPU_H
  3. #include <linux/preempt.h>
  4. #include <linux/slab.h> /* For kmalloc() */
  5. #include <linux/smp.h>
  6. #include <linux/cpumask.h>
  7. #include <linux/pfn.h>
  8. #include <asm/percpu.h>
  9. /* enough to cover all DEFINE_PER_CPUs in modules */
  10. #ifdef CONFIG_MODULES
  11. #define PERCPU_MODULE_RESERVE (8 << 10)
  12. #else
  13. #define PERCPU_MODULE_RESERVE 0
  14. #endif
  15. #ifndef PERCPU_ENOUGH_ROOM
  16. #define PERCPU_ENOUGH_ROOM \
  17. (ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES) + \
  18. PERCPU_MODULE_RESERVE)
  19. #endif
  20. /*
  21. * Must be an lvalue. Since @var must be a simple identifier,
  22. * we force a syntax error here if it isn't.
  23. */
  24. #define get_cpu_var(var) (*({ \
  25. extern int simple_identifier_##var(void); \
  26. preempt_disable(); \
  27. &__get_cpu_var(var); }))
  28. #define put_cpu_var(var) preempt_enable()
  29. #ifdef CONFIG_SMP
  30. #ifndef CONFIG_HAVE_LEGACY_PER_CPU_AREA
  31. /* minimum unit size, also is the maximum supported allocation size */
  32. #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(64 << 10)
  33. /*
  34. * PERCPU_DYNAMIC_RESERVE indicates the amount of free area to piggy
  35. * back on the first chunk for dynamic percpu allocation if arch is
  36. * manually allocating and mapping it for faster access (as a part of
  37. * large page mapping for example).
  38. *
  39. * The following values give between one and two pages of free space
  40. * after typical minimal boot (2-way SMP, single disk and NIC) with
  41. * both defconfig and a distro config on x86_64 and 32. More
  42. * intelligent way to determine this would be nice.
  43. */
  44. #if BITS_PER_LONG > 32
  45. #define PERCPU_DYNAMIC_RESERVE (20 << 10)
  46. #else
  47. #define PERCPU_DYNAMIC_RESERVE (12 << 10)
  48. #endif
  49. extern void *pcpu_base_addr;
  50. extern const unsigned long *pcpu_unit_offsets;
  51. struct pcpu_group_info {
  52. int nr_units; /* aligned # of units */
  53. unsigned long base_offset; /* base address offset */
  54. unsigned int *cpu_map; /* unit->cpu map, empty
  55. * entries contain NR_CPUS */
  56. };
  57. struct pcpu_alloc_info {
  58. size_t static_size;
  59. size_t reserved_size;
  60. size_t dyn_size;
  61. size_t unit_size;
  62. size_t atom_size;
  63. size_t alloc_size;
  64. size_t __ai_size; /* internal, don't use */
  65. int nr_groups; /* 0 if grouping unnecessary */
  66. struct pcpu_group_info groups[];
  67. };
  68. enum pcpu_fc {
  69. PCPU_FC_AUTO,
  70. PCPU_FC_EMBED,
  71. PCPU_FC_PAGE,
  72. PCPU_FC_NR,
  73. };
  74. extern const char *pcpu_fc_names[PCPU_FC_NR];
  75. extern enum pcpu_fc pcpu_chosen_fc;
  76. typedef void * (*pcpu_fc_alloc_fn_t)(unsigned int cpu, size_t size,
  77. size_t align);
  78. typedef void (*pcpu_fc_free_fn_t)(void *ptr, size_t size);
  79. typedef void (*pcpu_fc_populate_pte_fn_t)(unsigned long addr);
  80. typedef int (pcpu_fc_cpu_distance_fn_t)(unsigned int from, unsigned int to);
  81. extern struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
  82. int nr_units);
  83. extern void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai);
  84. extern struct pcpu_alloc_info * __init pcpu_build_alloc_info(
  85. size_t reserved_size, ssize_t dyn_size,
  86. size_t atom_size,
  87. pcpu_fc_cpu_distance_fn_t cpu_distance_fn);
  88. extern int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
  89. void *base_addr);
  90. #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
  91. extern int __init pcpu_embed_first_chunk(size_t reserved_size, ssize_t dyn_size,
  92. size_t atom_size,
  93. pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
  94. pcpu_fc_alloc_fn_t alloc_fn,
  95. pcpu_fc_free_fn_t free_fn);
  96. #endif
  97. #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
  98. extern int __init pcpu_page_first_chunk(size_t reserved_size,
  99. pcpu_fc_alloc_fn_t alloc_fn,
  100. pcpu_fc_free_fn_t free_fn,
  101. pcpu_fc_populate_pte_fn_t populate_pte_fn);
  102. #endif
  103. /*
  104. * Use this to get to a cpu's version of the per-cpu object
  105. * dynamically allocated. Non-atomic access to the current CPU's
  106. * version should probably be combined with get_cpu()/put_cpu().
  107. */
  108. #define per_cpu_ptr(ptr, cpu) SHIFT_PERCPU_PTR((ptr), per_cpu_offset((cpu)))
  109. extern void *__alloc_reserved_percpu(size_t size, size_t align);
  110. #else /* CONFIG_HAVE_LEGACY_PER_CPU_AREA */
  111. struct percpu_data {
  112. void *ptrs[1];
  113. };
  114. /* pointer disguising messes up the kmemleak objects tracking */
  115. #ifndef CONFIG_DEBUG_KMEMLEAK
  116. #define __percpu_disguise(pdata) (struct percpu_data *)~(unsigned long)(pdata)
  117. #else
  118. #define __percpu_disguise(pdata) (struct percpu_data *)(pdata)
  119. #endif
  120. #define per_cpu_ptr(ptr, cpu) \
  121. ({ \
  122. struct percpu_data *__p = __percpu_disguise(ptr); \
  123. (__typeof__(ptr))__p->ptrs[(cpu)]; \
  124. })
  125. #endif /* CONFIG_HAVE_LEGACY_PER_CPU_AREA */
  126. extern void *__alloc_percpu(size_t size, size_t align);
  127. extern void free_percpu(void *__pdata);
  128. #ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA
  129. extern void __init setup_per_cpu_areas(void);
  130. #endif
  131. #else /* CONFIG_SMP */
  132. #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); (ptr); })
  133. static inline void *__alloc_percpu(size_t size, size_t align)
  134. {
  135. /*
  136. * Can't easily make larger alignment work with kmalloc. WARN
  137. * on it. Larger alignment should only be used for module
  138. * percpu sections on SMP for which this path isn't used.
  139. */
  140. WARN_ON_ONCE(align > SMP_CACHE_BYTES);
  141. return kzalloc(size, GFP_KERNEL);
  142. }
  143. static inline void free_percpu(void *p)
  144. {
  145. kfree(p);
  146. }
  147. static inline void __init setup_per_cpu_areas(void) { }
  148. static inline void *pcpu_lpage_remapped(void *kaddr)
  149. {
  150. return NULL;
  151. }
  152. #endif /* CONFIG_SMP */
  153. #define alloc_percpu(type) (type *)__alloc_percpu(sizeof(type), \
  154. __alignof__(type))
  155. /*
  156. * Optional methods for optimized non-lvalue per-cpu variable access.
  157. *
  158. * @var can be a percpu variable or a field of it and its size should
  159. * equal char, int or long. percpu_read() evaluates to a lvalue and
  160. * all others to void.
  161. *
  162. * These operations are guaranteed to be atomic w.r.t. preemption.
  163. * The generic versions use plain get/put_cpu_var(). Archs are
  164. * encouraged to implement single-instruction alternatives which don't
  165. * require preemption protection.
  166. */
  167. #ifndef percpu_read
  168. # define percpu_read(var) \
  169. ({ \
  170. typeof(per_cpu_var(var)) __tmp_var__; \
  171. __tmp_var__ = get_cpu_var(var); \
  172. put_cpu_var(var); \
  173. __tmp_var__; \
  174. })
  175. #endif
  176. #define __percpu_generic_to_op(var, val, op) \
  177. do { \
  178. get_cpu_var(var) op val; \
  179. put_cpu_var(var); \
  180. } while (0)
  181. #ifndef percpu_write
  182. # define percpu_write(var, val) __percpu_generic_to_op(var, (val), =)
  183. #endif
  184. #ifndef percpu_add
  185. # define percpu_add(var, val) __percpu_generic_to_op(var, (val), +=)
  186. #endif
  187. #ifndef percpu_sub
  188. # define percpu_sub(var, val) __percpu_generic_to_op(var, (val), -=)
  189. #endif
  190. #ifndef percpu_and
  191. # define percpu_and(var, val) __percpu_generic_to_op(var, (val), &=)
  192. #endif
  193. #ifndef percpu_or
  194. # define percpu_or(var, val) __percpu_generic_to_op(var, (val), |=)
  195. #endif
  196. #ifndef percpu_xor
  197. # define percpu_xor(var, val) __percpu_generic_to_op(var, (val), ^=)
  198. #endif
  199. #endif /* __LINUX_PERCPU_H */