1 // SPDX-License-Identifier: GPL-2.0-only
3 #define pr_fmt(fmt) "rtas-work-area: " fmt
5 #include <linux/genalloc.h>
6 #include <linux/log2.h>
7 #include <linux/kernel.h>
8 #include <linux/memblock.h>
9 #include <linux/mempool.h>
10 #include <linux/minmax.h>
11 #include <linux/mutex.h>
12 #include <linux/numa.h>
13 #include <linux/sizes.h>
14 #include <linux/wait.h>
16 #include <asm/machdep.h>
17 #include <asm/rtas-work-area.h>
22 * Ensure the pool is page-aligned.
24 RTAS_WORK_AREA_ARENA_ALIGN = PAGE_SIZE,
26 * Don't let a single allocation claim the whole arena.
28 RTAS_WORK_AREA_ARENA_SZ = RTAS_WORK_AREA_MAX_ALLOC_SZ * 2,
30 * The smallest known work area size is for ibm,get-vpd's
31 * location code argument, which is limited to 79 characters
32 * plus 1 nul terminator.
34 * PAPR+ 7.3.20 ibm,get-vpd RTAS Call
35 * PAPR+ 12.3.2.4 Converged Location Code Rules - Length Restrictions
37 RTAS_WORK_AREA_MIN_ALLOC_SZ = roundup_pow_of_two(80),
41 struct gen_pool *gen_pool;
43 struct mutex mutex; /* serializes allocations */
44 struct wait_queue_head wqh;
45 mempool_t descriptor_pool;
48 .mutex = __MUTEX_INITIALIZER(rwa_state.mutex),
49 .wqh = __WAIT_QUEUE_HEAD_INITIALIZER(rwa_state.wqh),
53 * A single work area buffer and descriptor to serve requests early in
54 * boot before the allocator is fully initialized. We know 4KB is the
55 * most any boot time user needs (they all call ibm,get-system-parameter).
57 static bool early_work_area_in_use __initdata;
58 static char early_work_area_buf[SZ_4K] __initdata __aligned(SZ_4K);
59 static struct rtas_work_area early_work_area __initdata = {
60 .buf = early_work_area_buf,
61 .size = sizeof(early_work_area_buf),
65 static struct rtas_work_area * __init rtas_work_area_alloc_early(size_t size)
67 WARN_ON(size > early_work_area.size);
68 WARN_ON(early_work_area_in_use);
69 early_work_area_in_use = true;
70 memset(early_work_area.buf, 0, early_work_area.size);
71 return &early_work_area;
74 static void __init rtas_work_area_free_early(struct rtas_work_area *work_area)
76 WARN_ON(work_area != &early_work_area);
77 WARN_ON(!early_work_area_in_use);
78 early_work_area_in_use = false;
81 struct rtas_work_area * __ref __rtas_work_area_alloc(size_t size)
83 struct rtas_work_area *area;
89 * The rtas_work_area_alloc() wrapper enforces this at build
90 * time. Requests that exceed the arena size will block
93 WARN_ON(size > RTAS_WORK_AREA_MAX_ALLOC_SZ);
95 if (!rwa_state.available)
96 return rtas_work_area_alloc_early(size);
98 * To ensure FCFS behavior and prevent a high rate of smaller
99 * requests from starving larger ones, use the mutex to queue
102 mutex_lock(&rwa_state.mutex);
103 wait_event(rwa_state.wqh,
104 (addr = gen_pool_alloc(rwa_state.gen_pool, size)) != 0);
105 mutex_unlock(&rwa_state.mutex);
107 area = mempool_alloc(&rwa_state.descriptor_pool, GFP_KERNEL);
108 area->buf = (char *)addr;
114 void __ref rtas_work_area_free(struct rtas_work_area *area)
116 if (!rwa_state.available) {
117 rtas_work_area_free_early(area);
121 gen_pool_free(rwa_state.gen_pool, (unsigned long)area->buf, area->size);
122 mempool_free(area, &rwa_state.descriptor_pool);
123 wake_up(&rwa_state.wqh);
127 * Initialization of the work area allocator happens in two parts. To
128 * reliably reserve an arena that satisfies RTAS addressing
129 * requirements, we must perform a memblock allocation early,
130 * immmediately after RTAS instantiation. Then we have to wait until
131 * the slab allocator is up before setting up the descriptor mempool
132 * and adding the arena to a gen_pool.
134 static __init int rtas_work_area_allocator_init(void)
136 const unsigned int order = ilog2(RTAS_WORK_AREA_MIN_ALLOC_SZ);
137 const phys_addr_t pa_start = __pa(rwa_state.arena);
138 const phys_addr_t pa_end = pa_start + RTAS_WORK_AREA_ARENA_SZ - 1;
139 struct gen_pool *pool;
140 const int nid = NUMA_NO_NODE;
144 if (!rwa_state.arena)
147 pool = gen_pool_create(order, nid);
151 * All RTAS functions that consume work areas are OK with
152 * natural alignment, when they have alignment requirements at
155 gen_pool_set_algo(pool, gen_pool_first_fit_order_align, NULL);
157 err = gen_pool_add(pool, (unsigned long)rwa_state.arena,
158 RTAS_WORK_AREA_ARENA_SZ, nid);
162 err = mempool_init_kmalloc_pool(&rwa_state.descriptor_pool, 1,
163 sizeof(struct rtas_work_area));
167 rwa_state.gen_pool = pool;
168 rwa_state.available = true;
170 pr_debug("arena [%pa-%pa] (%uK), min/max alloc sizes %u/%u\n",
172 RTAS_WORK_AREA_ARENA_SZ / SZ_1K,
173 RTAS_WORK_AREA_MIN_ALLOC_SZ,
174 RTAS_WORK_AREA_MAX_ALLOC_SZ);
179 gen_pool_destroy(pool);
183 machine_arch_initcall(pseries, rtas_work_area_allocator_init);
186 * rtas_work_area_reserve_arena() - Reserve memory suitable for RTAS work areas.
188 void __init rtas_work_area_reserve_arena(const phys_addr_t limit)
190 const phys_addr_t align = RTAS_WORK_AREA_ARENA_ALIGN;
191 const phys_addr_t size = RTAS_WORK_AREA_ARENA_SZ;
192 const phys_addr_t min = MEMBLOCK_LOW_LIMIT;
193 const int nid = NUMA_NO_NODE;
196 * Too early for a machine_is(pseries) check. But PAPR
197 * effectively mandates that ibm,get-system-parameter is
200 * R1–7.3.16–1. All platforms must support the System
203 * So set up the arena if we find that, with a fallback to
204 * ibm,configure-connector, just in case.
206 if (rtas_function_implemented(RTAS_FN_IBM_GET_SYSTEM_PARAMETER) ||
207 rtas_function_implemented(RTAS_FN_IBM_CONFIGURE_CONNECTOR))
208 rwa_state.arena = memblock_alloc_try_nid(size, align, min, limit, nid);