12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2a88b5ba8SSam Ravnborg /*
3a88b5ba8SSam Ravnborg * Procedures for creating, accessing and interpreting the device tree.
4a88b5ba8SSam Ravnborg *
5a88b5ba8SSam Ravnborg * Paul Mackerras August 1996.
6a88b5ba8SSam Ravnborg * Copyright (C) 1996-2005 Paul Mackerras.
7a88b5ba8SSam Ravnborg *
8a88b5ba8SSam Ravnborg * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
9a88b5ba8SSam Ravnborg * {engebret|bergner}@us.ibm.com
10a88b5ba8SSam Ravnborg *
11a88b5ba8SSam Ravnborg * Adapted for sparc64 by David S. Miller davem@davemloft.net
12a88b5ba8SSam Ravnborg */
13a88b5ba8SSam Ravnborg
1495f72d1eSYinghai Lu #include <linux/memblock.h>
15cfbddd0dSSam Ravnborg #include <linux/kernel.h>
16cfbddd0dSSam Ravnborg #include <linux/string.h>
17cfbddd0dSSam Ravnborg #include <linux/types.h>
18cfbddd0dSSam Ravnborg #include <linux/cpu.h>
19cfbddd0dSSam Ravnborg #include <linux/mm.h>
20035ebefcSAndres Salomon #include <linux/of.h>
21a88b5ba8SSam Ravnborg
22a88b5ba8SSam Ravnborg #include <asm/prom.h>
23a88b5ba8SSam Ravnborg #include <asm/oplib.h>
24a88b5ba8SSam Ravnborg #include <asm/irq.h>
25a88b5ba8SSam Ravnborg #include <asm/asi.h>
26a88b5ba8SSam Ravnborg #include <asm/upa.h>
27a88b5ba8SSam Ravnborg #include <asm/smp.h>
28a88b5ba8SSam Ravnborg
29657f201dSDavid S. Miller #include "prom.h"
30a88b5ba8SSam Ravnborg
prom_early_alloc(unsigned long size)31efeac2f8SDavid S. Miller void * __init prom_early_alloc(unsigned long size)
32a88b5ba8SSam Ravnborg {
33b63a07d6SMike Rapoport void *ret = memblock_alloc(size, SMP_CACHE_BYTES);
34a88b5ba8SSam Ravnborg
35b63a07d6SMike Rapoport if (!ret) {
365da444aaSAkinobu Mita prom_printf("prom_early_alloc(%lu) failed\n", size);
37a88b5ba8SSam Ravnborg prom_halt();
38a88b5ba8SSam Ravnborg }
39a88b5ba8SSam Ravnborg
40a88b5ba8SSam Ravnborg prom_early_allocated += size;
41a88b5ba8SSam Ravnborg
42a88b5ba8SSam Ravnborg return ret;
43a88b5ba8SSam Ravnborg }
44a88b5ba8SSam Ravnborg
45a88b5ba8SSam Ravnborg /* The following routines deal with the black magic of fully naming a
46a88b5ba8SSam Ravnborg * node.
47a88b5ba8SSam Ravnborg *
48a88b5ba8SSam Ravnborg * Certain well known named nodes are just the simple name string.
49a88b5ba8SSam Ravnborg *
50a88b5ba8SSam Ravnborg * Actual devices have an address specifier appended to the base name
51a88b5ba8SSam Ravnborg * string, like this "foo@addr". The "addr" can be in any number of
52a88b5ba8SSam Ravnborg * formats, and the platform plus the type of the node determine the
53a88b5ba8SSam Ravnborg * format and how it is constructed.
54a88b5ba8SSam Ravnborg *
55a88b5ba8SSam Ravnborg * For children of the ROOT node, the naming convention is fixed and
56a88b5ba8SSam Ravnborg * determined by whether this is a sun4u or sun4v system.
57a88b5ba8SSam Ravnborg *
58a88b5ba8SSam Ravnborg * For children of other nodes, it is bus type specific. So
59a88b5ba8SSam Ravnborg * we walk up the tree until we discover a "device_type" property
60a88b5ba8SSam Ravnborg * we recognize and we go from there.
61a88b5ba8SSam Ravnborg *
62a88b5ba8SSam Ravnborg * As an example, the boot device on my workstation has a full path:
63a88b5ba8SSam Ravnborg *
64a88b5ba8SSam Ravnborg * /pci@1e,600000/ide@d/disk@0,0:c
65a88b5ba8SSam Ravnborg */
sun4v_path_component(struct device_node * dp,char * tmp_buf)66a88b5ba8SSam Ravnborg static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
67a88b5ba8SSam Ravnborg {
68bb31f9ebSRob Herring const char *name = of_get_property(dp, "name", NULL);
69a88b5ba8SSam Ravnborg struct linux_prom64_registers *regs;
70a88b5ba8SSam Ravnborg struct property *rprop;
71a88b5ba8SSam Ravnborg u32 high_bits, low_bits, type;
72a88b5ba8SSam Ravnborg
73a88b5ba8SSam Ravnborg rprop = of_find_property(dp, "reg", NULL);
74a88b5ba8SSam Ravnborg if (!rprop)
75a88b5ba8SSam Ravnborg return;
76a88b5ba8SSam Ravnborg
77a88b5ba8SSam Ravnborg regs = rprop->value;
78035ebefcSAndres Salomon if (!of_node_is_root(dp->parent)) {
79a06ecbfeSDavid S. Miller sprintf(tmp_buf, "%s@%x,%x",
80bb31f9ebSRob Herring name,
81a88b5ba8SSam Ravnborg (unsigned int) (regs->phys_addr >> 32UL),
82a88b5ba8SSam Ravnborg (unsigned int) (regs->phys_addr & 0xffffffffUL));
83a88b5ba8SSam Ravnborg return;
84a88b5ba8SSam Ravnborg }
85a88b5ba8SSam Ravnborg
86a88b5ba8SSam Ravnborg type = regs->phys_addr >> 60UL;
87a88b5ba8SSam Ravnborg high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
88a88b5ba8SSam Ravnborg low_bits = (regs->phys_addr & 0xffffffffUL);
89a88b5ba8SSam Ravnborg
90a88b5ba8SSam Ravnborg if (type == 0 || type == 8) {
91a88b5ba8SSam Ravnborg const char *prefix = (type == 0) ? "m" : "i";
92a88b5ba8SSam Ravnborg
93a88b5ba8SSam Ravnborg if (low_bits)
94a06ecbfeSDavid S. Miller sprintf(tmp_buf, "%s@%s%x,%x",
95bb31f9ebSRob Herring name, prefix,
96a88b5ba8SSam Ravnborg high_bits, low_bits);
97a88b5ba8SSam Ravnborg else
98a06ecbfeSDavid S. Miller sprintf(tmp_buf, "%s@%s%x",
99bb31f9ebSRob Herring name,
100a88b5ba8SSam Ravnborg prefix,
101a88b5ba8SSam Ravnborg high_bits);
102a88b5ba8SSam Ravnborg } else if (type == 12) {
103a06ecbfeSDavid S. Miller sprintf(tmp_buf, "%s@%x",
104bb31f9ebSRob Herring name, high_bits);
105a88b5ba8SSam Ravnborg }
106a88b5ba8SSam Ravnborg }
107a88b5ba8SSam Ravnborg
sun4u_path_component(struct device_node * dp,char * tmp_buf)108a88b5ba8SSam Ravnborg static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
109a88b5ba8SSam Ravnborg {
110bb31f9ebSRob Herring const char *name = of_get_property(dp, "name", NULL);
111a88b5ba8SSam Ravnborg struct linux_prom64_registers *regs;
112a88b5ba8SSam Ravnborg struct property *prop;
113a88b5ba8SSam Ravnborg
114a88b5ba8SSam Ravnborg prop = of_find_property(dp, "reg", NULL);
115a88b5ba8SSam Ravnborg if (!prop)
116a88b5ba8SSam Ravnborg return;
117a88b5ba8SSam Ravnborg
118a88b5ba8SSam Ravnborg regs = prop->value;
119035ebefcSAndres Salomon if (!of_node_is_root(dp->parent)) {
120a06ecbfeSDavid S. Miller sprintf(tmp_buf, "%s@%x,%x",
121bb31f9ebSRob Herring name,
122a88b5ba8SSam Ravnborg (unsigned int) (regs->phys_addr >> 32UL),
123a88b5ba8SSam Ravnborg (unsigned int) (regs->phys_addr & 0xffffffffUL));
124a88b5ba8SSam Ravnborg return;
125a88b5ba8SSam Ravnborg }
126a88b5ba8SSam Ravnborg
127a88b5ba8SSam Ravnborg prop = of_find_property(dp, "upa-portid", NULL);
128a88b5ba8SSam Ravnborg if (!prop)
129a88b5ba8SSam Ravnborg prop = of_find_property(dp, "portid", NULL);
130a88b5ba8SSam Ravnborg if (prop) {
131a88b5ba8SSam Ravnborg unsigned long mask = 0xffffffffUL;
132a88b5ba8SSam Ravnborg
133a88b5ba8SSam Ravnborg if (tlb_type >= cheetah)
134a88b5ba8SSam Ravnborg mask = 0x7fffff;
135a88b5ba8SSam Ravnborg
136a06ecbfeSDavid S. Miller sprintf(tmp_buf, "%s@%x,%x",
137bb31f9ebSRob Herring name,
138a88b5ba8SSam Ravnborg *(u32 *)prop->value,
139a88b5ba8SSam Ravnborg (unsigned int) (regs->phys_addr & mask));
140a88b5ba8SSam Ravnborg }
141a88b5ba8SSam Ravnborg }
142a88b5ba8SSam Ravnborg
143a88b5ba8SSam Ravnborg /* "name@slot,offset" */
sbus_path_component(struct device_node * dp,char * tmp_buf)144a88b5ba8SSam Ravnborg static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
145a88b5ba8SSam Ravnborg {
146bb31f9ebSRob Herring const char *name = of_get_property(dp, "name", NULL);
147a88b5ba8SSam Ravnborg struct linux_prom_registers *regs;
148a88b5ba8SSam Ravnborg struct property *prop;
149a88b5ba8SSam Ravnborg
150a88b5ba8SSam Ravnborg prop = of_find_property(dp, "reg", NULL);
151a88b5ba8SSam Ravnborg if (!prop)
152a88b5ba8SSam Ravnborg return;
153a88b5ba8SSam Ravnborg
154a88b5ba8SSam Ravnborg regs = prop->value;
155a06ecbfeSDavid S. Miller sprintf(tmp_buf, "%s@%x,%x",
156bb31f9ebSRob Herring name,
157a88b5ba8SSam Ravnborg regs->which_io,
158a88b5ba8SSam Ravnborg regs->phys_addr);
159a88b5ba8SSam Ravnborg }
160a88b5ba8SSam Ravnborg
161a88b5ba8SSam Ravnborg /* "name@devnum[,func]" */
pci_path_component(struct device_node * dp,char * tmp_buf)162a88b5ba8SSam Ravnborg static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
163a88b5ba8SSam Ravnborg {
164bb31f9ebSRob Herring const char *name = of_get_property(dp, "name", NULL);
165a88b5ba8SSam Ravnborg struct linux_prom_pci_registers *regs;
166a88b5ba8SSam Ravnborg struct property *prop;
167a88b5ba8SSam Ravnborg unsigned int devfn;
168a88b5ba8SSam Ravnborg
169a88b5ba8SSam Ravnborg prop = of_find_property(dp, "reg", NULL);
170a88b5ba8SSam Ravnborg if (!prop)
171a88b5ba8SSam Ravnborg return;
172a88b5ba8SSam Ravnborg
173a88b5ba8SSam Ravnborg regs = prop->value;
174a88b5ba8SSam Ravnborg devfn = (regs->phys_hi >> 8) & 0xff;
175a88b5ba8SSam Ravnborg if (devfn & 0x07) {
176a06ecbfeSDavid S. Miller sprintf(tmp_buf, "%s@%x,%x",
177bb31f9ebSRob Herring name,
178a88b5ba8SSam Ravnborg devfn >> 3,
179a88b5ba8SSam Ravnborg devfn & 0x07);
180a88b5ba8SSam Ravnborg } else {
181a06ecbfeSDavid S. Miller sprintf(tmp_buf, "%s@%x",
182bb31f9ebSRob Herring name,
183a88b5ba8SSam Ravnborg devfn >> 3);
184a88b5ba8SSam Ravnborg }
185a88b5ba8SSam Ravnborg }
186a88b5ba8SSam Ravnborg
187a88b5ba8SSam Ravnborg /* "name@UPA_PORTID,offset" */
upa_path_component(struct device_node * dp,char * tmp_buf)188a88b5ba8SSam Ravnborg static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
189a88b5ba8SSam Ravnborg {
190bb31f9ebSRob Herring const char *name = of_get_property(dp, "name", NULL);
191a88b5ba8SSam Ravnborg struct linux_prom64_registers *regs;
192a88b5ba8SSam Ravnborg struct property *prop;
193a88b5ba8SSam Ravnborg
194a88b5ba8SSam Ravnborg prop = of_find_property(dp, "reg", NULL);
195a88b5ba8SSam Ravnborg if (!prop)
196a88b5ba8SSam Ravnborg return;
197a88b5ba8SSam Ravnborg
198a88b5ba8SSam Ravnborg regs = prop->value;
199a88b5ba8SSam Ravnborg
200a88b5ba8SSam Ravnborg prop = of_find_property(dp, "upa-portid", NULL);
201a88b5ba8SSam Ravnborg if (!prop)
202a88b5ba8SSam Ravnborg return;
203a88b5ba8SSam Ravnborg
204a06ecbfeSDavid S. Miller sprintf(tmp_buf, "%s@%x,%x",
205bb31f9ebSRob Herring name,
206a88b5ba8SSam Ravnborg *(u32 *) prop->value,
207a88b5ba8SSam Ravnborg (unsigned int) (regs->phys_addr & 0xffffffffUL));
208a88b5ba8SSam Ravnborg }
209a88b5ba8SSam Ravnborg
210a88b5ba8SSam Ravnborg /* "name@reg" */
vdev_path_component(struct device_node * dp,char * tmp_buf)211a88b5ba8SSam Ravnborg static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
212a88b5ba8SSam Ravnborg {
213bb31f9ebSRob Herring const char *name = of_get_property(dp, "name", NULL);
214a88b5ba8SSam Ravnborg struct property *prop;
215a88b5ba8SSam Ravnborg u32 *regs;
216a88b5ba8SSam Ravnborg
217a88b5ba8SSam Ravnborg prop = of_find_property(dp, "reg", NULL);
218a88b5ba8SSam Ravnborg if (!prop)
219a88b5ba8SSam Ravnborg return;
220a88b5ba8SSam Ravnborg
221a88b5ba8SSam Ravnborg regs = prop->value;
222a88b5ba8SSam Ravnborg
223bb31f9ebSRob Herring sprintf(tmp_buf, "%s@%x", name, *regs);
224a88b5ba8SSam Ravnborg }
225a88b5ba8SSam Ravnborg
226a88b5ba8SSam Ravnborg /* "name@addrhi,addrlo" */
ebus_path_component(struct device_node * dp,char * tmp_buf)227a88b5ba8SSam Ravnborg static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
228a88b5ba8SSam Ravnborg {
229bb31f9ebSRob Herring const char *name = of_get_property(dp, "name", NULL);
230a88b5ba8SSam Ravnborg struct linux_prom64_registers *regs;
231a88b5ba8SSam Ravnborg struct property *prop;
232a88b5ba8SSam Ravnborg
233a88b5ba8SSam Ravnborg prop = of_find_property(dp, "reg", NULL);
234a88b5ba8SSam Ravnborg if (!prop)
235a88b5ba8SSam Ravnborg return;
236a88b5ba8SSam Ravnborg
237a88b5ba8SSam Ravnborg regs = prop->value;
238a88b5ba8SSam Ravnborg
239a06ecbfeSDavid S. Miller sprintf(tmp_buf, "%s@%x,%x",
240bb31f9ebSRob Herring name,
241a88b5ba8SSam Ravnborg (unsigned int) (regs->phys_addr >> 32UL),
242a88b5ba8SSam Ravnborg (unsigned int) (regs->phys_addr & 0xffffffffUL));
243a88b5ba8SSam Ravnborg }
244a88b5ba8SSam Ravnborg
245a88b5ba8SSam Ravnborg /* "name@bus,addr" */
i2c_path_component(struct device_node * dp,char * tmp_buf)246a88b5ba8SSam Ravnborg static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
247a88b5ba8SSam Ravnborg {
248bb31f9ebSRob Herring const char *name = of_get_property(dp, "name", NULL);
249a88b5ba8SSam Ravnborg struct property *prop;
250a88b5ba8SSam Ravnborg u32 *regs;
251a88b5ba8SSam Ravnborg
252a88b5ba8SSam Ravnborg prop = of_find_property(dp, "reg", NULL);
253a88b5ba8SSam Ravnborg if (!prop)
254a88b5ba8SSam Ravnborg return;
255a88b5ba8SSam Ravnborg
256a88b5ba8SSam Ravnborg regs = prop->value;
257a88b5ba8SSam Ravnborg
258a88b5ba8SSam Ravnborg /* This actually isn't right... should look at the #address-cells
259a88b5ba8SSam Ravnborg * property of the i2c bus node etc. etc.
260a88b5ba8SSam Ravnborg */
261a06ecbfeSDavid S. Miller sprintf(tmp_buf, "%s@%x,%x",
262bb31f9ebSRob Herring name, regs[0], regs[1]);
263a88b5ba8SSam Ravnborg }
264a88b5ba8SSam Ravnborg
265a88b5ba8SSam Ravnborg /* "name@reg0[,reg1]" */
usb_path_component(struct device_node * dp,char * tmp_buf)266a88b5ba8SSam Ravnborg static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
267a88b5ba8SSam Ravnborg {
268bb31f9ebSRob Herring const char *name = of_get_property(dp, "name", NULL);
269a88b5ba8SSam Ravnborg struct property *prop;
270a88b5ba8SSam Ravnborg u32 *regs;
271a88b5ba8SSam Ravnborg
272a88b5ba8SSam Ravnborg prop = of_find_property(dp, "reg", NULL);
273a88b5ba8SSam Ravnborg if (!prop)
274a88b5ba8SSam Ravnborg return;
275a88b5ba8SSam Ravnborg
276a88b5ba8SSam Ravnborg regs = prop->value;
277a88b5ba8SSam Ravnborg
278a88b5ba8SSam Ravnborg if (prop->length == sizeof(u32) || regs[1] == 1) {
279a06ecbfeSDavid S. Miller sprintf(tmp_buf, "%s@%x",
280bb31f9ebSRob Herring name, regs[0]);
281a88b5ba8SSam Ravnborg } else {
282a06ecbfeSDavid S. Miller sprintf(tmp_buf, "%s@%x,%x",
283bb31f9ebSRob Herring name, regs[0], regs[1]);
284a88b5ba8SSam Ravnborg }
285a88b5ba8SSam Ravnborg }
286a88b5ba8SSam Ravnborg
287a88b5ba8SSam Ravnborg /* "name@reg0reg1[,reg2reg3]" */
ieee1394_path_component(struct device_node * dp,char * tmp_buf)288a88b5ba8SSam Ravnborg static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
289a88b5ba8SSam Ravnborg {
290bb31f9ebSRob Herring const char *name = of_get_property(dp, "name", NULL);
291a88b5ba8SSam Ravnborg struct property *prop;
292a88b5ba8SSam Ravnborg u32 *regs;
293a88b5ba8SSam Ravnborg
294a88b5ba8SSam Ravnborg prop = of_find_property(dp, "reg", NULL);
295a88b5ba8SSam Ravnborg if (!prop)
296a88b5ba8SSam Ravnborg return;
297a88b5ba8SSam Ravnborg
298a88b5ba8SSam Ravnborg regs = prop->value;
299a88b5ba8SSam Ravnborg
300a88b5ba8SSam Ravnborg if (regs[2] || regs[3]) {
301a06ecbfeSDavid S. Miller sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
302bb31f9ebSRob Herring name, regs[0], regs[1], regs[2], regs[3]);
303a88b5ba8SSam Ravnborg } else {
304a06ecbfeSDavid S. Miller sprintf(tmp_buf, "%s@%08x%08x",
305bb31f9ebSRob Herring name, regs[0], regs[1]);
306a88b5ba8SSam Ravnborg }
307a88b5ba8SSam Ravnborg }
308a88b5ba8SSam Ravnborg
__build_path_component(struct device_node * dp,char * tmp_buf)309a88b5ba8SSam Ravnborg static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
310a88b5ba8SSam Ravnborg {
311a88b5ba8SSam Ravnborg struct device_node *parent = dp->parent;
312a88b5ba8SSam Ravnborg
313a88b5ba8SSam Ravnborg if (parent != NULL) {
31488ca0557SRob Herring if (of_node_is_type(parent, "pci") ||
31588ca0557SRob Herring of_node_is_type(parent, "pciex")) {
316a88b5ba8SSam Ravnborg pci_path_component(dp, tmp_buf);
317a88b5ba8SSam Ravnborg return;
318a88b5ba8SSam Ravnborg }
31988ca0557SRob Herring if (of_node_is_type(parent, "sbus")) {
320a88b5ba8SSam Ravnborg sbus_path_component(dp, tmp_buf);
321a88b5ba8SSam Ravnborg return;
322a88b5ba8SSam Ravnborg }
32388ca0557SRob Herring if (of_node_is_type(parent, "upa")) {
324a88b5ba8SSam Ravnborg upa_path_component(dp, tmp_buf);
325a88b5ba8SSam Ravnborg return;
326a88b5ba8SSam Ravnborg }
32788ca0557SRob Herring if (of_node_is_type(parent, "ebus")) {
328a88b5ba8SSam Ravnborg ebus_path_component(dp, tmp_buf);
329a88b5ba8SSam Ravnborg return;
330a88b5ba8SSam Ravnborg }
33129c990dfSRob Herring if (of_node_name_eq(parent, "usb") ||
33229c990dfSRob Herring of_node_name_eq(parent, "hub")) {
333a88b5ba8SSam Ravnborg usb_path_component(dp, tmp_buf);
334a88b5ba8SSam Ravnborg return;
335a88b5ba8SSam Ravnborg }
33688ca0557SRob Herring if (of_node_is_type(parent, "i2c")) {
337a88b5ba8SSam Ravnborg i2c_path_component(dp, tmp_buf);
338a88b5ba8SSam Ravnborg return;
339a88b5ba8SSam Ravnborg }
34088ca0557SRob Herring if (of_node_is_type(parent, "firewire")) {
341a88b5ba8SSam Ravnborg ieee1394_path_component(dp, tmp_buf);
342a88b5ba8SSam Ravnborg return;
343a88b5ba8SSam Ravnborg }
34488ca0557SRob Herring if (of_node_is_type(parent, "virtual-devices")) {
345a88b5ba8SSam Ravnborg vdev_path_component(dp, tmp_buf);
346a88b5ba8SSam Ravnborg return;
347a88b5ba8SSam Ravnborg }
348a88b5ba8SSam Ravnborg /* "isa" is handled with platform naming */
349a88b5ba8SSam Ravnborg }
350a88b5ba8SSam Ravnborg
351a88b5ba8SSam Ravnborg /* Use platform naming convention. */
352a88b5ba8SSam Ravnborg if (tlb_type == hypervisor) {
353a88b5ba8SSam Ravnborg sun4v_path_component(dp, tmp_buf);
354a88b5ba8SSam Ravnborg return;
355a88b5ba8SSam Ravnborg } else {
356a88b5ba8SSam Ravnborg sun4u_path_component(dp, tmp_buf);
357a88b5ba8SSam Ravnborg }
358a88b5ba8SSam Ravnborg }
359a88b5ba8SSam Ravnborg
build_path_component(struct device_node * dp)3606524036aSDavid S. Miller char * __init build_path_component(struct device_node *dp)
361a88b5ba8SSam Ravnborg {
362bb31f9ebSRob Herring const char *name = of_get_property(dp, "name", NULL);
363a88b5ba8SSam Ravnborg char tmp_buf[64], *n;
364a88b5ba8SSam Ravnborg
365a88b5ba8SSam Ravnborg tmp_buf[0] = '\0';
366a88b5ba8SSam Ravnborg __build_path_component(dp, tmp_buf);
367a88b5ba8SSam Ravnborg if (tmp_buf[0] == '\0')
368bb31f9ebSRob Herring strcpy(tmp_buf, name);
369a88b5ba8SSam Ravnborg
370a88b5ba8SSam Ravnborg n = prom_early_alloc(strlen(tmp_buf) + 1);
371a88b5ba8SSam Ravnborg strcpy(n, tmp_buf);
372a88b5ba8SSam Ravnborg
373a88b5ba8SSam Ravnborg return n;
374a88b5ba8SSam Ravnborg }
375a88b5ba8SSam Ravnborg
get_mid_prop(void)376a88b5ba8SSam Ravnborg static const char *get_mid_prop(void)
377a88b5ba8SSam Ravnborg {
378a88b5ba8SSam Ravnborg return (tlb_type == spitfire ? "upa-portid" : "portid");
379a88b5ba8SSam Ravnborg }
380a88b5ba8SSam Ravnborg
arch_find_n_match_cpu_physical_id(struct device_node * cpun,int cpu,unsigned int * thread)381d1cb9d1aSDavid Miller bool arch_find_n_match_cpu_physical_id(struct device_node *cpun,
382d1cb9d1aSDavid Miller int cpu, unsigned int *thread)
383d1cb9d1aSDavid Miller {
384d1cb9d1aSDavid Miller const char *mid_prop = get_mid_prop();
385d1cb9d1aSDavid Miller int this_cpu_id;
386d1cb9d1aSDavid Miller
387d1cb9d1aSDavid Miller /* On hypervisor based platforms we interrogate the 'reg'
388a718d139SPavel Tatashin * property. On everything else we look for a 'upa-portid',
389d1cb9d1aSDavid Miller * 'portid', or 'cpuid' property.
390d1cb9d1aSDavid Miller */
391d1cb9d1aSDavid Miller
392d1cb9d1aSDavid Miller if (tlb_type == hypervisor) {
393d1cb9d1aSDavid Miller struct property *prop = of_find_property(cpun, "reg", NULL);
394d1cb9d1aSDavid Miller u32 *regs;
395d1cb9d1aSDavid Miller
396d1cb9d1aSDavid Miller if (!prop) {
397d1cb9d1aSDavid Miller pr_warn("CPU node missing reg property\n");
398d1cb9d1aSDavid Miller return false;
399d1cb9d1aSDavid Miller }
400d1cb9d1aSDavid Miller regs = prop->value;
401d1cb9d1aSDavid Miller this_cpu_id = regs[0] & 0x0fffffff;
402d1cb9d1aSDavid Miller } else {
403d1cb9d1aSDavid Miller this_cpu_id = of_getintprop_default(cpun, mid_prop, -1);
404d1cb9d1aSDavid Miller
405d1cb9d1aSDavid Miller if (this_cpu_id < 0) {
406d1cb9d1aSDavid Miller mid_prop = "cpuid";
407d1cb9d1aSDavid Miller this_cpu_id = of_getintprop_default(cpun, mid_prop, -1);
408d1cb9d1aSDavid Miller }
409d1cb9d1aSDavid Miller if (this_cpu_id < 0) {
410d1cb9d1aSDavid Miller pr_warn("CPU node missing cpu ID property\n");
411d1cb9d1aSDavid Miller return false;
412d1cb9d1aSDavid Miller }
413d1cb9d1aSDavid Miller }
414d1cb9d1aSDavid Miller if (this_cpu_id == cpu) {
415d1cb9d1aSDavid Miller if (thread) {
416d1cb9d1aSDavid Miller int proc_id = cpu_data(cpu).proc_id;
417d1cb9d1aSDavid Miller
418d1cb9d1aSDavid Miller /* On sparc64, the cpu thread information is obtained
419d1cb9d1aSDavid Miller * either from OBP or the machine description. We've
420d1cb9d1aSDavid Miller * actually probed this information already long before
421d1cb9d1aSDavid Miller * this interface gets called so instead of interrogating
422d1cb9d1aSDavid Miller * both the OF node and the MDESC again, just use what
423d1cb9d1aSDavid Miller * we discovered already.
424d1cb9d1aSDavid Miller */
425d1cb9d1aSDavid Miller if (proc_id < 0)
426d1cb9d1aSDavid Miller proc_id = 0;
427d1cb9d1aSDavid Miller *thread = proc_id;
428d1cb9d1aSDavid Miller }
429d1cb9d1aSDavid Miller return true;
430d1cb9d1aSDavid Miller }
431d1cb9d1aSDavid Miller return false;
432d1cb9d1aSDavid Miller }
433d1cb9d1aSDavid Miller
of_iterate_over_cpus(void * (* func)(struct device_node *,int,int),int arg)4349bab5414SDavid S. Miller static void *of_iterate_over_cpus(void *(*func)(struct device_node *, int, int), int arg)
435a88b5ba8SSam Ravnborg {
436a88b5ba8SSam Ravnborg struct device_node *dp;
43723dc758eSDavid S. Miller const char *mid_prop;
438a88b5ba8SSam Ravnborg
43923dc758eSDavid S. Miller mid_prop = get_mid_prop();
440a88b5ba8SSam Ravnborg for_each_node_by_type(dp, "cpu") {
441a88b5ba8SSam Ravnborg int cpuid = of_getintprop_default(dp, mid_prop, -1);
442a88b5ba8SSam Ravnborg const char *this_mid_prop = mid_prop;
4439bab5414SDavid S. Miller void *ret;
444a88b5ba8SSam Ravnborg
445a88b5ba8SSam Ravnborg if (cpuid < 0) {
446a88b5ba8SSam Ravnborg this_mid_prop = "cpuid";
447a88b5ba8SSam Ravnborg cpuid = of_getintprop_default(dp, this_mid_prop, -1);
4489bab5414SDavid S. Miller }
4499bab5414SDavid S. Miller if (cpuid < 0) {
4509bab5414SDavid S. Miller prom_printf("OF: Serious problem, cpu lacks "
4519bab5414SDavid S. Miller "%s property", this_mid_prop);
4529bab5414SDavid S. Miller prom_halt();
4539bab5414SDavid S. Miller }
4549bab5414SDavid S. Miller #ifdef CONFIG_SMP
4559bab5414SDavid S. Miller if (cpuid >= NR_CPUS) {
4569bab5414SDavid S. Miller printk(KERN_WARNING "Ignoring CPU %d which is "
4579bab5414SDavid S. Miller ">= NR_CPUS (%d)\n",
4589bab5414SDavid S. Miller cpuid, NR_CPUS);
4599bab5414SDavid S. Miller continue;
4609bab5414SDavid S. Miller }
4619bab5414SDavid S. Miller #endif
4629bab5414SDavid S. Miller ret = func(dp, cpuid, arg);
4639bab5414SDavid S. Miller if (ret)
4649bab5414SDavid S. Miller return ret;
4659bab5414SDavid S. Miller }
4669bab5414SDavid S. Miller return NULL;
4679bab5414SDavid S. Miller }
4689bab5414SDavid S. Miller
check_cpu_node(struct device_node * dp,int cpuid,int id)4699bab5414SDavid S. Miller static void *check_cpu_node(struct device_node *dp, int cpuid, int id)
4709bab5414SDavid S. Miller {
4719bab5414SDavid S. Miller if (id == cpuid)
4729bab5414SDavid S. Miller return dp;
4739bab5414SDavid S. Miller return NULL;
4749bab5414SDavid S. Miller }
4759bab5414SDavid S. Miller
of_find_node_by_cpuid(int cpuid)4769bab5414SDavid S. Miller struct device_node *of_find_node_by_cpuid(int cpuid)
4779bab5414SDavid S. Miller {
4789bab5414SDavid S. Miller return of_iterate_over_cpus(check_cpu_node, cpuid);
4799bab5414SDavid S. Miller }
4809bab5414SDavid S. Miller
record_one_cpu(struct device_node * dp,int cpuid,int arg)4819bab5414SDavid S. Miller static void *record_one_cpu(struct device_node *dp, int cpuid, int arg)
4829bab5414SDavid S. Miller {
4839bab5414SDavid S. Miller ncpus_probed++;
4849bab5414SDavid S. Miller #ifdef CONFIG_SMP
4859bab5414SDavid S. Miller set_cpu_present(cpuid, true);
486*3e64c37fSSam Ravnborg
487*3e64c37fSSam Ravnborg if (num_possible_cpus() < nr_cpu_ids)
4889bab5414SDavid S. Miller set_cpu_possible(cpuid, true);
4899bab5414SDavid S. Miller #endif
4909bab5414SDavid S. Miller return NULL;
4919bab5414SDavid S. Miller }
4929bab5414SDavid S. Miller
of_populate_present_mask(void)4939bab5414SDavid S. Miller void __init of_populate_present_mask(void)
4949bab5414SDavid S. Miller {
4959bab5414SDavid S. Miller if (tlb_type == hypervisor)
4969bab5414SDavid S. Miller return;
4979bab5414SDavid S. Miller
4989bab5414SDavid S. Miller ncpus_probed = 0;
4999bab5414SDavid S. Miller of_iterate_over_cpus(record_one_cpu, 0);
5009bab5414SDavid S. Miller }
5019bab5414SDavid S. Miller
fill_in_one_cpu(struct device_node * dp,int cpuid,int arg)5029bab5414SDavid S. Miller static void *fill_in_one_cpu(struct device_node *dp, int cpuid, int arg)
5039bab5414SDavid S. Miller {
5049bab5414SDavid S. Miller struct device_node *portid_parent = NULL;
5059bab5414SDavid S. Miller int portid = -1;
5069bab5414SDavid S. Miller
5076a71ca74SRob Herring if (of_property_present(dp, "cpuid")) {
508a88b5ba8SSam Ravnborg int limit = 2;
509a88b5ba8SSam Ravnborg
510a88b5ba8SSam Ravnborg portid_parent = dp;
511a88b5ba8SSam Ravnborg while (limit--) {
512a88b5ba8SSam Ravnborg portid_parent = portid_parent->parent;
513a88b5ba8SSam Ravnborg if (!portid_parent)
514a88b5ba8SSam Ravnborg break;
515a88b5ba8SSam Ravnborg portid = of_getintprop_default(portid_parent,
516a88b5ba8SSam Ravnborg "portid", -1);
517a88b5ba8SSam Ravnborg if (portid >= 0)
518a88b5ba8SSam Ravnborg break;
519a88b5ba8SSam Ravnborg }
520a88b5ba8SSam Ravnborg }
521a88b5ba8SSam Ravnborg
5229bab5414SDavid S. Miller #ifndef CONFIG_SMP
523a88b5ba8SSam Ravnborg /* On uniprocessor we only want the values for the
524a88b5ba8SSam Ravnborg * real physical cpu the kernel booted onto, however
525a88b5ba8SSam Ravnborg * cpu_data() only has one entry at index 0.
526a88b5ba8SSam Ravnborg */
527a88b5ba8SSam Ravnborg if (cpuid != real_hard_smp_processor_id())
5289bab5414SDavid S. Miller return NULL;
529a88b5ba8SSam Ravnborg cpuid = 0;
530a88b5ba8SSam Ravnborg #endif
531a88b5ba8SSam Ravnborg
532a88b5ba8SSam Ravnborg cpu_data(cpuid).clock_tick =
533a88b5ba8SSam Ravnborg of_getintprop_default(dp, "clock-frequency", 0);
534a88b5ba8SSam Ravnborg
535a88b5ba8SSam Ravnborg if (portid_parent) {
536a88b5ba8SSam Ravnborg cpu_data(cpuid).dcache_size =
537a88b5ba8SSam Ravnborg of_getintprop_default(dp, "l1-dcache-size",
538a88b5ba8SSam Ravnborg 16 * 1024);
539a88b5ba8SSam Ravnborg cpu_data(cpuid).dcache_line_size =
540a88b5ba8SSam Ravnborg of_getintprop_default(dp, "l1-dcache-line-size",
541a88b5ba8SSam Ravnborg 32);
542a88b5ba8SSam Ravnborg cpu_data(cpuid).icache_size =
543a88b5ba8SSam Ravnborg of_getintprop_default(dp, "l1-icache-size",
544a88b5ba8SSam Ravnborg 8 * 1024);
545a88b5ba8SSam Ravnborg cpu_data(cpuid).icache_line_size =
546a88b5ba8SSam Ravnborg of_getintprop_default(dp, "l1-icache-line-size",
547a88b5ba8SSam Ravnborg 32);
548a88b5ba8SSam Ravnborg cpu_data(cpuid).ecache_size =
549a88b5ba8SSam Ravnborg of_getintprop_default(dp, "l2-cache-size", 0);
550a88b5ba8SSam Ravnborg cpu_data(cpuid).ecache_line_size =
551a88b5ba8SSam Ravnborg of_getintprop_default(dp, "l2-cache-line-size", 0);
552a88b5ba8SSam Ravnborg if (!cpu_data(cpuid).ecache_size ||
553a88b5ba8SSam Ravnborg !cpu_data(cpuid).ecache_line_size) {
554a88b5ba8SSam Ravnborg cpu_data(cpuid).ecache_size =
555a88b5ba8SSam Ravnborg of_getintprop_default(portid_parent,
556a88b5ba8SSam Ravnborg "l2-cache-size",
557a88b5ba8SSam Ravnborg (4 * 1024 * 1024));
558a88b5ba8SSam Ravnborg cpu_data(cpuid).ecache_line_size =
559a88b5ba8SSam Ravnborg of_getintprop_default(portid_parent,
560a88b5ba8SSam Ravnborg "l2-cache-line-size", 64);
561a88b5ba8SSam Ravnborg }
562a88b5ba8SSam Ravnborg
563a88b5ba8SSam Ravnborg cpu_data(cpuid).core_id = portid + 1;
564a88b5ba8SSam Ravnborg cpu_data(cpuid).proc_id = portid;
565a88b5ba8SSam Ravnborg } else {
566a88b5ba8SSam Ravnborg cpu_data(cpuid).dcache_size =
567a88b5ba8SSam Ravnborg of_getintprop_default(dp, "dcache-size", 16 * 1024);
568a88b5ba8SSam Ravnborg cpu_data(cpuid).dcache_line_size =
569a88b5ba8SSam Ravnborg of_getintprop_default(dp, "dcache-line-size", 32);
570a88b5ba8SSam Ravnborg
571a88b5ba8SSam Ravnborg cpu_data(cpuid).icache_size =
572a88b5ba8SSam Ravnborg of_getintprop_default(dp, "icache-size", 16 * 1024);
573a88b5ba8SSam Ravnborg cpu_data(cpuid).icache_line_size =
574a88b5ba8SSam Ravnborg of_getintprop_default(dp, "icache-line-size", 32);
575a88b5ba8SSam Ravnborg
576a88b5ba8SSam Ravnborg cpu_data(cpuid).ecache_size =
577a88b5ba8SSam Ravnborg of_getintprop_default(dp, "ecache-size",
578a88b5ba8SSam Ravnborg (4 * 1024 * 1024));
579a88b5ba8SSam Ravnborg cpu_data(cpuid).ecache_line_size =
580a88b5ba8SSam Ravnborg of_getintprop_default(dp, "ecache-line-size", 64);
581a88b5ba8SSam Ravnborg
582a88b5ba8SSam Ravnborg cpu_data(cpuid).core_id = 0;
583a88b5ba8SSam Ravnborg cpu_data(cpuid).proc_id = -1;
584a88b5ba8SSam Ravnborg }
585a88b5ba8SSam Ravnborg
5869bab5414SDavid S. Miller return NULL;
587a88b5ba8SSam Ravnborg }
588a88b5ba8SSam Ravnborg
of_fill_in_cpu_data(void)5899bab5414SDavid S. Miller void __init of_fill_in_cpu_data(void)
5909bab5414SDavid S. Miller {
5919bab5414SDavid S. Miller if (tlb_type == hypervisor)
5929bab5414SDavid S. Miller return;
5939bab5414SDavid S. Miller
5949bab5414SDavid S. Miller of_iterate_over_cpus(fill_in_one_cpu, 0);
5959bab5414SDavid S. Miller
596a88b5ba8SSam Ravnborg smp_fill_in_sib_core_maps();
597a88b5ba8SSam Ravnborg }
598a88b5ba8SSam Ravnborg
of_console_init(void)59923dc758eSDavid S. Miller void __init of_console_init(void)
600a88b5ba8SSam Ravnborg {
601a88b5ba8SSam Ravnborg char *msg = "OF stdout device is: %s\n";
602a88b5ba8SSam Ravnborg struct device_node *dp;
603a88b5ba8SSam Ravnborg phandle node;
604a88b5ba8SSam Ravnborg
605a88b5ba8SSam Ravnborg of_console_path = prom_early_alloc(256);
606a88b5ba8SSam Ravnborg if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) {
607a88b5ba8SSam Ravnborg prom_printf("Cannot obtain path of stdout.\n");
608a88b5ba8SSam Ravnborg prom_halt();
609a88b5ba8SSam Ravnborg }
610a88b5ba8SSam Ravnborg of_console_options = strrchr(of_console_path, ':');
611a88b5ba8SSam Ravnborg if (of_console_options) {
612a88b5ba8SSam Ravnborg of_console_options++;
613a88b5ba8SSam Ravnborg if (*of_console_options == '\0')
614a88b5ba8SSam Ravnborg of_console_options = NULL;
615a88b5ba8SSam Ravnborg }
616a88b5ba8SSam Ravnborg
617a88b5ba8SSam Ravnborg node = prom_inst2pkg(prom_stdout);
618a88b5ba8SSam Ravnborg if (!node) {
619a88b5ba8SSam Ravnborg prom_printf("Cannot resolve stdout node from "
620a88b5ba8SSam Ravnborg "instance %08x.\n", prom_stdout);
621a88b5ba8SSam Ravnborg prom_halt();
622a88b5ba8SSam Ravnborg }
623a88b5ba8SSam Ravnborg
624a88b5ba8SSam Ravnborg dp = of_find_node_by_phandle(node);
625a88b5ba8SSam Ravnborg
62688ca0557SRob Herring if (!of_node_is_type(dp, "display") && !of_node_is_type(dp, "serial")) {
627a88b5ba8SSam Ravnborg prom_printf("Console device_type is neither display "
628a88b5ba8SSam Ravnborg "nor serial.\n");
629a88b5ba8SSam Ravnborg prom_halt();
630a88b5ba8SSam Ravnborg }
631a88b5ba8SSam Ravnborg
632a88b5ba8SSam Ravnborg of_console_device = dp;
633a88b5ba8SSam Ravnborg
634a88b5ba8SSam Ravnborg printk(msg, of_console_path);
635a88b5ba8SSam Ravnborg }
636