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