blob: 6f61b61b7996a8f248af9eb64139a5f9bfc52c7c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __LINUX_PERCPU_H
2#define __LINUX_PERCPU_H
Martin Peschke7ff6f082006-09-25 23:31:21 -07003
Sasha Levin309381fea2014-01-23 15:52:54 -08004#include <linux/mmdebug.h>
Robert P. J. Day0a3021f2007-07-15 23:39:57 -07005#include <linux/preempt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/smp.h>
Martin Peschke7ff6f082006-09-25 23:31:21 -07007#include <linux/cpumask.h>
Tejun Heo6a242902009-03-06 14:33:58 +09008#include <linux/pfn.h>
Tejun Heode380b52010-03-24 17:06:43 +09009#include <linux/init.h>
Martin Peschke7ff6f082006-09-25 23:31:21 -070010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <asm/percpu.h>
12
Tejun Heo6a242902009-03-06 14:33:58 +090013/* enough to cover all DEFINE_PER_CPUs in modules */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +020014#ifdef CONFIG_MODULES
Tejun Heo6a242902009-03-06 14:33:58 +090015#define PERCPU_MODULE_RESERVE (8 << 10)
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +020016#else
Tejun Heo6a242902009-03-06 14:33:58 +090017#define PERCPU_MODULE_RESERVE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#endif
19
Tejun Heo6a242902009-03-06 14:33:58 +090020#ifndef PERCPU_ENOUGH_ROOM
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +020021#define PERCPU_ENOUGH_ROOM \
Tejun Heo6a242902009-03-06 14:33:58 +090022 (ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES) + \
23 PERCPU_MODULE_RESERVE)
24#endif
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +020025
Tejun Heo8d408b42009-02-24 11:57:21 +090026/* minimum unit size, also is the maximum supported allocation size */
Tejun Heo6abad5a2010-09-03 18:22:47 +020027#define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
Tejun Heo8d408b42009-02-24 11:57:21 +090028
29/*
Tejun Heo099a19d2010-06-27 18:50:00 +020030 * Percpu allocator can serve percpu allocations before slab is
31 * initialized which allows slab to depend on the percpu allocator.
32 * The following two parameters decide how much resource to
33 * preallocate for this. Keep PERCPU_DYNAMIC_RESERVE equal to or
34 * larger than PERCPU_DYNAMIC_EARLY_SIZE.
35 */
36#define PERCPU_DYNAMIC_EARLY_SLOTS 128
37#define PERCPU_DYNAMIC_EARLY_SIZE (12 << 10)
38
39/*
Tejun Heo8d408b42009-02-24 11:57:21 +090040 * PERCPU_DYNAMIC_RESERVE indicates the amount of free area to piggy
Tejun Heo6b19b0c2009-03-06 14:33:59 +090041 * back on the first chunk for dynamic percpu allocation if arch is
42 * manually allocating and mapping it for faster access (as a part of
43 * large page mapping for example).
Tejun Heo8d408b42009-02-24 11:57:21 +090044 *
Tejun Heo6b19b0c2009-03-06 14:33:59 +090045 * The following values give between one and two pages of free space
46 * after typical minimal boot (2-way SMP, single disk and NIC) with
47 * both defconfig and a distro config on x86_64 and 32. More
48 * intelligent way to determine this would be nice.
Tejun Heo8d408b42009-02-24 11:57:21 +090049 */
Tejun Heo6b19b0c2009-03-06 14:33:59 +090050#if BITS_PER_LONG > 32
51#define PERCPU_DYNAMIC_RESERVE (20 << 10)
52#else
53#define PERCPU_DYNAMIC_RESERVE (12 << 10)
54#endif
Tejun Heo8d408b42009-02-24 11:57:21 +090055
Tejun Heofbf59bc2009-02-20 16:29:08 +090056extern void *pcpu_base_addr;
Tejun Heofb435d52009-08-14 15:00:51 +090057extern const unsigned long *pcpu_unit_offsets;
Tejun Heofbf59bc2009-02-20 16:29:08 +090058
Tejun Heofd1e8a12009-08-14 15:00:51 +090059struct pcpu_group_info {
60 int nr_units; /* aligned # of units */
61 unsigned long base_offset; /* base address offset */
62 unsigned int *cpu_map; /* unit->cpu map, empty
63 * entries contain NR_CPUS */
64};
65
66struct pcpu_alloc_info {
67 size_t static_size;
68 size_t reserved_size;
69 size_t dyn_size;
70 size_t unit_size;
71 size_t atom_size;
72 size_t alloc_size;
73 size_t __ai_size; /* internal, don't use */
74 int nr_groups; /* 0 if grouping unnecessary */
75 struct pcpu_group_info groups[];
76};
77
Tejun Heof58dc012009-08-14 15:00:50 +090078enum pcpu_fc {
79 PCPU_FC_AUTO,
80 PCPU_FC_EMBED,
81 PCPU_FC_PAGE,
Tejun Heof58dc012009-08-14 15:00:50 +090082
83 PCPU_FC_NR,
84};
Andi Kleen17f36092012-10-04 17:12:07 -070085extern const char * const pcpu_fc_names[PCPU_FC_NR];
Tejun Heof58dc012009-08-14 15:00:50 +090086
87extern enum pcpu_fc pcpu_chosen_fc;
88
Tejun Heo3cbc8562009-08-14 15:00:50 +090089typedef void * (*pcpu_fc_alloc_fn_t)(unsigned int cpu, size_t size,
90 size_t align);
Tejun Heod4b95f82009-07-04 08:10:59 +090091typedef void (*pcpu_fc_free_fn_t)(void *ptr, size_t size);
92typedef void (*pcpu_fc_populate_pte_fn_t)(unsigned long addr);
Tejun Heoa530b792009-07-04 08:11:00 +090093typedef int (pcpu_fc_cpu_distance_fn_t)(unsigned int from, unsigned int to);
Tejun Heofbf59bc2009-02-20 16:29:08 +090094
Tejun Heofd1e8a12009-08-14 15:00:51 +090095extern struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
96 int nr_units);
97extern void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai);
Tejun Heo033e48f2009-08-14 15:00:51 +090098
Tejun Heofb435d52009-08-14 15:00:51 +090099extern int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
100 void *base_addr);
Tejun Heo8d408b42009-02-24 11:57:21 +0900101
Tejun Heo08fc4582009-08-14 15:00:49 +0900102#ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
Tejun Heo4ba6ce22010-06-27 18:49:59 +0200103extern int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
Tejun Heoc8826dd2009-08-14 15:00:52 +0900104 size_t atom_size,
105 pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
106 pcpu_fc_alloc_fn_t alloc_fn,
107 pcpu_fc_free_fn_t free_fn);
Tejun Heo08fc4582009-08-14 15:00:49 +0900108#endif
Tejun Heo66c3a752009-03-10 16:27:48 +0900109
Tejun Heo08fc4582009-08-14 15:00:49 +0900110#ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
Tejun Heofb435d52009-08-14 15:00:51 +0900111extern int __init pcpu_page_first_chunk(size_t reserved_size,
Tejun Heod4b95f82009-07-04 08:10:59 +0900112 pcpu_fc_alloc_fn_t alloc_fn,
113 pcpu_fc_free_fn_t free_fn,
114 pcpu_fc_populate_pte_fn_t populate_pte_fn);
Tejun Heo08fc4582009-08-14 15:00:49 +0900115#endif
Tejun Heod4b95f82009-07-04 08:10:59 +0900116
Rusty Russelle0fdb0e2009-10-29 22:34:15 +0900117extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align);
Tejun Heo10fad5e2010-03-10 18:57:54 +0900118extern bool is_kernel_percpu_address(unsigned long addr);
Tejun Heof2a82052009-02-20 16:29:08 +0900119
Tejun Heobbddff02010-09-03 18:22:48 +0200120#if !defined(CONFIG_SMP) || !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
Tejun Heoe74e3962009-03-30 19:07:44 +0900121extern void __init setup_per_cpu_areas(void);
122#endif
Tejun Heo099a19d2010-06-27 18:50:00 +0200123extern void __init percpu_init_late(void);
Tejun Heoe74e3962009-03-30 19:07:44 +0900124
Tejun Heode380b52010-03-24 17:06:43 +0900125extern void __percpu *__alloc_percpu(size_t size, size_t align);
126extern void free_percpu(void __percpu *__pdata);
127extern phys_addr_t per_cpu_ptr_to_phys(void *addr);
128
Tejun Heo64ef2912009-10-29 22:34:12 +0900129#define alloc_percpu(type) \
Rusty Russelle0fdb0e2009-10-29 22:34:15 +0900130 (typeof(type) __percpu *)__alloc_percpu(sizeof(type), __alignof__(type))
Tejun Heof2a82052009-02-20 16:29:08 +0900131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#endif /* __LINUX_PERCPU_H */