1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * c 2001 PPC 64 Team, IBM Corp
7 #include <linux/export.h>
8 #include <linux/memblock.h>
9 #include <linux/sched/task.h>
10 #include <linux/numa.h>
12 #include <asm/lppaca.h>
14 #include <asm/sections.h>
15 #include <asm/pgtable.h>
16 #include <asm/kexec.h>
24 static void *__init alloc_paca_data(unsigned long size, unsigned long align,
25 unsigned long limit, int cpu)
31 * boot_cpuid paca is allocated very early before cpu_to_node is up.
32 * Set bottom-up mode, because the boot CPU should be on node-0,
33 * which will put its paca in the right place.
35 if (cpu == boot_cpuid) {
37 memblock_set_bottom_up(true);
39 nid = early_cpu_to_node(cpu);
42 ptr = memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
45 panic("cannot allocate paca data");
47 if (cpu == boot_cpuid)
48 memblock_set_bottom_up(false);
53 #ifdef CONFIG_PPC_PSERIES
56 * See asm/lppaca.h for more detail.
58 * lppaca structures must must be 1kB in size, L1 cache line aligned,
59 * and not cross 4kB boundary. A 1kB size and 1kB alignment will satisfy
62 static inline void init_lppaca(struct lppaca *lppaca)
64 BUILD_BUG_ON(sizeof(struct lppaca) != 640);
66 *lppaca = (struct lppaca) {
67 .desc = cpu_to_be32(0xd397d781), /* "LpPa" */
68 .size = cpu_to_be16(0x400),
70 .slb_count = cpu_to_be16(64),
75 static struct lppaca * __init new_lppaca(int cpu, unsigned long limit)
80 BUILD_BUG_ON(size < sizeof(struct lppaca));
82 if (early_cpu_has_feature(CPU_FTR_HVMODE))
85 lp = alloc_paca_data(size, 0x400, limit, cpu);
90 #endif /* CONFIG_PPC_BOOK3S */
92 #ifdef CONFIG_PPC_BOOK3S_64
95 * 3 persistent SLBs are allocated here. The buffer will be zero
96 * initially, hence will all be invaild until we actually write them.
98 * If you make the number of persistent SLB entries dynamic, please also
99 * update PR KVM to flush and restore them accordingly.
101 static struct slb_shadow * __init new_slb_shadow(int cpu, unsigned long limit)
103 struct slb_shadow *s;
105 if (cpu != boot_cpuid) {
107 * Boot CPU comes here before early_radix_enabled
108 * is parsed (e.g., for disable_radix). So allocate
109 * always and this will be fixed up in free_unused_pacas.
111 if (early_radix_enabled())
115 s = alloc_paca_data(sizeof(*s), L1_CACHE_BYTES, limit, cpu);
117 s->persistent = cpu_to_be32(SLB_NUM_BOLTED);
118 s->buffer_length = cpu_to_be32(sizeof(*s));
123 #endif /* CONFIG_PPC_BOOK3S_64 */
125 /* The Paca is an array with one entry per processor. Each contains an
126 * lppaca, which contains the information shared between the
127 * hypervisor and Linux.
128 * On systems with hardware multi-threading, there are two threads
129 * per processor. The Paca array must contain an entry for each thread.
130 * The VPD Areas will give a max logical processors = 2 * max physical
131 * processors. The processor VPD array needs one entry per physical
132 * processor (not thread).
134 struct paca_struct **paca_ptrs __read_mostly;
135 EXPORT_SYMBOL(paca_ptrs);
137 void __init initialise_paca(struct paca_struct *new_paca, int cpu)
139 #ifdef CONFIG_PPC_PSERIES
140 new_paca->lppaca_ptr = NULL;
142 #ifdef CONFIG_PPC_BOOK3E
143 new_paca->kernel_pgd = swapper_pg_dir;
145 new_paca->lock_token = 0x8000;
146 new_paca->paca_index = cpu;
147 new_paca->kernel_toc = kernel_toc_addr();
148 new_paca->kernelbase = (unsigned long) _stext;
149 /* Only set MSR:IR/DR when MMU is initialized */
150 new_paca->kernel_msr = MSR_KERNEL & ~(MSR_IR | MSR_DR);
151 new_paca->hw_cpu_id = 0xffff;
152 new_paca->kexec_state = KEXEC_STATE_NONE;
153 new_paca->__current = &init_task;
154 new_paca->data_offset = 0xfeeeeeeeeeeeeeeeULL;
155 #ifdef CONFIG_PPC_BOOK3S_64
156 new_paca->slb_shadow_ptr = NULL;
159 #ifdef CONFIG_PPC_BOOK3E
160 /* For now -- if we have threads this will be adjusted later */
161 new_paca->tcd_ptr = &new_paca->tcd;
165 /* Put the paca pointer into r13 and SPRG_PACA */
166 void setup_paca(struct paca_struct *new_paca)
169 local_paca = new_paca;
171 #ifdef CONFIG_PPC_BOOK3E
172 /* On Book3E, initialize the TLB miss exception frames */
173 mtspr(SPRN_SPRG_TLB_EXFRAME, local_paca->extlb);
175 /* In HV mode, we setup both HPACA and PACA to avoid problems
176 * if we do a GET_PACA() before the feature fixups have been
179 if (early_cpu_has_feature(CPU_FTR_HVMODE))
180 mtspr(SPRN_SPRG_HPACA, local_paca);
182 mtspr(SPRN_SPRG_PACA, local_paca);
186 static int __initdata paca_nr_cpu_ids;
187 static int __initdata paca_ptrs_size;
188 static int __initdata paca_struct_size;
190 void __init allocate_paca_ptrs(void)
192 paca_nr_cpu_ids = nr_cpu_ids;
194 paca_ptrs_size = sizeof(struct paca_struct *) * nr_cpu_ids;
195 paca_ptrs = memblock_alloc_raw(paca_ptrs_size, SMP_CACHE_BYTES);
197 panic("Failed to allocate %d bytes for paca pointers\n",
200 memset(paca_ptrs, 0x88, paca_ptrs_size);
203 void __init allocate_paca(int cpu)
206 struct paca_struct *paca;
208 BUG_ON(cpu >= paca_nr_cpu_ids);
210 #ifdef CONFIG_PPC_BOOK3S_64
212 * We access pacas in real mode, and cannot take SLB faults
213 * on them when in virtual mode, so allocate them accordingly.
215 limit = min(ppc64_bolted_size(), ppc64_rma_size);
217 limit = ppc64_rma_size;
220 paca = alloc_paca_data(sizeof(struct paca_struct), L1_CACHE_BYTES,
222 paca_ptrs[cpu] = paca;
224 initialise_paca(paca, cpu);
225 #ifdef CONFIG_PPC_PSERIES
226 paca->lppaca_ptr = new_lppaca(cpu, limit);
228 #ifdef CONFIG_PPC_BOOK3S_64
229 paca->slb_shadow_ptr = new_slb_shadow(cpu, limit);
231 paca_struct_size += sizeof(struct paca_struct);
234 void __init free_unused_pacas(void)
238 new_ptrs_size = sizeof(struct paca_struct *) * nr_cpu_ids;
239 if (new_ptrs_size < paca_ptrs_size)
240 memblock_free(__pa(paca_ptrs) + new_ptrs_size,
241 paca_ptrs_size - new_ptrs_size);
243 paca_nr_cpu_ids = nr_cpu_ids;
244 paca_ptrs_size = new_ptrs_size;
246 #ifdef CONFIG_PPC_BOOK3S_64
247 if (early_radix_enabled()) {
248 /* Ugly fixup, see new_slb_shadow() */
249 memblock_free(__pa(paca_ptrs[boot_cpuid]->slb_shadow_ptr),
250 sizeof(struct slb_shadow));
251 paca_ptrs[boot_cpuid]->slb_shadow_ptr = NULL;
255 printk(KERN_DEBUG "Allocated %u bytes for %u pacas\n",
256 paca_ptrs_size + paca_struct_size, nr_cpu_ids);
259 void copy_mm_to_paca(struct mm_struct *mm)
261 #ifdef CONFIG_PPC_BOOK3S
262 mm_context_t *context = &mm->context;
264 get_paca()->mm_ctx_id = context->id;
265 #ifdef CONFIG_PPC_MM_SLICES
266 VM_BUG_ON(!mm_ctx_slb_addr_limit(context));
267 get_paca()->mm_ctx_slb_addr_limit = mm_ctx_slb_addr_limit(context);
268 memcpy(&get_paca()->mm_ctx_low_slices_psize, mm_ctx_low_slices(context),
270 memcpy(&get_paca()->mm_ctx_high_slices_psize, mm_ctx_high_slices(context),
271 TASK_SLICE_ARRAY_SZ(context));
272 #else /* CONFIG_PPC_MM_SLICES */
273 get_paca()->mm_ctx_user_psize = context->user_psize;
274 get_paca()->mm_ctx_sllp = context->sllp;
276 #else /* !CONFIG_PPC_BOOK3S */