xref: /openbmc/linux/arch/sparc/kernel/prom_64.c (revision ae213c44)
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/memblock.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/types.h>
22 #include <linux/cpu.h>
23 #include <linux/mm.h>
24 #include <linux/of.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 
35 void * __init prom_early_alloc(unsigned long size)
36 {
37 	void *ret = memblock_alloc(size, SMP_CACHE_BYTES);
38 
39 	if (!ret) {
40 		prom_printf("prom_early_alloc(%lu) failed\n", size);
41 		prom_halt();
42 	}
43 
44 	prom_early_allocated += size;
45 
46 	return ret;
47 }
48 
49 /* The following routines deal with the black magic of fully naming a
50  * node.
51  *
52  * Certain well known named nodes are just the simple name string.
53  *
54  * Actual devices have an address specifier appended to the base name
55  * string, like this "foo@addr".  The "addr" can be in any number of
56  * formats, and the platform plus the type of the node determine the
57  * format and how it is constructed.
58  *
59  * For children of the ROOT node, the naming convention is fixed and
60  * determined by whether this is a sun4u or sun4v system.
61  *
62  * For children of other nodes, it is bus type specific.  So
63  * we walk up the tree until we discover a "device_type" property
64  * we recognize and we go from there.
65  *
66  * As an example, the boot device on my workstation has a full path:
67  *
68  *	/pci@1e,600000/ide@d/disk@0,0:c
69  */
70 static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
71 {
72 	const char *name = of_get_property(dp, "name", NULL);
73 	struct linux_prom64_registers *regs;
74 	struct property *rprop;
75 	u32 high_bits, low_bits, type;
76 
77 	rprop = of_find_property(dp, "reg", NULL);
78 	if (!rprop)
79 		return;
80 
81 	regs = rprop->value;
82 	if (!of_node_is_root(dp->parent)) {
83 		sprintf(tmp_buf, "%s@%x,%x",
84 			name,
85 			(unsigned int) (regs->phys_addr >> 32UL),
86 			(unsigned int) (regs->phys_addr & 0xffffffffUL));
87 		return;
88 	}
89 
90 	type = regs->phys_addr >> 60UL;
91 	high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
92 	low_bits = (regs->phys_addr & 0xffffffffUL);
93 
94 	if (type == 0 || type == 8) {
95 		const char *prefix = (type == 0) ? "m" : "i";
96 
97 		if (low_bits)
98 			sprintf(tmp_buf, "%s@%s%x,%x",
99 				name, prefix,
100 				high_bits, low_bits);
101 		else
102 			sprintf(tmp_buf, "%s@%s%x",
103 				name,
104 				prefix,
105 				high_bits);
106 	} else if (type == 12) {
107 		sprintf(tmp_buf, "%s@%x",
108 			name, high_bits);
109 	}
110 }
111 
112 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
113 {
114 	const char *name = of_get_property(dp, "name", NULL);
115 	struct linux_prom64_registers *regs;
116 	struct property *prop;
117 
118 	prop = of_find_property(dp, "reg", NULL);
119 	if (!prop)
120 		return;
121 
122 	regs = prop->value;
123 	if (!of_node_is_root(dp->parent)) {
124 		sprintf(tmp_buf, "%s@%x,%x",
125 			name,
126 			(unsigned int) (regs->phys_addr >> 32UL),
127 			(unsigned int) (regs->phys_addr & 0xffffffffUL));
128 		return;
129 	}
130 
131 	prop = of_find_property(dp, "upa-portid", NULL);
132 	if (!prop)
133 		prop = of_find_property(dp, "portid", NULL);
134 	if (prop) {
135 		unsigned long mask = 0xffffffffUL;
136 
137 		if (tlb_type >= cheetah)
138 			mask = 0x7fffff;
139 
140 		sprintf(tmp_buf, "%s@%x,%x",
141 			name,
142 			*(u32 *)prop->value,
143 			(unsigned int) (regs->phys_addr & mask));
144 	}
145 }
146 
147 /* "name@slot,offset"  */
148 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
149 {
150 	const char *name = of_get_property(dp, "name", NULL);
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 		name,
161 		regs->which_io,
162 		regs->phys_addr);
163 }
164 
165 /* "name@devnum[,func]" */
166 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
167 {
168 	const char *name = of_get_property(dp, "name", NULL);
169 	struct linux_prom_pci_registers *regs;
170 	struct property *prop;
171 	unsigned int devfn;
172 
173 	prop = of_find_property(dp, "reg", NULL);
174 	if (!prop)
175 		return;
176 
177 	regs = prop->value;
178 	devfn = (regs->phys_hi >> 8) & 0xff;
179 	if (devfn & 0x07) {
180 		sprintf(tmp_buf, "%s@%x,%x",
181 			name,
182 			devfn >> 3,
183 			devfn & 0x07);
184 	} else {
185 		sprintf(tmp_buf, "%s@%x",
186 			name,
187 			devfn >> 3);
188 	}
189 }
190 
191 /* "name@UPA_PORTID,offset" */
192 static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
193 {
194 	const char *name = of_get_property(dp, "name", NULL);
195 	struct linux_prom64_registers *regs;
196 	struct property *prop;
197 
198 	prop = of_find_property(dp, "reg", NULL);
199 	if (!prop)
200 		return;
201 
202 	regs = prop->value;
203 
204 	prop = of_find_property(dp, "upa-portid", NULL);
205 	if (!prop)
206 		return;
207 
208 	sprintf(tmp_buf, "%s@%x,%x",
209 		name,
210 		*(u32 *) prop->value,
211 		(unsigned int) (regs->phys_addr & 0xffffffffUL));
212 }
213 
214 /* "name@reg" */
215 static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
216 {
217 	const char *name = of_get_property(dp, "name", NULL);
218 	struct property *prop;
219 	u32 *regs;
220 
221 	prop = of_find_property(dp, "reg", NULL);
222 	if (!prop)
223 		return;
224 
225 	regs = prop->value;
226 
227 	sprintf(tmp_buf, "%s@%x", name, *regs);
228 }
229 
230 /* "name@addrhi,addrlo" */
231 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
232 {
233 	const char *name = of_get_property(dp, "name", NULL);
234 	struct linux_prom64_registers *regs;
235 	struct property *prop;
236 
237 	prop = of_find_property(dp, "reg", NULL);
238 	if (!prop)
239 		return;
240 
241 	regs = prop->value;
242 
243 	sprintf(tmp_buf, "%s@%x,%x",
244 		name,
245 		(unsigned int) (regs->phys_addr >> 32UL),
246 		(unsigned int) (regs->phys_addr & 0xffffffffUL));
247 }
248 
249 /* "name@bus,addr" */
250 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
251 {
252 	const char *name = of_get_property(dp, "name", NULL);
253 	struct property *prop;
254 	u32 *regs;
255 
256 	prop = of_find_property(dp, "reg", NULL);
257 	if (!prop)
258 		return;
259 
260 	regs = prop->value;
261 
262 	/* This actually isn't right... should look at the #address-cells
263 	 * property of the i2c bus node etc. etc.
264 	 */
265 	sprintf(tmp_buf, "%s@%x,%x",
266 		name, regs[0], regs[1]);
267 }
268 
269 /* "name@reg0[,reg1]" */
270 static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
271 {
272 	const char *name = of_get_property(dp, "name", NULL);
273 	struct property *prop;
274 	u32 *regs;
275 
276 	prop = of_find_property(dp, "reg", NULL);
277 	if (!prop)
278 		return;
279 
280 	regs = prop->value;
281 
282 	if (prop->length == sizeof(u32) || regs[1] == 1) {
283 		sprintf(tmp_buf, "%s@%x",
284 			name, regs[0]);
285 	} else {
286 		sprintf(tmp_buf, "%s@%x,%x",
287 			name, regs[0], regs[1]);
288 	}
289 }
290 
291 /* "name@reg0reg1[,reg2reg3]" */
292 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
293 {
294 	const char *name = of_get_property(dp, "name", NULL);
295 	struct property *prop;
296 	u32 *regs;
297 
298 	prop = of_find_property(dp, "reg", NULL);
299 	if (!prop)
300 		return;
301 
302 	regs = prop->value;
303 
304 	if (regs[2] || regs[3]) {
305 		sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
306 			name, regs[0], regs[1], regs[2], regs[3]);
307 	} else {
308 		sprintf(tmp_buf, "%s@%08x%08x",
309 			name, regs[0], regs[1]);
310 	}
311 }
312 
313 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
314 {
315 	struct device_node *parent = dp->parent;
316 
317 	if (parent != NULL) {
318 		if (of_node_is_type(parent, "pci") ||
319 		    of_node_is_type(parent, "pciex")) {
320 			pci_path_component(dp, tmp_buf);
321 			return;
322 		}
323 		if (of_node_is_type(parent, "sbus")) {
324 			sbus_path_component(dp, tmp_buf);
325 			return;
326 		}
327 		if (of_node_is_type(parent, "upa")) {
328 			upa_path_component(dp, tmp_buf);
329 			return;
330 		}
331 		if (of_node_is_type(parent, "ebus")) {
332 			ebus_path_component(dp, tmp_buf);
333 			return;
334 		}
335 		if (of_node_name_eq(parent, "usb") ||
336 		    of_node_name_eq(parent, "hub")) {
337 			usb_path_component(dp, tmp_buf);
338 			return;
339 		}
340 		if (of_node_is_type(parent, "i2c")) {
341 			i2c_path_component(dp, tmp_buf);
342 			return;
343 		}
344 		if (of_node_is_type(parent, "firewire")) {
345 			ieee1394_path_component(dp, tmp_buf);
346 			return;
347 		}
348 		if (of_node_is_type(parent, "virtual-devices")) {
349 			vdev_path_component(dp, tmp_buf);
350 			return;
351 		}
352 		/* "isa" is handled with platform naming */
353 	}
354 
355 	/* Use platform naming convention.  */
356 	if (tlb_type == hypervisor) {
357 		sun4v_path_component(dp, tmp_buf);
358 		return;
359 	} else {
360 		sun4u_path_component(dp, tmp_buf);
361 	}
362 }
363 
364 char * __init build_path_component(struct device_node *dp)
365 {
366 	const char *name = of_get_property(dp, "name", NULL);
367 	char tmp_buf[64], *n;
368 
369 	tmp_buf[0] = '\0';
370 	__build_path_component(dp, tmp_buf);
371 	if (tmp_buf[0] == '\0')
372 		strcpy(tmp_buf, name);
373 
374 	n = prom_early_alloc(strlen(tmp_buf) + 1);
375 	strcpy(n, tmp_buf);
376 
377 	return n;
378 }
379 
380 static const char *get_mid_prop(void)
381 {
382 	return (tlb_type == spitfire ? "upa-portid" : "portid");
383 }
384 
385 bool arch_find_n_match_cpu_physical_id(struct device_node *cpun,
386 				       int cpu, unsigned int *thread)
387 {
388 	const char *mid_prop = get_mid_prop();
389 	int this_cpu_id;
390 
391 	/* On hypervisor based platforms we interrogate the 'reg'
392 	 * property.  On everything else we look for a 'upa-portid',
393 	 * 'portid', or 'cpuid' property.
394 	 */
395 
396 	if (tlb_type == hypervisor) {
397 		struct property *prop = of_find_property(cpun, "reg", NULL);
398 		u32 *regs;
399 
400 		if (!prop) {
401 			pr_warn("CPU node missing reg property\n");
402 			return false;
403 		}
404 		regs = prop->value;
405 		this_cpu_id = regs[0] & 0x0fffffff;
406 	} else {
407 		this_cpu_id = of_getintprop_default(cpun, mid_prop, -1);
408 
409 		if (this_cpu_id < 0) {
410 			mid_prop = "cpuid";
411 			this_cpu_id = of_getintprop_default(cpun, mid_prop, -1);
412 		}
413 		if (this_cpu_id < 0) {
414 			pr_warn("CPU node missing cpu ID property\n");
415 			return false;
416 		}
417 	}
418 	if (this_cpu_id == cpu) {
419 		if (thread) {
420 			int proc_id = cpu_data(cpu).proc_id;
421 
422 			/* On sparc64, the cpu thread information is obtained
423 			 * either from OBP or the machine description.  We've
424 			 * actually probed this information already long before
425 			 * this interface gets called so instead of interrogating
426 			 * both the OF node and the MDESC again, just use what
427 			 * we discovered already.
428 			 */
429 			if (proc_id < 0)
430 				proc_id = 0;
431 			*thread = proc_id;
432 		}
433 		return true;
434 	}
435 	return false;
436 }
437 
438 static void *of_iterate_over_cpus(void *(*func)(struct device_node *, int, int), int arg)
439 {
440 	struct device_node *dp;
441 	const char *mid_prop;
442 
443 	mid_prop = get_mid_prop();
444 	for_each_node_by_type(dp, "cpu") {
445 		int cpuid = of_getintprop_default(dp, mid_prop, -1);
446 		const char *this_mid_prop = mid_prop;
447 		void *ret;
448 
449 		if (cpuid < 0) {
450 			this_mid_prop = "cpuid";
451 			cpuid = of_getintprop_default(dp, this_mid_prop, -1);
452 		}
453 		if (cpuid < 0) {
454 			prom_printf("OF: Serious problem, cpu lacks "
455 				    "%s property", this_mid_prop);
456 			prom_halt();
457 		}
458 #ifdef CONFIG_SMP
459 		if (cpuid >= NR_CPUS) {
460 			printk(KERN_WARNING "Ignoring CPU %d which is "
461 			       ">= NR_CPUS (%d)\n",
462 			       cpuid, NR_CPUS);
463 			continue;
464 		}
465 #endif
466 		ret = func(dp, cpuid, arg);
467 		if (ret)
468 			return ret;
469 	}
470 	return NULL;
471 }
472 
473 static void *check_cpu_node(struct device_node *dp, int cpuid, int id)
474 {
475 	if (id == cpuid)
476 		return dp;
477 	return NULL;
478 }
479 
480 struct device_node *of_find_node_by_cpuid(int cpuid)
481 {
482 	return of_iterate_over_cpus(check_cpu_node, cpuid);
483 }
484 
485 static void *record_one_cpu(struct device_node *dp, int cpuid, int arg)
486 {
487 	ncpus_probed++;
488 #ifdef CONFIG_SMP
489 	set_cpu_present(cpuid, true);
490 	set_cpu_possible(cpuid, true);
491 #endif
492 	return NULL;
493 }
494 
495 void __init of_populate_present_mask(void)
496 {
497 	if (tlb_type == hypervisor)
498 		return;
499 
500 	ncpus_probed = 0;
501 	of_iterate_over_cpus(record_one_cpu, 0);
502 }
503 
504 static void *fill_in_one_cpu(struct device_node *dp, int cpuid, int arg)
505 {
506 	struct device_node *portid_parent = NULL;
507 	int portid = -1;
508 
509 	if (of_find_property(dp, "cpuid", NULL)) {
510 		int limit = 2;
511 
512 		portid_parent = dp;
513 		while (limit--) {
514 			portid_parent = portid_parent->parent;
515 			if (!portid_parent)
516 				break;
517 			portid = of_getintprop_default(portid_parent,
518 						       "portid", -1);
519 			if (portid >= 0)
520 				break;
521 		}
522 	}
523 
524 #ifndef CONFIG_SMP
525 	/* On uniprocessor we only want the values for the
526 	 * real physical cpu the kernel booted onto, however
527 	 * cpu_data() only has one entry at index 0.
528 	 */
529 	if (cpuid != real_hard_smp_processor_id())
530 		return NULL;
531 	cpuid = 0;
532 #endif
533 
534 	cpu_data(cpuid).clock_tick =
535 		of_getintprop_default(dp, "clock-frequency", 0);
536 
537 	if (portid_parent) {
538 		cpu_data(cpuid).dcache_size =
539 			of_getintprop_default(dp, "l1-dcache-size",
540 					      16 * 1024);
541 		cpu_data(cpuid).dcache_line_size =
542 			of_getintprop_default(dp, "l1-dcache-line-size",
543 					      32);
544 		cpu_data(cpuid).icache_size =
545 			of_getintprop_default(dp, "l1-icache-size",
546 					      8 * 1024);
547 		cpu_data(cpuid).icache_line_size =
548 			of_getintprop_default(dp, "l1-icache-line-size",
549 					      32);
550 		cpu_data(cpuid).ecache_size =
551 			of_getintprop_default(dp, "l2-cache-size", 0);
552 		cpu_data(cpuid).ecache_line_size =
553 			of_getintprop_default(dp, "l2-cache-line-size", 0);
554 		if (!cpu_data(cpuid).ecache_size ||
555 		    !cpu_data(cpuid).ecache_line_size) {
556 			cpu_data(cpuid).ecache_size =
557 				of_getintprop_default(portid_parent,
558 						      "l2-cache-size",
559 						      (4 * 1024 * 1024));
560 			cpu_data(cpuid).ecache_line_size =
561 				of_getintprop_default(portid_parent,
562 						      "l2-cache-line-size", 64);
563 		}
564 
565 		cpu_data(cpuid).core_id = portid + 1;
566 		cpu_data(cpuid).proc_id = portid;
567 	} else {
568 		cpu_data(cpuid).dcache_size =
569 			of_getintprop_default(dp, "dcache-size", 16 * 1024);
570 		cpu_data(cpuid).dcache_line_size =
571 			of_getintprop_default(dp, "dcache-line-size", 32);
572 
573 		cpu_data(cpuid).icache_size =
574 			of_getintprop_default(dp, "icache-size", 16 * 1024);
575 		cpu_data(cpuid).icache_line_size =
576 			of_getintprop_default(dp, "icache-line-size", 32);
577 
578 		cpu_data(cpuid).ecache_size =
579 			of_getintprop_default(dp, "ecache-size",
580 					      (4 * 1024 * 1024));
581 		cpu_data(cpuid).ecache_line_size =
582 			of_getintprop_default(dp, "ecache-line-size", 64);
583 
584 		cpu_data(cpuid).core_id = 0;
585 		cpu_data(cpuid).proc_id = -1;
586 	}
587 
588 	return NULL;
589 }
590 
591 void __init of_fill_in_cpu_data(void)
592 {
593 	if (tlb_type == hypervisor)
594 		return;
595 
596 	of_iterate_over_cpus(fill_in_one_cpu, 0);
597 
598 	smp_fill_in_sib_core_maps();
599 }
600 
601 void __init of_console_init(void)
602 {
603 	char *msg = "OF stdout device is: %s\n";
604 	struct device_node *dp;
605 	phandle node;
606 
607 	of_console_path = prom_early_alloc(256);
608 	if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) {
609 		prom_printf("Cannot obtain path of stdout.\n");
610 		prom_halt();
611 	}
612 	of_console_options = strrchr(of_console_path, ':');
613 	if (of_console_options) {
614 		of_console_options++;
615 		if (*of_console_options == '\0')
616 			of_console_options = NULL;
617 	}
618 
619 	node = prom_inst2pkg(prom_stdout);
620 	if (!node) {
621 		prom_printf("Cannot resolve stdout node from "
622 			    "instance %08x.\n", prom_stdout);
623 		prom_halt();
624 	}
625 
626 	dp = of_find_node_by_phandle(node);
627 
628 	if (!of_node_is_type(dp, "display") && !of_node_is_type(dp, "serial")) {
629 		prom_printf("Console device_type is neither display "
630 			    "nor serial.\n");
631 		prom_halt();
632 	}
633 
634 	of_console_device = dp;
635 
636 	printk(msg, of_console_path);
637 }
638