xref: /linux-6.15/arch/arm/kernel/devtree.c (revision b2473a35)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29eb8f674SGrant Likely /*
39eb8f674SGrant Likely  *  linux/arch/arm/kernel/devtree.c
49eb8f674SGrant Likely  *
59eb8f674SGrant Likely  *  Copyright (C) 2009 Canonical Ltd. <[email protected]>
69eb8f674SGrant Likely  */
79eb8f674SGrant Likely 
89eb8f674SGrant Likely #include <linux/init.h>
9ecea4ab6SPaul Gortmaker #include <linux/export.h>
109eb8f674SGrant Likely #include <linux/errno.h>
119eb8f674SGrant Likely #include <linux/types.h>
129eb8f674SGrant Likely #include <linux/memblock.h>
139eb8f674SGrant Likely #include <linux/of.h>
149eb8f674SGrant Likely #include <linux/of_fdt.h>
159eb8f674SGrant Likely #include <linux/of_irq.h>
166c3ff8b1SStephen Boyd #include <linux/smp.h>
179eb8f674SGrant Likely 
18a0ae0240SLorenzo Pieralisi #include <asm/cputype.h>
199eb8f674SGrant Likely #include <asm/setup.h>
209eb8f674SGrant Likely #include <asm/page.h>
212374b063SBen Dooks #include <asm/prom.h>
22a0ae0240SLorenzo Pieralisi #include <asm/smp_plat.h>
2393c02ab4SGrant Likely #include <asm/mach/arch.h>
2493c02ab4SGrant Likely #include <asm/mach-types.h>
259eb8f674SGrant Likely 
269eb8f674SGrant Likely 
276c3ff8b1SStephen Boyd #ifdef CONFIG_SMP
289a721c41SRob Herring extern struct of_cpu_method __cpu_method_of_table[];
299a721c41SRob Herring 
309a721c41SRob Herring static const struct of_cpu_method __cpu_method_of_table_sentinel
3133def849SJoe Perches 	__used __section("__cpu_method_of_table_end");
329a721c41SRob Herring 
336c3ff8b1SStephen Boyd 
set_smp_ops_by_method(struct device_node * node)346c3ff8b1SStephen Boyd static int __init set_smp_ops_by_method(struct device_node *node)
356c3ff8b1SStephen Boyd {
366c3ff8b1SStephen Boyd 	const char *method;
379a721c41SRob Herring 	struct of_cpu_method *m = __cpu_method_of_table;
386c3ff8b1SStephen Boyd 
396c3ff8b1SStephen Boyd 	if (of_property_read_string(node, "enable-method", &method))
406c3ff8b1SStephen Boyd 		return 0;
416c3ff8b1SStephen Boyd 
429a721c41SRob Herring 	for (; m->method; m++)
436c3ff8b1SStephen Boyd 		if (!strcmp(m->method, method)) {
446c3ff8b1SStephen Boyd 			smp_set_ops(m->ops);
456c3ff8b1SStephen Boyd 			return 1;
466c3ff8b1SStephen Boyd 		}
476c3ff8b1SStephen Boyd 
486c3ff8b1SStephen Boyd 	return 0;
496c3ff8b1SStephen Boyd }
506c3ff8b1SStephen Boyd #else
set_smp_ops_by_method(struct device_node * node)516c3ff8b1SStephen Boyd static inline int set_smp_ops_by_method(struct device_node *node)
526c3ff8b1SStephen Boyd {
536c3ff8b1SStephen Boyd 	return 1;
546c3ff8b1SStephen Boyd }
556c3ff8b1SStephen Boyd #endif
566c3ff8b1SStephen Boyd 
576c3ff8b1SStephen Boyd 
58a0ae0240SLorenzo Pieralisi /*
59a0ae0240SLorenzo Pieralisi  * arm_dt_init_cpu_maps - Function retrieves cpu nodes from the device tree
60a0ae0240SLorenzo Pieralisi  * and builds the cpu logical map array containing MPIDR values related to
61a0ae0240SLorenzo Pieralisi  * logical cpus
62a0ae0240SLorenzo Pieralisi  *
63a0ae0240SLorenzo Pieralisi  * Updates the cpu possible mask with the number of parsed cpu nodes
64a0ae0240SLorenzo Pieralisi  */
arm_dt_init_cpu_maps(void)65a0ae0240SLorenzo Pieralisi void __init arm_dt_init_cpu_maps(void)
66a0ae0240SLorenzo Pieralisi {
67a0ae0240SLorenzo Pieralisi 	/*
68a0ae0240SLorenzo Pieralisi 	 * Temp logical map is initialized with UINT_MAX values that are
69a0ae0240SLorenzo Pieralisi 	 * considered invalid logical map entries since the logical map must
70a0ae0240SLorenzo Pieralisi 	 * contain a list of MPIDR[23:0] values where MPIDR[31:24] must
71a0ae0240SLorenzo Pieralisi 	 * read as 0.
72a0ae0240SLorenzo Pieralisi 	 */
73a0ae0240SLorenzo Pieralisi 	struct device_node *cpu, *cpus;
746c3ff8b1SStephen Boyd 	int found_method = 0;
75a0ae0240SLorenzo Pieralisi 	u32 i, j, cpuidx = 1;
76a0ae0240SLorenzo Pieralisi 	u32 mpidr = is_smp() ? read_cpuid_mpidr() & MPIDR_HWID_BITMASK : 0;
77a0ae0240SLorenzo Pieralisi 
7818d7f152SLorenzo Pieralisi 	u32 tmp_map[NR_CPUS] = { [0 ... NR_CPUS-1] = MPIDR_INVALID };
79a0ae0240SLorenzo Pieralisi 	bool bootcpu_valid = false;
80a0ae0240SLorenzo Pieralisi 	cpus = of_find_node_by_path("/cpus");
81a0ae0240SLorenzo Pieralisi 
82a0ae0240SLorenzo Pieralisi 	if (!cpus)
83a0ae0240SLorenzo Pieralisi 		return;
84a0ae0240SLorenzo Pieralisi 
85d4866f75SRob Herring 	for_each_of_cpu_node(cpu) {
86ca96bbe2SRob Herring 		u32 hwid = of_get_cpu_hwid(cpu, 0);
87a0ae0240SLorenzo Pieralisi 
88a8e65e06SRob Herring 		pr_debug(" * %pOF...\n", cpu);
89a0ae0240SLorenzo Pieralisi 
90a0ae0240SLorenzo Pieralisi 		/*
91ba6dea4fSRobin Murphy 		 * Bits n:24 must be set to 0 in the DT since the reg property
92a0ae0240SLorenzo Pieralisi 		 * defines the MPIDR[23:0].
93a0ae0240SLorenzo Pieralisi 		 */
94ca96bbe2SRob Herring 		if (hwid & ~MPIDR_HWID_BITMASK) {
95a4283e41SJulia Lawall 			of_node_put(cpu);
96a0ae0240SLorenzo Pieralisi 			return;
97a4283e41SJulia Lawall 		}
98a0ae0240SLorenzo Pieralisi 
99a0ae0240SLorenzo Pieralisi 		/*
100a0ae0240SLorenzo Pieralisi 		 * Duplicate MPIDRs are a recipe for disaster.
101a0ae0240SLorenzo Pieralisi 		 * Scan all initialized entries and check for
102a0ae0240SLorenzo Pieralisi 		 * duplicates. If any is found just bail out.
103a0ae0240SLorenzo Pieralisi 		 * temp values were initialized to UINT_MAX
104a0ae0240SLorenzo Pieralisi 		 * to avoid matching valid MPIDR[23:0] values.
105a0ae0240SLorenzo Pieralisi 		 */
106a0ae0240SLorenzo Pieralisi 		for (j = 0; j < cpuidx; j++)
107a4283e41SJulia Lawall 			if (WARN(tmp_map[j] == hwid,
108a4283e41SJulia Lawall 				 "Duplicate /cpu reg properties in the DT\n")) {
109a4283e41SJulia Lawall 				of_node_put(cpu);
110a0ae0240SLorenzo Pieralisi 				return;
111a4283e41SJulia Lawall 			}
112a0ae0240SLorenzo Pieralisi 
113a0ae0240SLorenzo Pieralisi 		/*
114a0ae0240SLorenzo Pieralisi 		 * Build a stashed array of MPIDR values. Numbering scheme
115a0ae0240SLorenzo Pieralisi 		 * requires that if detected the boot CPU must be assigned
116a0ae0240SLorenzo Pieralisi 		 * logical id 0. Other CPUs get sequential indexes starting
117a0ae0240SLorenzo Pieralisi 		 * from 1. If a CPU node with a reg property matching the
118a0ae0240SLorenzo Pieralisi 		 * boot CPU MPIDR is detected, this is recorded so that the
119a0ae0240SLorenzo Pieralisi 		 * logical map built from DT is validated and can be used
120a0ae0240SLorenzo Pieralisi 		 * to override the map created in smp_setup_processor_id().
121a0ae0240SLorenzo Pieralisi 		 */
122a0ae0240SLorenzo Pieralisi 		if (hwid == mpidr) {
123a0ae0240SLorenzo Pieralisi 			i = 0;
124a0ae0240SLorenzo Pieralisi 			bootcpu_valid = true;
125a0ae0240SLorenzo Pieralisi 		} else {
126a0ae0240SLorenzo Pieralisi 			i = cpuidx++;
127a0ae0240SLorenzo Pieralisi 		}
128a0ae0240SLorenzo Pieralisi 
129ce7b1756SLorenzo Pieralisi 		if (WARN(cpuidx > nr_cpu_ids, "DT /cpu %u nodes greater than "
130ce7b1756SLorenzo Pieralisi 					       "max cores %u, capping them\n",
131ce7b1756SLorenzo Pieralisi 					       cpuidx, nr_cpu_ids)) {
132ce7b1756SLorenzo Pieralisi 			cpuidx = nr_cpu_ids;
133a4283e41SJulia Lawall 			of_node_put(cpu);
134a0ae0240SLorenzo Pieralisi 			break;
135a0ae0240SLorenzo Pieralisi 		}
136a0ae0240SLorenzo Pieralisi 
137ce7b1756SLorenzo Pieralisi 		tmp_map[i] = hwid;
1386c3ff8b1SStephen Boyd 
1396c3ff8b1SStephen Boyd 		if (!found_method)
1406c3ff8b1SStephen Boyd 			found_method = set_smp_ops_by_method(cpu);
141ce7b1756SLorenzo Pieralisi 	}
142ce7b1756SLorenzo Pieralisi 
1436c3ff8b1SStephen Boyd 	/*
1446c3ff8b1SStephen Boyd 	 * Fallback to an enable-method in the cpus node if nothing found in
1456c3ff8b1SStephen Boyd 	 * a cpu node.
1466c3ff8b1SStephen Boyd 	 */
1476c3ff8b1SStephen Boyd 	if (!found_method)
1486c3ff8b1SStephen Boyd 		set_smp_ops_by_method(cpus);
1496c3ff8b1SStephen Boyd 
1508d5bc1a6SOlof Johansson 	if (!bootcpu_valid) {
1518d5bc1a6SOlof Johansson 		pr_warn("DT missing boot CPU MPIDR[23:0], fall back to default cpu_logical_map\n");
152a0ae0240SLorenzo Pieralisi 		return;
1538d5bc1a6SOlof Johansson 	}
154a0ae0240SLorenzo Pieralisi 
155a0ae0240SLorenzo Pieralisi 	/*
156a0ae0240SLorenzo Pieralisi 	 * Since the boot CPU node contains proper data, and all nodes have
157a0ae0240SLorenzo Pieralisi 	 * a reg property, the DT CPU list can be considered valid and the
158a0ae0240SLorenzo Pieralisi 	 * logical map created in smp_setup_processor_id() can be overridden
159a0ae0240SLorenzo Pieralisi 	 */
160a0ae0240SLorenzo Pieralisi 	for (i = 0; i < cpuidx; i++) {
161a0ae0240SLorenzo Pieralisi 		set_cpu_possible(i, true);
162a0ae0240SLorenzo Pieralisi 		cpu_logical_map(i) = tmp_map[i];
163a0ae0240SLorenzo Pieralisi 		pr_debug("cpu logical map 0x%x\n", cpu_logical_map(i));
164a0ae0240SLorenzo Pieralisi 	}
165a0ae0240SLorenzo Pieralisi }
166a0ae0240SLorenzo Pieralisi 
arch_match_cpu_phys_id(int cpu,u64 phys_id)167973e02c1SSudeep KarkadaNagesha bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
168973e02c1SSudeep KarkadaNagesha {
169e44ef891SSudeep Holla 	return phys_id == cpu_logical_map(cpu);
170973e02c1SSudeep KarkadaNagesha }
171973e02c1SSudeep KarkadaNagesha 
arch_get_next_mach(const char * const ** match)1726d67a9f6SRob Herring static const void * __init arch_get_next_mach(const char *const **match)
1736d67a9f6SRob Herring {
1746d67a9f6SRob Herring 	static const struct machine_desc *mdesc = __arch_info_begin;
1756d67a9f6SRob Herring 	const struct machine_desc *m = mdesc;
1766d67a9f6SRob Herring 
1776d67a9f6SRob Herring 	if (m >= __arch_info_end)
1786d67a9f6SRob Herring 		return NULL;
1796d67a9f6SRob Herring 
1806d67a9f6SRob Herring 	mdesc++;
1816d67a9f6SRob Herring 	*match = m->dt_compat;
1826d67a9f6SRob Herring 	return m;
1836d67a9f6SRob Herring }
1846d67a9f6SRob Herring 
18593c02ab4SGrant Likely /**
18693c02ab4SGrant Likely  * setup_machine_fdt - Machine setup when an dtb was passed to the kernel
187e9a2f8b5SArd Biesheuvel  * @dt_virt: virtual address of dt blob
18893c02ab4SGrant Likely  *
18993c02ab4SGrant Likely  * If a dtb was passed to the kernel in r2, then use it to choose the
19093c02ab4SGrant Likely  * correct machine_desc and to setup the system.
19193c02ab4SGrant Likely  */
setup_machine_fdt(void * dt_virt)192e9a2f8b5SArd Biesheuvel const struct machine_desc * __init setup_machine_fdt(void *dt_virt)
19393c02ab4SGrant Likely {
194ff69a4c8SRussell King 	const struct machine_desc *mdesc, *mdesc_best = NULL;
19593c02ab4SGrant Likely 
196883a106bSArnd Bergmann 	DT_MACHINE_START(GENERIC_DT, "Generic DT based system")
197cb6f8344SLinus Walleij 		.l2c_aux_val = 0x0,
198cb6f8344SLinus Walleij 		.l2c_aux_mask = ~0x0,
199883a106bSArnd Bergmann 	MACHINE_END
200883a106bSArnd Bergmann 
201ff69a4c8SRussell King 	mdesc_best = &__mach_desc_GENERIC_DT;
202883a106bSArnd Bergmann 
203*b2473a35SUsama Arif 	if (!dt_virt || !early_init_dt_verify(dt_virt, __pa(dt_virt)))
204f506cd48SNicolas Pitre 		return NULL;
205f506cd48SNicolas Pitre 
2066d67a9f6SRob Herring 	mdesc = of_flat_dt_match_machine(mdesc_best, arch_get_next_mach);
20793c02ab4SGrant Likely 
2086d67a9f6SRob Herring 	if (!mdesc) {
20993c02ab4SGrant Likely 		const char *prop;
2109d0c4dfeSRob Herring 		int size;
2116d67a9f6SRob Herring 		unsigned long dt_root;
21293c02ab4SGrant Likely 
21393c02ab4SGrant Likely 		early_print("\nError: unrecognized/unsupported "
21493c02ab4SGrant Likely 			    "device tree compatible list:\n[ ");
21593c02ab4SGrant Likely 
2166d67a9f6SRob Herring 		dt_root = of_get_flat_dt_root();
21793c02ab4SGrant Likely 		prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
21893c02ab4SGrant Likely 		while (size > 0) {
21993c02ab4SGrant Likely 			early_print("'%s' ", prop);
22093c02ab4SGrant Likely 			size -= strlen(prop) + 1;
22193c02ab4SGrant Likely 			prop += strlen(prop) + 1;
22293c02ab4SGrant Likely 		}
22393c02ab4SGrant Likely 		early_print("]\n\n");
22493c02ab4SGrant Likely 
22593c02ab4SGrant Likely 		dump_machine_table(); /* does not return */
22693c02ab4SGrant Likely 	}
22793c02ab4SGrant Likely 
2285a12a597SLaura Abbott 	/* We really don't want to do this, but sometimes firmware provides buggy data */
2295a12a597SLaura Abbott 	if (mdesc->dt_fixup)
2305a12a597SLaura Abbott 		mdesc->dt_fixup();
2315a12a597SLaura Abbott 
2325a12a597SLaura Abbott 	early_init_dt_scan_nodes();
2335a12a597SLaura Abbott 
23493c02ab4SGrant Likely 	/* Change machine number to match the mdesc we're using */
2356d67a9f6SRob Herring 	__machine_arch_type = mdesc->nr;
23693c02ab4SGrant Likely 
2376d67a9f6SRob Herring 	return mdesc;
23893c02ab4SGrant Likely }
239