]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - arch/powerpc/kernel/paca.c
powerpc/64: Use array of paca pointers and allocate pacas individually
[mirror_ubuntu-focal-kernel.git] / arch / powerpc / kernel / paca.c
CommitLineData
1da177e4
LT
1/*
2 * c 2001 PPC 64 Team, IBM Corp
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9
2cd947f1 10#include <linux/smp.h>
4b16f8e2 11#include <linux/export.h>
95f72d1e 12#include <linux/memblock.h>
9164bb4a 13#include <linux/sched/task.h>
1da177e4 14
1da177e4 15#include <asm/lppaca.h>
1da177e4 16#include <asm/paca.h>
549e8152 17#include <asm/sections.h>
dce6670a 18#include <asm/pgtable.h>
1fc711f7 19#include <asm/kexec.h>
1da177e4 20
1af19331
NP
21#include "setup.h"
22
8e0b634b 23#ifdef CONFIG_PPC_PSERIES
91c60b5b 24
3356bb9f 25/*
30ff2e87 26 * The structure which the hypervisor knows about - this structure
3356bb9f
DG
27 * should not cross a page boundary. The vpa_init/register_vpa call
28 * is now known to fail if the lppaca structure crosses a page
f5339277
SR
29 * boundary. The lppaca is also used on POWER5 pSeries boxes.
30 * The lppaca is 640 bytes long, and cannot readily
30ff2e87
SR
31 * change since the hypervisor knows its layout, so a 1kB alignment
32 * will suffice to ensure that it doesn't cross a page boundary.
3356bb9f
DG
33 */
34struct lppaca lppaca[] = {
93c22703 35 [0 ... (NR_LPPACAS-1)] = {
7ffcf8ec
AB
36 .desc = cpu_to_be32(0xd397d781), /* "LpPa" */
37 .size = cpu_to_be16(sizeof(struct lppaca)),
3356bb9f 38 .fpregs_in_use = 1,
7ffcf8ec 39 .slb_count = cpu_to_be16(64),
3356bb9f 40 .vmxregs_in_use = 0,
40900194 41 .page_ins = 0,
3356bb9f
DG
42 },
43};
44
93c22703
PM
45static struct lppaca *extra_lppacas;
46static long __initdata lppaca_size;
47
8616dff5 48static void __init allocate_lppacas(int nr_cpus, unsigned long limit)
93c22703 49{
8e0b634b
NP
50 if (early_cpu_has_feature(CPU_FTR_HVMODE))
51 return;
52
93c22703
PM
53 if (nr_cpus <= NR_LPPACAS)
54 return;
55
56 lppaca_size = PAGE_ALIGN(sizeof(struct lppaca) *
57 (nr_cpus - NR_LPPACAS));
58 extra_lppacas = __va(memblock_alloc_base(lppaca_size,
59 PAGE_SIZE, limit));
60}
61
8616dff5 62static struct lppaca * __init new_lppaca(int cpu)
93c22703
PM
63{
64 struct lppaca *lp;
65
8e0b634b
NP
66 if (early_cpu_has_feature(CPU_FTR_HVMODE))
67 return NULL;
68
93c22703
PM
69 if (cpu < NR_LPPACAS)
70 return &lppaca[cpu];
71
72 lp = extra_lppacas + (cpu - NR_LPPACAS);
73 *lp = lppaca[0];
74
75 return lp;
76}
77
8616dff5 78static void __init free_lppacas(void)
93c22703
PM
79{
80 long new_size = 0, nr;
81
8e0b634b
NP
82 if (early_cpu_has_feature(CPU_FTR_HVMODE))
83 return;
84
93c22703
PM
85 if (!lppaca_size)
86 return;
87 nr = num_possible_cpus() - NR_LPPACAS;
88 if (nr > 0)
89 new_size = PAGE_ALIGN(nr * sizeof(struct lppaca));
90 if (new_size >= lppaca_size)
91 return;
92
93 memblock_free(__pa(extra_lppacas) + new_size, lppaca_size - new_size);
94 lppaca_size = new_size;
95}
96
97#else
98
3c4b7644 99static inline void allocate_lppacas(int nr_cpus, unsigned long limit) { }
93c22703
PM
100static inline void free_lppacas(void) { }
101
91c60b5b
BH
102#endif /* CONFIG_PPC_BOOK3S */
103
4e003747 104#ifdef CONFIG_PPC_BOOK3S_64
91c60b5b 105
2f6093c8
MN
106/*
107 * 3 persistent SLBs are registered here. The buffer will be zero
108 * initially, hence will all be invaild until we actually write them.
d8d164a9
AG
109 *
110 * If you make the number of persistent SLB entries dynamic, please also
111 * update PR KVM to flush and restore them accordingly.
2f6093c8 112 */
b68b1d74 113static struct slb_shadow * __initdata slb_shadow;
6f4441ef
JK
114
115static void __init allocate_slb_shadows(int nr_cpus, int limit)
116{
117 int size = PAGE_ALIGN(sizeof(struct slb_shadow) * nr_cpus);
b68b1d74
NP
118
119 if (early_radix_enabled())
120 return;
121
6f4441ef
JK
122 slb_shadow = __va(memblock_alloc_base(size, PAGE_SIZE, limit));
123 memset(slb_shadow, 0, size);
124}
125
126static struct slb_shadow * __init init_slb_shadow(int cpu)
127{
b68b1d74
NP
128 struct slb_shadow *s;
129
130 if (early_radix_enabled())
131 return NULL;
132
133 s = &slb_shadow[cpu];
6f4441ef 134
6f20e7f2
GS
135 /*
136 * When we come through here to initialise boot_paca, the slb_shadow
137 * buffers are not allocated yet. That's OK, we'll get one later in
138 * boot, but make sure we don't corrupt memory at 0.
139 */
140 if (!slb_shadow)
141 return NULL;
142
6f4441ef
JK
143 s->persistent = cpu_to_be32(SLB_NUM_BOLTED);
144 s->buffer_length = cpu_to_be32(sizeof(*s));
145
146 return s;
147}
148
4e003747 149#else /* !CONFIG_PPC_BOOK3S_64 */
6f4441ef
JK
150
151static void __init allocate_slb_shadows(int nr_cpus, int limit) { }
2f6093c8 152
4e003747 153#endif /* CONFIG_PPC_BOOK3S_64 */
91c60b5b 154
8882a4da 155/* The Paca is an array with one entry per processor. Each contains an
1da177e4 156 * lppaca, which contains the information shared between the
1888e7b5 157 * hypervisor and Linux.
1da177e4
LT
158 * On systems with hardware multi-threading, there are two threads
159 * per processor. The Paca array must contain an entry for each thread.
160 * The VPD Areas will give a max logical processors = 2 * max physical
161 * processors. The processor VPD array needs one entry per physical
162 * processor (not thread).
163 */
d2e60075
NP
164struct paca_struct **paca_ptrs __read_mostly;
165EXPORT_SYMBOL(paca_ptrs);
90035fe3 166
1426d5a3
ME
167void __init initialise_paca(struct paca_struct *new_paca, int cpu)
168{
8e0b634b 169#ifdef CONFIG_PPC_PSERIES
93c22703 170 new_paca->lppaca_ptr = new_lppaca(cpu);
8e0b634b
NP
171#endif
172#ifdef CONFIG_PPC_BOOK3E
1426d5a3 173 new_paca->kernel_pgd = swapper_pg_dir;
91c60b5b 174#endif
1426d5a3
ME
175 new_paca->lock_token = 0x8000;
176 new_paca->paca_index = cpu;
a5cab83c 177 new_paca->kernel_toc = kernel_toc_addr();
1426d5a3 178 new_paca->kernelbase = (unsigned long) _stext;
a944a9c4
BH
179 /* Only set MSR:IR/DR when MMU is initialized */
180 new_paca->kernel_msr = MSR_KERNEL & ~(MSR_IR | MSR_DR);
1426d5a3 181 new_paca->hw_cpu_id = 0xffff;
1fc711f7 182 new_paca->kexec_state = KEXEC_STATE_NONE;
1426d5a3 183 new_paca->__current = &init_task;
407821a3 184 new_paca->data_offset = 0xfeeeeeeeeeeeeeeeULL;
4e003747 185#ifdef CONFIG_PPC_BOOK3S_64
6f4441ef 186 new_paca->slb_shadow_ptr = init_slb_shadow(cpu);
4e003747 187#endif
28efc35f
SW
188
189#ifdef CONFIG_PPC_BOOK3E
190 /* For now -- if we have threads this will be adjusted later */
191 new_paca->tcd_ptr = &new_paca->tcd;
192#endif
1426d5a3
ME
193}
194
fc53b420
ME
195/* Put the paca pointer into r13 and SPRG_PACA */
196void setup_paca(struct paca_struct *new_paca)
197{
2dd60d79 198 /* Setup r13 */
fc53b420 199 local_paca = new_paca;
2dd60d79 200
fc53b420 201#ifdef CONFIG_PPC_BOOK3E
2dd60d79 202 /* On Book3E, initialize the TLB miss exception frames */
fc53b420 203 mtspr(SPRN_SPRG_TLB_EXFRAME, local_paca->extlb);
2dd60d79
BH
204#else
205 /* In HV mode, we setup both HPACA and PACA to avoid problems
206 * if we do a GET_PACA() before the feature fixups have been
207 * applied
208 */
b8f1b4f8 209 if (early_cpu_has_feature(CPU_FTR_HVMODE))
2dd60d79 210 mtspr(SPRN_SPRG_HPACA, local_paca);
fc53b420 211#endif
2dd60d79
BH
212 mtspr(SPRN_SPRG_PACA, local_paca);
213
fc53b420
ME
214}
215
d2e60075
NP
216static int __initdata paca_nr_cpu_ids;
217static int __initdata paca_ptrs_size;
1426d5a3
ME
218
219void __init allocate_pacas(void)
220{
ecc4999f 221 u64 limit;
d2e60075 222 unsigned long size = 0;
ecc4999f 223 int cpu;
1426d5a3 224
ecc4999f 225#ifdef CONFIG_PPC_BOOK3S_64
1426d5a3 226 /*
1af19331
NP
227 * We access pacas in real mode, and cannot take SLB faults
228 * on them when in virtual mode, so allocate them accordingly.
1426d5a3 229 */
1af19331
NP
230 limit = min(ppc64_bolted_size(), ppc64_rma_size);
231#else
232 limit = ppc64_rma_size;
ecc4999f 233#endif
1426d5a3 234
d2e60075 235 paca_nr_cpu_ids = nr_cpu_ids;
1426d5a3 236
d2e60075
NP
237 paca_ptrs_size = sizeof(struct paca_struct *) * nr_cpu_ids;
238 paca_ptrs = __va(memblock_alloc_base(paca_ptrs_size, 0, limit));
239 memset(paca_ptrs, 0, paca_ptrs_size);
1426d5a3 240
d2e60075
NP
241 size += paca_ptrs_size;
242
243 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
244 unsigned long pa;
245
246 pa = memblock_alloc_base(sizeof(struct paca_struct),
247 L1_CACHE_BYTES, limit);
248 paca_ptrs[cpu] = __va(pa);
249 memset(paca_ptrs[cpu], 0, sizeof(struct paca_struct));
250
251 size += sizeof(struct paca_struct);
252 }
253
254 printk(KERN_DEBUG "Allocated %lu bytes for %u pacas\n",
255 size, nr_cpu_ids);
1426d5a3 256
2cd947f1 257 allocate_lppacas(nr_cpu_ids, limit);
93c22703 258
6f4441ef
JK
259 allocate_slb_shadows(nr_cpu_ids, limit);
260
1426d5a3 261 /* Can't use for_each_*_cpu, as they aren't functional yet */
2cd947f1 262 for (cpu = 0; cpu < nr_cpu_ids; cpu++)
d2e60075 263 initialise_paca(paca_ptrs[cpu], cpu);
1426d5a3
ME
264}
265
266void __init free_unused_pacas(void)
267{
d2e60075
NP
268 unsigned long size = 0;
269 int new_ptrs_size;
270 int cpu;
90035fe3 271
d2e60075
NP
272 for (cpu = 0; cpu < paca_nr_cpu_ids; cpu++) {
273 if (!cpu_possible(cpu)) {
274 unsigned long pa = __pa(paca_ptrs[cpu]);
275 memblock_free(pa, sizeof(struct paca_struct));
276 paca_ptrs[cpu] = NULL;
277 size += sizeof(struct paca_struct);
278 }
279 }
280
281 new_ptrs_size = sizeof(struct paca_struct *) * nr_cpu_ids;
282 if (new_ptrs_size < paca_ptrs_size) {
283 memblock_free(__pa(paca_ptrs) + new_ptrs_size,
284 paca_ptrs_size - new_ptrs_size);
285 size += paca_ptrs_size - new_ptrs_size;
286 }
287
288 if (size)
289 printk(KERN_DEBUG "Freed %lu bytes for unused pacas\n", size);
93c22703
PM
290
291 free_lppacas();
d2e60075
NP
292
293 paca_nr_cpu_ids = nr_cpu_ids;
294 paca_ptrs_size = new_ptrs_size;
90035fe3 295}
52b1e665
AK
296
297void copy_mm_to_paca(struct mm_struct *mm)
298{
299#ifdef CONFIG_PPC_BOOK3S
300 mm_context_t *context = &mm->context;
301
302 get_paca()->mm_ctx_id = context->id;
303#ifdef CONFIG_PPC_MM_SLICES
4722476b
NP
304 VM_BUG_ON(!mm->context.slb_addr_limit);
305 get_paca()->mm_ctx_slb_addr_limit = mm->context.slb_addr_limit;
52b1e665
AK
306 get_paca()->mm_ctx_low_slices_psize = context->low_slices_psize;
307 memcpy(&get_paca()->mm_ctx_high_slices_psize,
957b778a 308 &context->high_slices_psize, TASK_SLICE_ARRAY_SZ(mm));
52b1e665
AK
309#else /* CONFIG_PPC_MM_SLICES */
310 get_paca()->mm_ctx_user_psize = context->user_psize;
311 get_paca()->mm_ctx_sllp = context->sllp;
312#endif
4e003747 313#else /* !CONFIG_PPC_BOOK3S */
52b1e665
AK
314 return;
315#endif
316}