xref: /linux-6.15/drivers/of/cpu.c (revision ec8c2329)
1b58fa269SRob Herring // SPDX-License-Identifier: GPL-2.0
2b58fa269SRob Herring #include <linux/cpu.h>
3b58fa269SRob Herring #include <linux/kernel.h>
4b58fa269SRob Herring #include <linux/of.h>
5b58fa269SRob Herring 
6b58fa269SRob Herring /**
7b58fa269SRob Herring  * of_get_cpu_hwid - Get the hardware ID from a CPU device node
8b58fa269SRob Herring  *
9b58fa269SRob Herring  * @cpun: CPU number(logical index) for which device node is required
10b58fa269SRob Herring  * @thread: The local thread number to get the hardware ID for.
11b58fa269SRob Herring  *
12b58fa269SRob Herring  * Return: The hardware ID for the CPU node or ~0ULL if not found.
13b58fa269SRob Herring  */
of_get_cpu_hwid(struct device_node * cpun,unsigned int thread)14b58fa269SRob Herring u64 of_get_cpu_hwid(struct device_node *cpun, unsigned int thread)
15b58fa269SRob Herring {
16b58fa269SRob Herring 	const __be32 *cell;
17b58fa269SRob Herring 	int ac, len;
18b58fa269SRob Herring 
19b58fa269SRob Herring 	ac = of_n_addr_cells(cpun);
20b58fa269SRob Herring 	cell = of_get_property(cpun, "reg", &len);
21b58fa269SRob Herring 	if (!cell || !ac || ((sizeof(*cell) * ac * (thread + 1)) > len))
22b58fa269SRob Herring 		return ~0ULL;
23b58fa269SRob Herring 
24b58fa269SRob Herring 	cell += ac * thread;
25b58fa269SRob Herring 	return of_read_number(cell, ac);
26b58fa269SRob Herring }
27b58fa269SRob Herring 
28b58fa269SRob Herring /*
29b58fa269SRob Herring  * arch_match_cpu_phys_id - Match the given logical CPU and physical id
30b58fa269SRob Herring  *
31b58fa269SRob Herring  * @cpu: logical cpu index of a core/thread
32b58fa269SRob Herring  * @phys_id: physical identifier of a core/thread
33b58fa269SRob Herring  *
34b58fa269SRob Herring  * CPU logical to physical index mapping is architecture specific.
35b58fa269SRob Herring  * However this __weak function provides a default match of physical
36b58fa269SRob Herring  * id to logical cpu index. phys_id provided here is usually values read
37b58fa269SRob Herring  * from the device tree which must match the hardware internal registers.
38b58fa269SRob Herring  *
39b58fa269SRob Herring  * Returns true if the physical identifier and the logical cpu index
40b58fa269SRob Herring  * correspond to the same core/thread, false otherwise.
41b58fa269SRob Herring  */
arch_match_cpu_phys_id(int cpu,u64 phys_id)42b58fa269SRob Herring bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
43b58fa269SRob Herring {
44b58fa269SRob Herring 	return (u32)phys_id == cpu;
45b58fa269SRob Herring }
46b58fa269SRob Herring 
47b58fa269SRob Herring /*
48b58fa269SRob Herring  * Checks if the given "prop_name" property holds the physical id of the
49b58fa269SRob Herring  * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
50b58fa269SRob Herring  * NULL, local thread number within the core is returned in it.
51b58fa269SRob Herring  */
__of_find_n_match_cpu_property(struct device_node * cpun,const char * prop_name,int cpu,unsigned int * thread)52b58fa269SRob Herring static bool __of_find_n_match_cpu_property(struct device_node *cpun,
53b58fa269SRob Herring 			const char *prop_name, int cpu, unsigned int *thread)
54b58fa269SRob Herring {
55b58fa269SRob Herring 	const __be32 *cell;
56b58fa269SRob Herring 	int ac, prop_len, tid;
57b58fa269SRob Herring 	u64 hwid;
58b58fa269SRob Herring 
59b58fa269SRob Herring 	ac = of_n_addr_cells(cpun);
60b58fa269SRob Herring 	cell = of_get_property(cpun, prop_name, &prop_len);
61b58fa269SRob Herring 	if (!cell && !ac && arch_match_cpu_phys_id(cpu, 0))
62b58fa269SRob Herring 		return true;
63b58fa269SRob Herring 	if (!cell || !ac)
64b58fa269SRob Herring 		return false;
65b58fa269SRob Herring 	prop_len /= sizeof(*cell) * ac;
66b58fa269SRob Herring 	for (tid = 0; tid < prop_len; tid++) {
67b58fa269SRob Herring 		hwid = of_read_number(cell, ac);
68b58fa269SRob Herring 		if (arch_match_cpu_phys_id(cpu, hwid)) {
69b58fa269SRob Herring 			if (thread)
70b58fa269SRob Herring 				*thread = tid;
71b58fa269SRob Herring 			return true;
72b58fa269SRob Herring 		}
73b58fa269SRob Herring 		cell += ac;
74b58fa269SRob Herring 	}
75b58fa269SRob Herring 	return false;
76b58fa269SRob Herring }
77b58fa269SRob Herring 
78b58fa269SRob Herring /*
79b58fa269SRob Herring  * arch_find_n_match_cpu_physical_id - See if the given device node is
80b58fa269SRob Herring  * for the cpu corresponding to logical cpu 'cpu'.  Return true if so,
81b58fa269SRob Herring  * else false.  If 'thread' is non-NULL, the local thread number within the
82b58fa269SRob Herring  * core is returned in it.
83b58fa269SRob Herring  */
arch_find_n_match_cpu_physical_id(struct device_node * cpun,int cpu,unsigned int * thread)84b58fa269SRob Herring bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
85b58fa269SRob Herring 					      int cpu, unsigned int *thread)
86b58fa269SRob Herring {
87b58fa269SRob Herring 	/* Check for non-standard "ibm,ppc-interrupt-server#s" property
88b58fa269SRob Herring 	 * for thread ids on PowerPC. If it doesn't exist fallback to
89b58fa269SRob Herring 	 * standard "reg" property.
90b58fa269SRob Herring 	 */
91b58fa269SRob Herring 	if (IS_ENABLED(CONFIG_PPC) &&
92b58fa269SRob Herring 	    __of_find_n_match_cpu_property(cpun,
93b58fa269SRob Herring 					   "ibm,ppc-interrupt-server#s",
94b58fa269SRob Herring 					   cpu, thread))
95b58fa269SRob Herring 		return true;
96b58fa269SRob Herring 
97b58fa269SRob Herring 	return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
98b58fa269SRob Herring }
99b58fa269SRob Herring 
100b58fa269SRob Herring /**
101b58fa269SRob Herring  * of_get_cpu_node - Get device node associated with the given logical CPU
102b58fa269SRob Herring  *
103b58fa269SRob Herring  * @cpu: CPU number(logical index) for which device node is required
104b58fa269SRob Herring  * @thread: if not NULL, local thread number within the physical core is
105b58fa269SRob Herring  *          returned
106b58fa269SRob Herring  *
107b58fa269SRob Herring  * The main purpose of this function is to retrieve the device node for the
108b58fa269SRob Herring  * given logical CPU index. It should be used to initialize the of_node in
109b58fa269SRob Herring  * cpu device. Once of_node in cpu device is populated, all the further
110b58fa269SRob Herring  * references can use that instead.
111b58fa269SRob Herring  *
112b58fa269SRob Herring  * CPU logical to physical index mapping is architecture specific and is built
113b58fa269SRob Herring  * before booting secondary cores. This function uses arch_match_cpu_phys_id
114b58fa269SRob Herring  * which can be overridden by architecture specific implementation.
115b58fa269SRob Herring  *
116b58fa269SRob Herring  * Return: A node pointer for the logical cpu with refcount incremented, use
117b58fa269SRob Herring  * of_node_put() on it when done. Returns NULL if not found.
118b58fa269SRob Herring  */
of_get_cpu_node(int cpu,unsigned int * thread)119b58fa269SRob Herring struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
120b58fa269SRob Herring {
121b58fa269SRob Herring 	struct device_node *cpun;
122b58fa269SRob Herring 
123b58fa269SRob Herring 	for_each_of_cpu_node(cpun) {
124b58fa269SRob Herring 		if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
125b58fa269SRob Herring 			return cpun;
126b58fa269SRob Herring 	}
127b58fa269SRob Herring 	return NULL;
128b58fa269SRob Herring }
129b58fa269SRob Herring EXPORT_SYMBOL(of_get_cpu_node);
130b58fa269SRob Herring 
131b58fa269SRob Herring /**
132b58fa269SRob Herring  * of_cpu_device_node_get: Get the CPU device_node for a given logical CPU number
133b58fa269SRob Herring  *
134b58fa269SRob Herring  * @cpu: The logical CPU number
135b58fa269SRob Herring  *
136b58fa269SRob Herring  * Return: Pointer to the device_node for CPU with its reference count
137b58fa269SRob Herring  * incremented of the given logical CPU number or NULL if the CPU device_node
138b58fa269SRob Herring  * is not found.
139b58fa269SRob Herring  */
of_cpu_device_node_get(int cpu)140b58fa269SRob Herring struct device_node *of_cpu_device_node_get(int cpu)
141b58fa269SRob Herring {
142b58fa269SRob Herring 	struct device *cpu_dev;
143b58fa269SRob Herring 	cpu_dev = get_cpu_device(cpu);
144b58fa269SRob Herring 	if (!cpu_dev)
145b58fa269SRob Herring 		return of_get_cpu_node(cpu, NULL);
146b58fa269SRob Herring 	return of_node_get(cpu_dev->of_node);
147b58fa269SRob Herring }
148b58fa269SRob Herring EXPORT_SYMBOL(of_cpu_device_node_get);
149b58fa269SRob Herring 
150b58fa269SRob Herring /**
151b58fa269SRob Herring  * of_cpu_node_to_id: Get the logical CPU number for a given device_node
152b58fa269SRob Herring  *
153b58fa269SRob Herring  * @cpu_node: Pointer to the device_node for CPU.
154b58fa269SRob Herring  *
155b58fa269SRob Herring  * Return: The logical CPU number of the given CPU device_node or -ENODEV if the
156b58fa269SRob Herring  * CPU is not found.
157b58fa269SRob Herring  */
of_cpu_node_to_id(struct device_node * cpu_node)158b58fa269SRob Herring int of_cpu_node_to_id(struct device_node *cpu_node)
159b58fa269SRob Herring {
160b58fa269SRob Herring 	int cpu;
161b58fa269SRob Herring 	bool found = false;
162b58fa269SRob Herring 	struct device_node *np;
163b58fa269SRob Herring 
164b58fa269SRob Herring 	for_each_possible_cpu(cpu) {
165b58fa269SRob Herring 		np = of_cpu_device_node_get(cpu);
166b58fa269SRob Herring 		found = (cpu_node == np);
167b58fa269SRob Herring 		of_node_put(np);
168b58fa269SRob Herring 		if (found)
169b58fa269SRob Herring 			return cpu;
170b58fa269SRob Herring 	}
171b58fa269SRob Herring 
172b58fa269SRob Herring 	return -ENODEV;
173b58fa269SRob Herring }
174b58fa269SRob Herring EXPORT_SYMBOL(of_cpu_node_to_id);
175b58fa269SRob Herring 
176b58fa269SRob Herring /**
177b58fa269SRob Herring  * of_get_cpu_state_node - Get CPU's idle state node at the given index
178b58fa269SRob Herring  *
179b58fa269SRob Herring  * @cpu_node: The device node for the CPU
180b58fa269SRob Herring  * @index: The index in the list of the idle states
181b58fa269SRob Herring  *
182b58fa269SRob Herring  * Two generic methods can be used to describe a CPU's idle states, either via
183b58fa269SRob Herring  * a flattened description through the "cpu-idle-states" binding or via the
184b58fa269SRob Herring  * hierarchical layout, using the "power-domains" and the "domain-idle-states"
185b58fa269SRob Herring  * bindings. This function check for both and returns the idle state node for
186b58fa269SRob Herring  * the requested index.
187b58fa269SRob Herring  *
188b58fa269SRob Herring  * Return: An idle state node if found at @index. The refcount is incremented
189b58fa269SRob Herring  * for it, so call of_node_put() on it when done. Returns NULL if not found.
190b58fa269SRob Herring  */
of_get_cpu_state_node(const struct device_node * cpu_node,int index)191*ec8c2329SRob Herring (Arm) struct device_node *of_get_cpu_state_node(const struct device_node *cpu_node,
192b58fa269SRob Herring 					  int index)
193b58fa269SRob Herring {
194b58fa269SRob Herring 	struct of_phandle_args args;
195b58fa269SRob Herring 	int err;
196b58fa269SRob Herring 
197b58fa269SRob Herring 	err = of_parse_phandle_with_args(cpu_node, "power-domains",
198b58fa269SRob Herring 					"#power-domain-cells", 0, &args);
199b58fa269SRob Herring 	if (!err) {
200b58fa269SRob Herring 		struct device_node *state_node =
201b58fa269SRob Herring 			of_parse_phandle(args.np, "domain-idle-states", index);
202b58fa269SRob Herring 
203b58fa269SRob Herring 		of_node_put(args.np);
204b58fa269SRob Herring 		if (state_node)
205b58fa269SRob Herring 			return state_node;
206b58fa269SRob Herring 	}
207b58fa269SRob Herring 
208b58fa269SRob Herring 	return of_parse_phandle(cpu_node, "cpu-idle-states", index);
209b58fa269SRob Herring }
210b58fa269SRob Herring EXPORT_SYMBOL(of_get_cpu_state_node);
211