• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Procedures for creating, accessing and interpreting the device tree.
3  *
4  * Paul Mackerras	August 1996.
5  * Copyright (C) 1996-2005 Paul Mackerras.
6  *
7  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8  *    {engebret|bergner}@us.ibm.com
9  *
10  *  Adapted for sparc64 by David S. Miller davem@davemloft.net
11  *
12  *      This program is free software; you can redistribute it and/or
13  *      modify it under the terms of the GNU General Public License
14  *      as published by the Free Software Foundation; either version
15  *      2 of the License, or (at your option) any later version.
16  */
17 
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/string.h>
21 #include <linux/mm.h>
22 #include <linux/module.h>
23 #include <linux/lmb.h>
24 #include <linux/of_device.h>
25 
26 #include <asm/prom.h>
27 #include <asm/oplib.h>
28 #include <asm/irq.h>
29 #include <asm/asi.h>
30 #include <asm/upa.h>
31 #include <asm/smp.h>
32 
33 #include "prom.h"
34 
prom_early_alloc(unsigned long size)35 void * __init prom_early_alloc(unsigned long size)
36 {
37 	unsigned long paddr = lmb_alloc(size, SMP_CACHE_BYTES);
38 	void *ret;
39 
40 	if (!paddr) {
41 		prom_printf("prom_early_alloc(%lu) failed\n");
42 		prom_halt();
43 	}
44 
45 	ret = __va(paddr);
46 	memset(ret, 0, size);
47 	prom_early_allocated += size;
48 
49 	return ret;
50 }
51 
52 /* The following routines deal with the black magic of fully naming a
53  * node.
54  *
55  * Certain well known named nodes are just the simple name string.
56  *
57  * Actual devices have an address specifier appended to the base name
58  * string, like this "foo@addr".  The "addr" can be in any number of
59  * formats, and the platform plus the type of the node determine the
60  * format and how it is constructed.
61  *
62  * For children of the ROOT node, the naming convention is fixed and
63  * determined by whether this is a sun4u or sun4v system.
64  *
65  * For children of other nodes, it is bus type specific.  So
66  * we walk up the tree until we discover a "device_type" property
67  * we recognize and we go from there.
68  *
69  * As an example, the boot device on my workstation has a full path:
70  *
71  *	/pci@1e,600000/ide@d/disk@0,0:c
72  */
sun4v_path_component(struct device_node * dp,char * tmp_buf)73 static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
74 {
75 	struct linux_prom64_registers *regs;
76 	struct property *rprop;
77 	u32 high_bits, low_bits, type;
78 
79 	rprop = of_find_property(dp, "reg", NULL);
80 	if (!rprop)
81 		return;
82 
83 	regs = rprop->value;
84 	if (!is_root_node(dp->parent)) {
85 		sprintf(tmp_buf, "%s@%x,%x",
86 			dp->name,
87 			(unsigned int) (regs->phys_addr >> 32UL),
88 			(unsigned int) (regs->phys_addr & 0xffffffffUL));
89 		return;
90 	}
91 
92 	type = regs->phys_addr >> 60UL;
93 	high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
94 	low_bits = (regs->phys_addr & 0xffffffffUL);
95 
96 	if (type == 0 || type == 8) {
97 		const char *prefix = (type == 0) ? "m" : "i";
98 
99 		if (low_bits)
100 			sprintf(tmp_buf, "%s@%s%x,%x",
101 				dp->name, prefix,
102 				high_bits, low_bits);
103 		else
104 			sprintf(tmp_buf, "%s@%s%x",
105 				dp->name,
106 				prefix,
107 				high_bits);
108 	} else if (type == 12) {
109 		sprintf(tmp_buf, "%s@%x",
110 			dp->name, high_bits);
111 	}
112 }
113 
sun4u_path_component(struct device_node * dp,char * tmp_buf)114 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
115 {
116 	struct linux_prom64_registers *regs;
117 	struct property *prop;
118 
119 	prop = of_find_property(dp, "reg", NULL);
120 	if (!prop)
121 		return;
122 
123 	regs = prop->value;
124 	if (!is_root_node(dp->parent)) {
125 		sprintf(tmp_buf, "%s@%x,%x",
126 			dp->name,
127 			(unsigned int) (regs->phys_addr >> 32UL),
128 			(unsigned int) (regs->phys_addr & 0xffffffffUL));
129 		return;
130 	}
131 
132 	prop = of_find_property(dp, "upa-portid", NULL);
133 	if (!prop)
134 		prop = of_find_property(dp, "portid", NULL);
135 	if (prop) {
136 		unsigned long mask = 0xffffffffUL;
137 
138 		if (tlb_type >= cheetah)
139 			mask = 0x7fffff;
140 
141 		sprintf(tmp_buf, "%s@%x,%x",
142 			dp->name,
143 			*(u32 *)prop->value,
144 			(unsigned int) (regs->phys_addr & mask));
145 	}
146 }
147 
148 /* "name@slot,offset"  */
sbus_path_component(struct device_node * dp,char * tmp_buf)149 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
150 {
151 	struct linux_prom_registers *regs;
152 	struct property *prop;
153 
154 	prop = of_find_property(dp, "reg", NULL);
155 	if (!prop)
156 		return;
157 
158 	regs = prop->value;
159 	sprintf(tmp_buf, "%s@%x,%x",
160 		dp->name,
161 		regs->which_io,
162 		regs->phys_addr);
163 }
164 
165 /* "name@devnum[,func]" */
pci_path_component(struct device_node * dp,char * tmp_buf)166 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
167 {
168 	struct linux_prom_pci_registers *regs;
169 	struct property *prop;
170 	unsigned int devfn;
171 
172 	prop = of_find_property(dp, "reg", NULL);
173 	if (!prop)
174 		return;
175 
176 	regs = prop->value;
177 	devfn = (regs->phys_hi >> 8) & 0xff;
178 	if (devfn & 0x07) {
179 		sprintf(tmp_buf, "%s@%x,%x",
180 			dp->name,
181 			devfn >> 3,
182 			devfn & 0x07);
183 	} else {
184 		sprintf(tmp_buf, "%s@%x",
185 			dp->name,
186 			devfn >> 3);
187 	}
188 }
189 
190 /* "name@UPA_PORTID,offset" */
upa_path_component(struct device_node * dp,char * tmp_buf)191 static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
192 {
193 	struct linux_prom64_registers *regs;
194 	struct property *prop;
195 
196 	prop = of_find_property(dp, "reg", NULL);
197 	if (!prop)
198 		return;
199 
200 	regs = prop->value;
201 
202 	prop = of_find_property(dp, "upa-portid", NULL);
203 	if (!prop)
204 		return;
205 
206 	sprintf(tmp_buf, "%s@%x,%x",
207 		dp->name,
208 		*(u32 *) prop->value,
209 		(unsigned int) (regs->phys_addr & 0xffffffffUL));
210 }
211 
212 /* "name@reg" */
vdev_path_component(struct device_node * dp,char * tmp_buf)213 static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
214 {
215 	struct property *prop;
216 	u32 *regs;
217 
218 	prop = of_find_property(dp, "reg", NULL);
219 	if (!prop)
220 		return;
221 
222 	regs = prop->value;
223 
224 	sprintf(tmp_buf, "%s@%x", dp->name, *regs);
225 }
226 
227 /* "name@addrhi,addrlo" */
ebus_path_component(struct device_node * dp,char * tmp_buf)228 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
229 {
230 	struct linux_prom64_registers *regs;
231 	struct property *prop;
232 
233 	prop = of_find_property(dp, "reg", NULL);
234 	if (!prop)
235 		return;
236 
237 	regs = prop->value;
238 
239 	sprintf(tmp_buf, "%s@%x,%x",
240 		dp->name,
241 		(unsigned int) (regs->phys_addr >> 32UL),
242 		(unsigned int) (regs->phys_addr & 0xffffffffUL));
243 }
244 
245 /* "name@bus,addr" */
i2c_path_component(struct device_node * dp,char * tmp_buf)246 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
247 {
248 	struct property *prop;
249 	u32 *regs;
250 
251 	prop = of_find_property(dp, "reg", NULL);
252 	if (!prop)
253 		return;
254 
255 	regs = prop->value;
256 
257 	/* This actually isn't right... should look at the #address-cells
258 	 * property of the i2c bus node etc. etc.
259 	 */
260 	sprintf(tmp_buf, "%s@%x,%x",
261 		dp->name, regs[0], regs[1]);
262 }
263 
264 /* "name@reg0[,reg1]" */
usb_path_component(struct device_node * dp,char * tmp_buf)265 static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
266 {
267 	struct property *prop;
268 	u32 *regs;
269 
270 	prop = of_find_property(dp, "reg", NULL);
271 	if (!prop)
272 		return;
273 
274 	regs = prop->value;
275 
276 	if (prop->length == sizeof(u32) || regs[1] == 1) {
277 		sprintf(tmp_buf, "%s@%x",
278 			dp->name, regs[0]);
279 	} else {
280 		sprintf(tmp_buf, "%s@%x,%x",
281 			dp->name, regs[0], regs[1]);
282 	}
283 }
284 
285 /* "name@reg0reg1[,reg2reg3]" */
ieee1394_path_component(struct device_node * dp,char * tmp_buf)286 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
287 {
288 	struct property *prop;
289 	u32 *regs;
290 
291 	prop = of_find_property(dp, "reg", NULL);
292 	if (!prop)
293 		return;
294 
295 	regs = prop->value;
296 
297 	if (regs[2] || regs[3]) {
298 		sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
299 			dp->name, regs[0], regs[1], regs[2], regs[3]);
300 	} else {
301 		sprintf(tmp_buf, "%s@%08x%08x",
302 			dp->name, regs[0], regs[1]);
303 	}
304 }
305 
__build_path_component(struct device_node * dp,char * tmp_buf)306 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
307 {
308 	struct device_node *parent = dp->parent;
309 
310 	if (parent != NULL) {
311 		if (!strcmp(parent->type, "pci") ||
312 		    !strcmp(parent->type, "pciex")) {
313 			pci_path_component(dp, tmp_buf);
314 			return;
315 		}
316 		if (!strcmp(parent->type, "sbus")) {
317 			sbus_path_component(dp, tmp_buf);
318 			return;
319 		}
320 		if (!strcmp(parent->type, "upa")) {
321 			upa_path_component(dp, tmp_buf);
322 			return;
323 		}
324 		if (!strcmp(parent->type, "ebus")) {
325 			ebus_path_component(dp, tmp_buf);
326 			return;
327 		}
328 		if (!strcmp(parent->name, "usb") ||
329 		    !strcmp(parent->name, "hub")) {
330 			usb_path_component(dp, tmp_buf);
331 			return;
332 		}
333 		if (!strcmp(parent->type, "i2c")) {
334 			i2c_path_component(dp, tmp_buf);
335 			return;
336 		}
337 		if (!strcmp(parent->type, "firewire")) {
338 			ieee1394_path_component(dp, tmp_buf);
339 			return;
340 		}
341 		if (!strcmp(parent->type, "virtual-devices")) {
342 			vdev_path_component(dp, tmp_buf);
343 			return;
344 		}
345 		/* "isa" is handled with platform naming */
346 	}
347 
348 	/* Use platform naming convention.  */
349 	if (tlb_type == hypervisor) {
350 		sun4v_path_component(dp, tmp_buf);
351 		return;
352 	} else {
353 		sun4u_path_component(dp, tmp_buf);
354 	}
355 }
356 
build_path_component(struct device_node * dp)357 char * __init build_path_component(struct device_node *dp)
358 {
359 	char tmp_buf[64], *n;
360 
361 	tmp_buf[0] = '\0';
362 	__build_path_component(dp, tmp_buf);
363 	if (tmp_buf[0] == '\0')
364 		strcpy(tmp_buf, dp->name);
365 
366 	n = prom_early_alloc(strlen(tmp_buf) + 1);
367 	strcpy(n, tmp_buf);
368 
369 	return n;
370 }
371 
get_mid_prop(void)372 static const char *get_mid_prop(void)
373 {
374 	return (tlb_type == spitfire ? "upa-portid" : "portid");
375 }
376 
of_find_node_by_cpuid(int cpuid)377 struct device_node *of_find_node_by_cpuid(int cpuid)
378 {
379 	struct device_node *dp;
380 	const char *mid_prop = get_mid_prop();
381 
382 	for_each_node_by_type(dp, "cpu") {
383 		int id = of_getintprop_default(dp, mid_prop, -1);
384 		const char *this_mid_prop = mid_prop;
385 
386 		if (id < 0) {
387 			this_mid_prop = "cpuid";
388 			id = of_getintprop_default(dp, this_mid_prop, -1);
389 		}
390 
391 		if (id < 0) {
392 			prom_printf("OF: Serious problem, cpu lacks "
393 				    "%s property", this_mid_prop);
394 			prom_halt();
395 		}
396 		if (cpuid == id)
397 			return dp;
398 	}
399 	return NULL;
400 }
401 
of_fill_in_cpu_data(void)402 void __init of_fill_in_cpu_data(void)
403 {
404 	struct device_node *dp;
405 	const char *mid_prop;
406 
407 	if (tlb_type == hypervisor)
408 		return;
409 
410 	mid_prop = get_mid_prop();
411 	ncpus_probed = 0;
412 	for_each_node_by_type(dp, "cpu") {
413 		int cpuid = of_getintprop_default(dp, mid_prop, -1);
414 		const char *this_mid_prop = mid_prop;
415 		struct device_node *portid_parent;
416 		int portid = -1;
417 
418 		portid_parent = NULL;
419 		if (cpuid < 0) {
420 			this_mid_prop = "cpuid";
421 			cpuid = of_getintprop_default(dp, this_mid_prop, -1);
422 			if (cpuid >= 0) {
423 				int limit = 2;
424 
425 				portid_parent = dp;
426 				while (limit--) {
427 					portid_parent = portid_parent->parent;
428 					if (!portid_parent)
429 						break;
430 					portid = of_getintprop_default(portid_parent,
431 								       "portid", -1);
432 					if (portid >= 0)
433 						break;
434 				}
435 			}
436 		}
437 
438 		if (cpuid < 0) {
439 			prom_printf("OF: Serious problem, cpu lacks "
440 				    "%s property", this_mid_prop);
441 			prom_halt();
442 		}
443 
444 		ncpus_probed++;
445 
446 #ifdef CONFIG_SMP
447 		if (cpuid >= NR_CPUS) {
448 			printk(KERN_WARNING "Ignoring CPU %d which is "
449 			       ">= NR_CPUS (%d)\n",
450 			       cpuid, NR_CPUS);
451 			continue;
452 		}
453 #else
454 		/* On uniprocessor we only want the values for the
455 		 * real physical cpu the kernel booted onto, however
456 		 * cpu_data() only has one entry at index 0.
457 		 */
458 		if (cpuid != real_hard_smp_processor_id())
459 			continue;
460 		cpuid = 0;
461 #endif
462 
463 		cpu_data(cpuid).clock_tick =
464 			of_getintprop_default(dp, "clock-frequency", 0);
465 
466 		if (portid_parent) {
467 			cpu_data(cpuid).dcache_size =
468 				of_getintprop_default(dp, "l1-dcache-size",
469 						      16 * 1024);
470 			cpu_data(cpuid).dcache_line_size =
471 				of_getintprop_default(dp, "l1-dcache-line-size",
472 						      32);
473 			cpu_data(cpuid).icache_size =
474 				of_getintprop_default(dp, "l1-icache-size",
475 						      8 * 1024);
476 			cpu_data(cpuid).icache_line_size =
477 				of_getintprop_default(dp, "l1-icache-line-size",
478 						      32);
479 			cpu_data(cpuid).ecache_size =
480 				of_getintprop_default(dp, "l2-cache-size", 0);
481 			cpu_data(cpuid).ecache_line_size =
482 				of_getintprop_default(dp, "l2-cache-line-size", 0);
483 			if (!cpu_data(cpuid).ecache_size ||
484 			    !cpu_data(cpuid).ecache_line_size) {
485 				cpu_data(cpuid).ecache_size =
486 					of_getintprop_default(portid_parent,
487 							      "l2-cache-size",
488 							      (4 * 1024 * 1024));
489 				cpu_data(cpuid).ecache_line_size =
490 					of_getintprop_default(portid_parent,
491 							      "l2-cache-line-size", 64);
492 			}
493 
494 			cpu_data(cpuid).core_id = portid + 1;
495 			cpu_data(cpuid).proc_id = portid;
496 #ifdef CONFIG_SMP
497 			sparc64_multi_core = 1;
498 #endif
499 		} else {
500 			cpu_data(cpuid).dcache_size =
501 				of_getintprop_default(dp, "dcache-size", 16 * 1024);
502 			cpu_data(cpuid).dcache_line_size =
503 				of_getintprop_default(dp, "dcache-line-size", 32);
504 
505 			cpu_data(cpuid).icache_size =
506 				of_getintprop_default(dp, "icache-size", 16 * 1024);
507 			cpu_data(cpuid).icache_line_size =
508 				of_getintprop_default(dp, "icache-line-size", 32);
509 
510 			cpu_data(cpuid).ecache_size =
511 				of_getintprop_default(dp, "ecache-size",
512 						      (4 * 1024 * 1024));
513 			cpu_data(cpuid).ecache_line_size =
514 				of_getintprop_default(dp, "ecache-line-size", 64);
515 
516 			cpu_data(cpuid).core_id = 0;
517 			cpu_data(cpuid).proc_id = -1;
518 		}
519 
520 #ifdef CONFIG_SMP
521 		cpu_set(cpuid, cpu_present_map);
522 		cpu_set(cpuid, cpu_possible_map);
523 #endif
524 	}
525 
526 	smp_fill_in_sib_core_maps();
527 }
528 
of_console_init(void)529 void __init of_console_init(void)
530 {
531 	char *msg = "OF stdout device is: %s\n";
532 	struct device_node *dp;
533 	const char *type;
534 	phandle node;
535 
536 	of_console_path = prom_early_alloc(256);
537 	if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) {
538 		prom_printf("Cannot obtain path of stdout.\n");
539 		prom_halt();
540 	}
541 	of_console_options = strrchr(of_console_path, ':');
542 	if (of_console_options) {
543 		of_console_options++;
544 		if (*of_console_options == '\0')
545 			of_console_options = NULL;
546 	}
547 
548 	node = prom_inst2pkg(prom_stdout);
549 	if (!node) {
550 		prom_printf("Cannot resolve stdout node from "
551 			    "instance %08x.\n", prom_stdout);
552 		prom_halt();
553 	}
554 
555 	dp = of_find_node_by_phandle(node);
556 	type = of_get_property(dp, "device_type", NULL);
557 	if (!type) {
558 		prom_printf("Console stdout lacks device_type property.\n");
559 		prom_halt();
560 	}
561 
562 	if (strcmp(type, "display") && strcmp(type, "serial")) {
563 		prom_printf("Console device_type is neither display "
564 			    "nor serial.\n");
565 		prom_halt();
566 	}
567 
568 	of_console_device = dp;
569 
570 	printk(msg, of_console_path);
571 }
572