xref: /openbmc/linux/arch/powerpc/kernel/paca.c (revision a9a08845)
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 
10 #include <linux/smp.h>
11 #include <linux/export.h>
12 #include <linux/memblock.h>
13 #include <linux/sched/task.h>
14 
15 #include <asm/lppaca.h>
16 #include <asm/paca.h>
17 #include <asm/sections.h>
18 #include <asm/pgtable.h>
19 #include <asm/kexec.h>
20 
21 #include "setup.h"
22 
23 #ifdef CONFIG_PPC_BOOK3S
24 
25 /*
26  * The structure which the hypervisor knows about - this structure
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
29  * boundary.  The lppaca is also used on POWER5 pSeries boxes.
30  * The lppaca is 640 bytes long, and cannot readily
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.
33  */
34 struct lppaca lppaca[] = {
35 	[0 ... (NR_LPPACAS-1)] = {
36 		.desc = cpu_to_be32(0xd397d781),	/* "LpPa" */
37 		.size = cpu_to_be16(sizeof(struct lppaca)),
38 		.fpregs_in_use = 1,
39 		.slb_count = cpu_to_be16(64),
40 		.vmxregs_in_use = 0,
41 		.page_ins = 0,
42 	},
43 };
44 
45 static struct lppaca *extra_lppacas;
46 static long __initdata lppaca_size;
47 
48 static void __init allocate_lppacas(int nr_cpus, unsigned long limit)
49 {
50 	if (nr_cpus <= NR_LPPACAS)
51 		return;
52 
53 	lppaca_size = PAGE_ALIGN(sizeof(struct lppaca) *
54 				 (nr_cpus - NR_LPPACAS));
55 	extra_lppacas = __va(memblock_alloc_base(lppaca_size,
56 						 PAGE_SIZE, limit));
57 }
58 
59 static struct lppaca * __init new_lppaca(int cpu)
60 {
61 	struct lppaca *lp;
62 
63 	if (cpu < NR_LPPACAS)
64 		return &lppaca[cpu];
65 
66 	lp = extra_lppacas + (cpu - NR_LPPACAS);
67 	*lp = lppaca[0];
68 
69 	return lp;
70 }
71 
72 static void __init free_lppacas(void)
73 {
74 	long new_size = 0, nr;
75 
76 	if (!lppaca_size)
77 		return;
78 	nr = num_possible_cpus() - NR_LPPACAS;
79 	if (nr > 0)
80 		new_size = PAGE_ALIGN(nr * sizeof(struct lppaca));
81 	if (new_size >= lppaca_size)
82 		return;
83 
84 	memblock_free(__pa(extra_lppacas) + new_size, lppaca_size - new_size);
85 	lppaca_size = new_size;
86 }
87 
88 #else
89 
90 static inline void allocate_lppacas(int nr_cpus, unsigned long limit) { }
91 static inline void free_lppacas(void) { }
92 
93 #endif /* CONFIG_PPC_BOOK3S */
94 
95 #ifdef CONFIG_PPC_BOOK3S_64
96 
97 /*
98  * 3 persistent SLBs are registered here.  The buffer will be zero
99  * initially, hence will all be invaild until we actually write them.
100  *
101  * If you make the number of persistent SLB entries dynamic, please also
102  * update PR KVM to flush and restore them accordingly.
103  */
104 static struct slb_shadow * __initdata slb_shadow;
105 
106 static void __init allocate_slb_shadows(int nr_cpus, int limit)
107 {
108 	int size = PAGE_ALIGN(sizeof(struct slb_shadow) * nr_cpus);
109 
110 	if (early_radix_enabled())
111 		return;
112 
113 	slb_shadow = __va(memblock_alloc_base(size, PAGE_SIZE, limit));
114 	memset(slb_shadow, 0, size);
115 }
116 
117 static struct slb_shadow * __init init_slb_shadow(int cpu)
118 {
119 	struct slb_shadow *s;
120 
121 	if (early_radix_enabled())
122 		return NULL;
123 
124 	s = &slb_shadow[cpu];
125 
126 	/*
127 	 * When we come through here to initialise boot_paca, the slb_shadow
128 	 * buffers are not allocated yet. That's OK, we'll get one later in
129 	 * boot, but make sure we don't corrupt memory at 0.
130 	 */
131 	if (!slb_shadow)
132 		return NULL;
133 
134 	s->persistent = cpu_to_be32(SLB_NUM_BOLTED);
135 	s->buffer_length = cpu_to_be32(sizeof(*s));
136 
137 	return s;
138 }
139 
140 #else /* !CONFIG_PPC_BOOK3S_64 */
141 
142 static void __init allocate_slb_shadows(int nr_cpus, int limit) { }
143 
144 #endif /* CONFIG_PPC_BOOK3S_64 */
145 
146 /* The Paca is an array with one entry per processor.  Each contains an
147  * lppaca, which contains the information shared between the
148  * hypervisor and Linux.
149  * On systems with hardware multi-threading, there are two threads
150  * per processor.  The Paca array must contain an entry for each thread.
151  * The VPD Areas will give a max logical processors = 2 * max physical
152  * processors.  The processor VPD array needs one entry per physical
153  * processor (not thread).
154  */
155 struct paca_struct *paca;
156 EXPORT_SYMBOL(paca);
157 
158 void __init initialise_paca(struct paca_struct *new_paca, int cpu)
159 {
160 #ifdef CONFIG_PPC_BOOK3S
161 	new_paca->lppaca_ptr = new_lppaca(cpu);
162 #else
163 	new_paca->kernel_pgd = swapper_pg_dir;
164 #endif
165 	new_paca->lock_token = 0x8000;
166 	new_paca->paca_index = cpu;
167 	new_paca->kernel_toc = kernel_toc_addr();
168 	new_paca->kernelbase = (unsigned long) _stext;
169 	/* Only set MSR:IR/DR when MMU is initialized */
170 	new_paca->kernel_msr = MSR_KERNEL & ~(MSR_IR | MSR_DR);
171 	new_paca->hw_cpu_id = 0xffff;
172 	new_paca->kexec_state = KEXEC_STATE_NONE;
173 	new_paca->__current = &init_task;
174 	new_paca->data_offset = 0xfeeeeeeeeeeeeeeeULL;
175 #ifdef CONFIG_PPC_BOOK3S_64
176 	new_paca->slb_shadow_ptr = init_slb_shadow(cpu);
177 #endif
178 
179 #ifdef CONFIG_PPC_BOOK3E
180 	/* For now -- if we have threads this will be adjusted later */
181 	new_paca->tcd_ptr = &new_paca->tcd;
182 #endif
183 }
184 
185 /* Put the paca pointer into r13 and SPRG_PACA */
186 void setup_paca(struct paca_struct *new_paca)
187 {
188 	/* Setup r13 */
189 	local_paca = new_paca;
190 
191 #ifdef CONFIG_PPC_BOOK3E
192 	/* On Book3E, initialize the TLB miss exception frames */
193 	mtspr(SPRN_SPRG_TLB_EXFRAME, local_paca->extlb);
194 #else
195 	/* In HV mode, we setup both HPACA and PACA to avoid problems
196 	 * if we do a GET_PACA() before the feature fixups have been
197 	 * applied
198 	 */
199 	if (early_cpu_has_feature(CPU_FTR_HVMODE))
200 		mtspr(SPRN_SPRG_HPACA, local_paca);
201 #endif
202 	mtspr(SPRN_SPRG_PACA, local_paca);
203 
204 }
205 
206 static int __initdata paca_size;
207 
208 void __init allocate_pacas(void)
209 {
210 	u64 limit;
211 	int cpu;
212 
213 #ifdef CONFIG_PPC_BOOK3S_64
214 	/*
215 	 * We access pacas in real mode, and cannot take SLB faults
216 	 * on them when in virtual mode, so allocate them accordingly.
217 	 */
218 	limit = min(ppc64_bolted_size(), ppc64_rma_size);
219 #else
220 	limit = ppc64_rma_size;
221 #endif
222 
223 	paca_size = PAGE_ALIGN(sizeof(struct paca_struct) * nr_cpu_ids);
224 
225 	paca = __va(memblock_alloc_base(paca_size, PAGE_SIZE, limit));
226 	memset(paca, 0, paca_size);
227 
228 	printk(KERN_DEBUG "Allocated %u bytes for %u pacas at %p\n",
229 		paca_size, nr_cpu_ids, paca);
230 
231 	allocate_lppacas(nr_cpu_ids, limit);
232 
233 	allocate_slb_shadows(nr_cpu_ids, limit);
234 
235 	/* Can't use for_each_*_cpu, as they aren't functional yet */
236 	for (cpu = 0; cpu < nr_cpu_ids; cpu++)
237 		initialise_paca(&paca[cpu], cpu);
238 }
239 
240 void __init free_unused_pacas(void)
241 {
242 	int new_size;
243 
244 	new_size = PAGE_ALIGN(sizeof(struct paca_struct) * nr_cpu_ids);
245 
246 	if (new_size >= paca_size)
247 		return;
248 
249 	memblock_free(__pa(paca) + new_size, paca_size - new_size);
250 
251 	printk(KERN_DEBUG "Freed %u bytes for unused pacas\n",
252 		paca_size - new_size);
253 
254 	paca_size = new_size;
255 
256 	free_lppacas();
257 }
258 
259 void copy_mm_to_paca(struct mm_struct *mm)
260 {
261 #ifdef CONFIG_PPC_BOOK3S
262 	mm_context_t *context = &mm->context;
263 
264 	get_paca()->mm_ctx_id = context->id;
265 #ifdef CONFIG_PPC_MM_SLICES
266 	VM_BUG_ON(!mm->context.slb_addr_limit);
267 	get_paca()->mm_ctx_slb_addr_limit = mm->context.slb_addr_limit;
268 	get_paca()->mm_ctx_low_slices_psize = context->low_slices_psize;
269 	memcpy(&get_paca()->mm_ctx_high_slices_psize,
270 	       &context->high_slices_psize, TASK_SLICE_ARRAY_SZ(mm));
271 #else /* CONFIG_PPC_MM_SLICES */
272 	get_paca()->mm_ctx_user_psize = context->user_psize;
273 	get_paca()->mm_ctx_sllp = context->sllp;
274 #endif
275 #else /* !CONFIG_PPC_BOOK3S */
276 	return;
277 #endif
278 }
279