blob: caebf2a758dc0e573d7e5dc76b916dc7cb639ea3 [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>
Steven Rostedt (Red Hat)04b74b22014-11-21 09:16:58 -05008#include <linux/printk.h>
Tejun Heo6a242902009-03-06 14:33:58 +09009#include <linux/pfn.h>
Tejun Heode380b52010-03-24 17:06:43 +090010#include <linux/init.h>
Martin Peschke7ff6f082006-09-25 23:31:21 -070011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/percpu.h>
13
Tejun Heo6a242902009-03-06 14:33:58 +090014/* enough to cover all DEFINE_PER_CPUs in modules */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +020015#ifdef CONFIG_MODULES
Tejun Heo6a242902009-03-06 14:33:58 +090016#define PERCPU_MODULE_RESERVE (8 << 10)
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +020017#else
Tejun Heo6a242902009-03-06 14:33:58 +090018#define PERCPU_MODULE_RESERVE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#endif
20
Tejun Heo6a242902009-03-06 14:33:58 +090021#ifndef PERCPU_ENOUGH_ROOM
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +020022#define PERCPU_ENOUGH_ROOM \
Tejun Heo6a242902009-03-06 14:33:58 +090023 (ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES) + \
24 PERCPU_MODULE_RESERVE)
25#endif
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +020026
Tejun Heo8d408b42009-02-24 11:57:21 +090027/* minimum unit size, also is the maximum supported allocation size */
Tejun Heo6abad5a2010-09-03 18:22:47 +020028#define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
Tejun Heo8d408b42009-02-24 11:57:21 +090029
30/*
Tejun Heo099a19d2010-06-27 18:50:00 +020031 * Percpu allocator can serve percpu allocations before slab is
32 * initialized which allows slab to depend on the percpu allocator.
33 * The following two parameters decide how much resource to
34 * preallocate for this. Keep PERCPU_DYNAMIC_RESERVE equal to or
35 * larger than PERCPU_DYNAMIC_EARLY_SIZE.
36 */
37#define PERCPU_DYNAMIC_EARLY_SLOTS 128
38#define PERCPU_DYNAMIC_EARLY_SIZE (12 << 10)
39
40/*
Tejun Heo8d408b42009-02-24 11:57:21 +090041 * PERCPU_DYNAMIC_RESERVE indicates the amount of free area to piggy
Tejun Heo6b19b0c2009-03-06 14:33:59 +090042 * back on the first chunk for dynamic percpu allocation if arch is
43 * manually allocating and mapping it for faster access (as a part of
44 * large page mapping for example).
Tejun Heo8d408b42009-02-24 11:57:21 +090045 *
Tejun Heo6b19b0c2009-03-06 14:33:59 +090046 * The following values give between one and two pages of free space
47 * after typical minimal boot (2-way SMP, single disk and NIC) with
48 * both defconfig and a distro config on x86_64 and 32. More
49 * intelligent way to determine this would be nice.
Tejun Heo8d408b42009-02-24 11:57:21 +090050 */
Tejun Heo6b19b0c2009-03-06 14:33:59 +090051#if BITS_PER_LONG > 32
Tejun Heo1a4d7602014-09-02 14:46:05 -040052#define PERCPU_DYNAMIC_RESERVE (28 << 10)
Tejun Heo6b19b0c2009-03-06 14:33:59 +090053#else
Tejun Heo1a4d7602014-09-02 14:46:05 -040054#define PERCPU_DYNAMIC_RESERVE (20 << 10)
Tejun Heo6b19b0c2009-03-06 14:33:59 +090055#endif
Tejun Heo8d408b42009-02-24 11:57:21 +090056
Tejun Heofbf59bc2009-02-20 16:29:08 +090057extern void *pcpu_base_addr;
Tejun Heofb435d52009-08-14 15:00:51 +090058extern const unsigned long *pcpu_unit_offsets;
Tejun Heofbf59bc2009-02-20 16:29:08 +090059
Tejun Heofd1e8a12009-08-14 15:00:51 +090060struct pcpu_group_info {
61 int nr_units; /* aligned # of units */
62 unsigned long base_offset; /* base address offset */
63 unsigned int *cpu_map; /* unit->cpu map, empty
64 * entries contain NR_CPUS */
65};
66
67struct pcpu_alloc_info {
68 size_t static_size;
69 size_t reserved_size;
70 size_t dyn_size;
71 size_t unit_size;
72 size_t atom_size;
73 size_t alloc_size;
74 size_t __ai_size; /* internal, don't use */
75 int nr_groups; /* 0 if grouping unnecessary */
76 struct pcpu_group_info groups[];
77};
78
Tejun Heof58dc012009-08-14 15:00:50 +090079enum pcpu_fc {
80 PCPU_FC_AUTO,
81 PCPU_FC_EMBED,
82 PCPU_FC_PAGE,
Tejun Heof58dc012009-08-14 15:00:50 +090083
84 PCPU_FC_NR,
85};
Andi Kleen17f36092012-10-04 17:12:07 -070086extern const char * const pcpu_fc_names[PCPU_FC_NR];
Tejun Heof58dc012009-08-14 15:00:50 +090087
88extern enum pcpu_fc pcpu_chosen_fc;
89
Tejun Heo3cbc8562009-08-14 15:00:50 +090090typedef void * (*pcpu_fc_alloc_fn_t)(unsigned int cpu, size_t size,
91 size_t align);
Tejun Heod4b95f82009-07-04 08:10:59 +090092typedef void (*pcpu_fc_free_fn_t)(void *ptr, size_t size);
93typedef void (*pcpu_fc_populate_pte_fn_t)(unsigned long addr);
Tejun Heoa530b792009-07-04 08:11:00 +090094typedef int (pcpu_fc_cpu_distance_fn_t)(unsigned int from, unsigned int to);
Tejun Heofbf59bc2009-02-20 16:29:08 +090095
Tejun Heofd1e8a12009-08-14 15:00:51 +090096extern struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
97 int nr_units);
98extern void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai);
Tejun Heo033e48f2009-08-14 15:00:51 +090099
Tejun Heofb435d52009-08-14 15:00:51 +0900100extern int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
101 void *base_addr);
Tejun Heo8d408b42009-02-24 11:57:21 +0900102
Tejun Heo08fc4582009-08-14 15:00:49 +0900103#ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
Tejun Heo4ba6ce22010-06-27 18:49:59 +0200104extern int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
Tejun Heoc8826dd2009-08-14 15:00:52 +0900105 size_t atom_size,
106 pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
107 pcpu_fc_alloc_fn_t alloc_fn,
108 pcpu_fc_free_fn_t free_fn);
Tejun Heo08fc4582009-08-14 15:00:49 +0900109#endif
Tejun Heo66c3a752009-03-10 16:27:48 +0900110
Tejun Heo08fc4582009-08-14 15:00:49 +0900111#ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
Tejun Heofb435d52009-08-14 15:00:51 +0900112extern int __init pcpu_page_first_chunk(size_t reserved_size,
Tejun Heod4b95f82009-07-04 08:10:59 +0900113 pcpu_fc_alloc_fn_t alloc_fn,
114 pcpu_fc_free_fn_t free_fn,
115 pcpu_fc_populate_pte_fn_t populate_pte_fn);
Tejun Heo08fc4582009-08-14 15:00:49 +0900116#endif
Tejun Heod4b95f82009-07-04 08:10:59 +0900117
Rusty Russelle0fdb0e2009-10-29 22:34:15 +0900118extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align);
Tejun Heo10fad5e2010-03-10 18:57:54 +0900119extern bool is_kernel_percpu_address(unsigned long addr);
Tejun Heof2a82052009-02-20 16:29:08 +0900120
Tejun Heobbddff02010-09-03 18:22:48 +0200121#if !defined(CONFIG_SMP) || !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
Tejun Heoe74e3962009-03-30 19:07:44 +0900122extern void __init setup_per_cpu_areas(void);
123#endif
Tejun Heo099a19d2010-06-27 18:50:00 +0200124extern void __init percpu_init_late(void);
Tejun Heoe74e3962009-03-30 19:07:44 +0900125
Tejun Heo5835d962014-09-02 14:46:04 -0400126extern void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp);
Tejun Heode380b52010-03-24 17:06:43 +0900127extern void __percpu *__alloc_percpu(size_t size, size_t align);
128extern void free_percpu(void __percpu *__pdata);
129extern phys_addr_t per_cpu_ptr_to_phys(void *addr);
130
Tejun Heo5835d962014-09-02 14:46:04 -0400131#define alloc_percpu_gfp(type, gfp) \
132 (typeof(type) __percpu *)__alloc_percpu_gfp(sizeof(type), \
133 __alignof__(type), gfp)
134#define alloc_percpu(type) \
135 (typeof(type) __percpu *)__alloc_percpu(sizeof(type), \
136 __alignof__(type))
Tejun Heof2a82052009-02-20 16:29:08 +0900137
Steven Rostedt (Red Hat)afdc34a2014-06-19 17:33:31 -0400138/* To avoid include hell, as printk can not declare this, we declare it here */
139DECLARE_PER_CPU(printk_func_t, printk_func);
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#endif /* __LINUX_PERCPU_H */