1 /*
2  * Based on arch/arm/kernel/setup.c
3  *
4  * Copyright (C) 1995-2001 Russell King
5  * Copyright (C) 2012 ARM Ltd.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <linux/acpi.h>
21 #include <linux/export.h>
22 #include <linux/kernel.h>
23 #include <linux/stddef.h>
24 #include <linux/ioport.h>
25 #include <linux/delay.h>
26 #include <linux/initrd.h>
27 #include <linux/console.h>
28 #include <linux/cache.h>
29 #include <linux/bootmem.h>
30 #include <linux/screen_info.h>
31 #include <linux/init.h>
32 #include <linux/kexec.h>
33 #include <linux/root_dev.h>
34 #include <linux/cpu.h>
35 #include <linux/interrupt.h>
36 #include <linux/smp.h>
37 #include <linux/fs.h>
38 #include <linux/proc_fs.h>
39 #include <linux/memblock.h>
40 #include <linux/of_fdt.h>
41 #include <linux/efi.h>
42 #include <linux/psci.h>
43 #include <linux/sched/task.h>
44 #include <linux/mm.h>
45 
46 #include <asm/acpi.h>
47 #include <asm/fixmap.h>
48 #include <asm/cpu.h>
49 #include <asm/cputype.h>
50 #include <asm/daifflags.h>
51 #include <asm/elf.h>
52 #include <asm/cpufeature.h>
53 #include <asm/cpu_ops.h>
54 #include <asm/kasan.h>
55 #include <asm/numa.h>
56 #include <asm/sections.h>
57 #include <asm/setup.h>
58 #include <asm/smp_plat.h>
59 #include <asm/cacheflush.h>
60 #include <asm/tlbflush.h>
61 #include <asm/traps.h>
62 #include <asm/memblock.h>
63 #include <asm/efi.h>
64 #include <asm/xen/hypervisor.h>
65 #include <asm/mmu_context.h>
66 
67 static int num_standard_resources;
68 static struct resource *standard_resources;
69 
70 phys_addr_t __fdt_pointer __initdata;
71 
72 /*
73  * Standard memory resources
74  */
75 static struct resource mem_res[] = {
76 	{
77 		.name = "Kernel code",
78 		.start = 0,
79 		.end = 0,
80 		.flags = IORESOURCE_SYSTEM_RAM
81 	},
82 	{
83 		.name = "Kernel data",
84 		.start = 0,
85 		.end = 0,
86 		.flags = IORESOURCE_SYSTEM_RAM
87 	}
88 };
89 
90 #define kernel_code mem_res[0]
91 #define kernel_data mem_res[1]
92 
93 /*
94  * The recorded values of x0 .. x3 upon kernel entry.
95  */
96 u64 __cacheline_aligned boot_args[4];
97 
smp_setup_processor_id(void)98 void __init smp_setup_processor_id(void)
99 {
100 	u64 mpidr = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
101 	cpu_logical_map(0) = mpidr;
102 
103 	/*
104 	 * clear __my_cpu_offset on boot CPU to avoid hang caused by
105 	 * using percpu variable early, for example, lockdep will
106 	 * access percpu variable inside lock_release
107 	 */
108 	set_my_cpu_offset(0);
109 	pr_info("Booting Linux on physical CPU 0x%010lx [0x%08x]\n",
110 		(unsigned long)mpidr, read_cpuid_id());
111 }
112 
arch_match_cpu_phys_id(int cpu,u64 phys_id)113 bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
114 {
115 	return phys_id == cpu_logical_map(cpu);
116 }
117 
118 struct mpidr_hash mpidr_hash;
119 /**
120  * smp_build_mpidr_hash - Pre-compute shifts required at each affinity
121  *			  level in order to build a linear index from an
122  *			  MPIDR value. Resulting algorithm is a collision
123  *			  free hash carried out through shifting and ORing
124  */
smp_build_mpidr_hash(void)125 static void __init smp_build_mpidr_hash(void)
126 {
127 	u32 i, affinity, fs[4], bits[4], ls;
128 	u64 mask = 0;
129 	/*
130 	 * Pre-scan the list of MPIDRS and filter out bits that do
131 	 * not contribute to affinity levels, ie they never toggle.
132 	 */
133 	for_each_possible_cpu(i)
134 		mask |= (cpu_logical_map(i) ^ cpu_logical_map(0));
135 	pr_debug("mask of set bits %#llx\n", mask);
136 	/*
137 	 * Find and stash the last and first bit set at all affinity levels to
138 	 * check how many bits are required to represent them.
139 	 */
140 	for (i = 0; i < 4; i++) {
141 		affinity = MPIDR_AFFINITY_LEVEL(mask, i);
142 		/*
143 		 * Find the MSB bit and LSB bits position
144 		 * to determine how many bits are required
145 		 * to express the affinity level.
146 		 */
147 		ls = fls(affinity);
148 		fs[i] = affinity ? ffs(affinity) - 1 : 0;
149 		bits[i] = ls - fs[i];
150 	}
151 	/*
152 	 * An index can be created from the MPIDR_EL1 by isolating the
153 	 * significant bits at each affinity level and by shifting
154 	 * them in order to compress the 32 bits values space to a
155 	 * compressed set of values. This is equivalent to hashing
156 	 * the MPIDR_EL1 through shifting and ORing. It is a collision free
157 	 * hash though not minimal since some levels might contain a number
158 	 * of CPUs that is not an exact power of 2 and their bit
159 	 * representation might contain holes, eg MPIDR_EL1[7:0] = {0x2, 0x80}.
160 	 */
161 	mpidr_hash.shift_aff[0] = MPIDR_LEVEL_SHIFT(0) + fs[0];
162 	mpidr_hash.shift_aff[1] = MPIDR_LEVEL_SHIFT(1) + fs[1] - bits[0];
163 	mpidr_hash.shift_aff[2] = MPIDR_LEVEL_SHIFT(2) + fs[2] -
164 						(bits[1] + bits[0]);
165 	mpidr_hash.shift_aff[3] = MPIDR_LEVEL_SHIFT(3) +
166 				  fs[3] - (bits[2] + bits[1] + bits[0]);
167 	mpidr_hash.mask = mask;
168 	mpidr_hash.bits = bits[3] + bits[2] + bits[1] + bits[0];
169 	pr_debug("MPIDR hash: aff0[%u] aff1[%u] aff2[%u] aff3[%u] mask[%#llx] bits[%u]\n",
170 		mpidr_hash.shift_aff[0],
171 		mpidr_hash.shift_aff[1],
172 		mpidr_hash.shift_aff[2],
173 		mpidr_hash.shift_aff[3],
174 		mpidr_hash.mask,
175 		mpidr_hash.bits);
176 	/*
177 	 * 4x is an arbitrary value used to warn on a hash table much bigger
178 	 * than expected on most systems.
179 	 */
180 	if (mpidr_hash_size() > 4 * num_possible_cpus())
181 		pr_warn("Large number of MPIDR hash buckets detected\n");
182 }
183 
setup_machine_fdt(phys_addr_t dt_phys)184 static void __init setup_machine_fdt(phys_addr_t dt_phys)
185 {
186 	void *dt_virt = fixmap_remap_fdt(dt_phys);
187 	const char *name;
188 
189 	if (!dt_virt || !early_init_dt_scan(dt_virt)) {
190 		pr_crit("\n"
191 			"Error: invalid device tree blob at physical address %pa (virtual address 0x%p)\n"
192 			"The dtb must be 8-byte aligned and must not exceed 2 MB in size\n"
193 			"\nPlease check your bootloader.",
194 			&dt_phys, dt_virt);
195 
196 		while (true)
197 			cpu_relax();
198 	}
199 
200 	name = of_flat_dt_get_machine_name();
201 	if (!name)
202 		return;
203 
204 	pr_info("Machine model: %s\n", name);
205 	dump_stack_set_arch_desc("%s (DT)", name);
206 }
207 
request_standard_resources(void)208 static void __init request_standard_resources(void)
209 {
210 	struct memblock_region *region;
211 	struct resource *res;
212 	unsigned long i = 0;
213 
214 	kernel_code.start   = __pa_symbol(_text);
215 	kernel_code.end     = __pa_symbol(__init_begin - 1);
216 	kernel_data.start   = __pa_symbol(_sdata);
217 	kernel_data.end     = __pa_symbol(_end - 1);
218 
219 	num_standard_resources = memblock.memory.cnt;
220 	standard_resources = alloc_bootmem_low(num_standard_resources *
221 					       sizeof(*standard_resources));
222 
223 	for_each_memblock(memory, region) {
224 		res = &standard_resources[i++];
225 		if (memblock_is_nomap(region)) {
226 			res->name  = "reserved";
227 			res->flags = IORESOURCE_MEM;
228 		} else {
229 			res->name  = "System RAM";
230 			res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
231 		}
232 		res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
233 		res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
234 
235 		request_resource(&iomem_resource, res);
236 
237 		if (kernel_code.start >= res->start &&
238 		    kernel_code.end <= res->end)
239 			request_resource(res, &kernel_code);
240 		if (kernel_data.start >= res->start &&
241 		    kernel_data.end <= res->end)
242 			request_resource(res, &kernel_data);
243 #ifdef CONFIG_KEXEC_CORE
244 		/* Userspace will find "Crash kernel" region in /proc/iomem. */
245 		if (crashk_res.end && crashk_res.start >= res->start &&
246 		    crashk_res.end <= res->end)
247 			request_resource(res, &crashk_res);
248 #endif
249 	}
250 }
251 
reserve_memblock_reserved_regions(void)252 static int __init reserve_memblock_reserved_regions(void)
253 {
254 	u64 i, j;
255 
256 	for (i = 0; i < num_standard_resources; ++i) {
257 		struct resource *mem = &standard_resources[i];
258 		phys_addr_t r_start, r_end, mem_size = resource_size(mem);
259 
260 		if (!memblock_is_region_reserved(mem->start, mem_size))
261 			continue;
262 
263 		for_each_reserved_mem_region(j, &r_start, &r_end) {
264 			resource_size_t start, end;
265 
266 			start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
267 			end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
268 
269 			if (start > mem->end || end < mem->start)
270 				continue;
271 
272 			reserve_region_with_split(mem, start, end, "reserved");
273 		}
274 	}
275 
276 	return 0;
277 }
278 arch_initcall(reserve_memblock_reserved_regions);
279 
280 u64 __cpu_logical_map[NR_CPUS] = { [0 ... NR_CPUS-1] = INVALID_HWID };
281 
setup_arch(char ** cmdline_p)282 void __init setup_arch(char **cmdline_p)
283 {
284 	init_mm.start_code = (unsigned long) _text;
285 	init_mm.end_code   = (unsigned long) _etext;
286 	init_mm.end_data   = (unsigned long) _edata;
287 	init_mm.brk	   = (unsigned long) _end;
288 
289 	*cmdline_p = boot_command_line;
290 
291 	early_fixmap_init();
292 	early_ioremap_init();
293 
294 	setup_machine_fdt(__fdt_pointer);
295 
296 	parse_early_param();
297 
298 	/*
299 	 * Unmask asynchronous aborts and fiq after bringing up possible
300 	 * earlycon. (Report possible System Errors once we can report this
301 	 * occurred).
302 	 */
303 	local_daif_restore(DAIF_PROCCTX_NOIRQ);
304 
305 	/*
306 	 * TTBR0 is only used for the identity mapping at this stage. Make it
307 	 * point to zero page to avoid speculatively fetching new entries.
308 	 */
309 	cpu_uninstall_idmap();
310 
311 	xen_early_init();
312 	efi_init();
313 	arm64_memblock_init();
314 
315 	paging_init();
316 
317 	acpi_table_upgrade();
318 
319 	/* Parse the ACPI tables for possible boot-time configuration */
320 	acpi_boot_table_init();
321 
322 	if (acpi_disabled)
323 		unflatten_device_tree();
324 
325 	bootmem_init();
326 
327 	kasan_init();
328 
329 	request_standard_resources();
330 
331 	early_ioremap_reset();
332 
333 	if (acpi_disabled)
334 		psci_dt_init();
335 	else
336 		psci_acpi_init();
337 
338 	cpu_read_bootcpu_ops();
339 	smp_init_cpus();
340 	smp_build_mpidr_hash();
341 
342 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
343 	/*
344 	 * Make sure init_thread_info.ttbr0 always generates translation
345 	 * faults in case uaccess_enable() is inadvertently called by the init
346 	 * thread.
347 	 */
348 	init_task.thread_info.ttbr0 = __pa_symbol(empty_zero_page);
349 #endif
350 
351 #ifdef CONFIG_VT
352 #if defined(CONFIG_VGA_CONSOLE)
353 	conswitchp = &vga_con;
354 #elif defined(CONFIG_DUMMY_CONSOLE)
355 	conswitchp = &dummy_con;
356 #endif
357 #endif
358 	if (boot_args[1] || boot_args[2] || boot_args[3]) {
359 		pr_err("WARNING: x1-x3 nonzero in violation of boot protocol:\n"
360 			"\tx1: %016llx\n\tx2: %016llx\n\tx3: %016llx\n"
361 			"This indicates a broken bootloader or old kernel\n",
362 			boot_args[1], boot_args[2], boot_args[3]);
363 	}
364 }
365 
topology_init(void)366 static int __init topology_init(void)
367 {
368 	int i;
369 
370 	for_each_online_node(i)
371 		register_one_node(i);
372 
373 	for_each_possible_cpu(i) {
374 		struct cpu *cpu = &per_cpu(cpu_data.cpu, i);
375 		cpu->hotpluggable = 1;
376 		register_cpu(cpu, i);
377 	}
378 
379 	return 0;
380 }
381 subsys_initcall(topology_init);
382 
383 /*
384  * Dump out kernel offset information on panic.
385  */
dump_kernel_offset(struct notifier_block * self,unsigned long v,void * p)386 static int dump_kernel_offset(struct notifier_block *self, unsigned long v,
387 			      void *p)
388 {
389 	const unsigned long offset = kaslr_offset();
390 
391 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && offset > 0) {
392 		pr_emerg("Kernel Offset: 0x%lx from 0x%lx\n",
393 			 offset, KIMAGE_VADDR);
394 	} else {
395 		pr_emerg("Kernel Offset: disabled\n");
396 	}
397 	return 0;
398 }
399 
400 static struct notifier_block kernel_offset_notifier = {
401 	.notifier_call = dump_kernel_offset
402 };
403 
register_kernel_offset_dumper(void)404 static int __init register_kernel_offset_dumper(void)
405 {
406 	atomic_notifier_chain_register(&panic_notifier_list,
407 				       &kernel_offset_notifier);
408 	return 0;
409 }
410 __initcall(register_kernel_offset_dumper);
411