1 /*
2  * Common boot and setup code for both 32-bit and 64-bit.
3  * Extracted from arch/powerpc/kernel/setup_64.c.
4  *
5  * Copyright (C) 2001 PPC64 Team, IBM Corp
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version
10  *      2 of the License, or (at your option) any later version.
11  */
12 
13 #undef DEBUG
14 
15 #include <linux/module.h>
16 #include <linux/string.h>
17 #include <linux/sched.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/reboot.h>
21 #include <linux/delay.h>
22 #include <linux/initrd.h>
23 #include <linux/platform_device.h>
24 #include <linux/seq_file.h>
25 #include <linux/ioport.h>
26 #include <linux/console.h>
27 #include <linux/utsname.h>
28 #include <linux/screen_info.h>
29 #include <linux/root_dev.h>
30 #include <linux/notifier.h>
31 #include <linux/cpu.h>
32 #include <linux/unistd.h>
33 #include <linux/serial.h>
34 #include <linux/serial_8250.h>
35 #include <linux/debugfs.h>
36 #include <linux/percpu.h>
37 #include <asm/io.h>
38 #include <asm/prom.h>
39 #include <asm/processor.h>
40 #include <asm/vdso_datapage.h>
41 #include <asm/pgtable.h>
42 #include <asm/smp.h>
43 #include <asm/elf.h>
44 #include <asm/machdep.h>
45 #include <asm/time.h>
46 #include <asm/cputable.h>
47 #include <asm/sections.h>
48 #include <asm/firmware.h>
49 #include <asm/btext.h>
50 #include <asm/nvram.h>
51 #include <asm/setup.h>
52 #include <asm/system.h>
53 #include <asm/rtas.h>
54 #include <asm/iommu.h>
55 #include <asm/serial.h>
56 #include <asm/cache.h>
57 #include <asm/page.h>
58 #include <asm/mmu.h>
59 #include <asm/lmb.h>
60 #include <asm/xmon.h>
61 #include <asm/cputhreads.h>
62 
63 #include "setup.h"
64 
65 #ifdef DEBUG
66 #include <asm/udbg.h>
67 #define DBG(fmt...) udbg_printf(fmt)
68 #else
69 #define DBG(fmt...)
70 #endif
71 
72 /* The main machine-dep calls structure
73  */
74 struct machdep_calls ppc_md;
75 EXPORT_SYMBOL(ppc_md);
76 struct machdep_calls *machine_id;
77 EXPORT_SYMBOL(machine_id);
78 
79 unsigned long klimit = (unsigned long) _end;
80 
81 char cmd_line[COMMAND_LINE_SIZE];
82 
83 /*
84  * This still seems to be needed... -- paulus
85  */
86 struct screen_info screen_info = {
87 	.orig_x = 0,
88 	.orig_y = 25,
89 	.orig_video_cols = 80,
90 	.orig_video_lines = 25,
91 	.orig_video_isVGA = 1,
92 	.orig_video_points = 16
93 };
94 
95 #ifdef __DO_IRQ_CANON
96 /* XXX should go elsewhere eventually */
97 int ppc_do_canonicalize_irqs;
98 EXPORT_SYMBOL(ppc_do_canonicalize_irqs);
99 #endif
100 
101 /* also used by kexec */
102 void machine_shutdown(void)
103 {
104 	if (ppc_md.machine_shutdown)
105 		ppc_md.machine_shutdown();
106 }
107 
108 void machine_restart(char *cmd)
109 {
110 	machine_shutdown();
111 	if (ppc_md.restart)
112 		ppc_md.restart(cmd);
113 #ifdef CONFIG_SMP
114 	smp_send_stop();
115 #endif
116 	printk(KERN_EMERG "System Halted, OK to turn off power\n");
117 	local_irq_disable();
118 	while (1) ;
119 }
120 
121 void machine_power_off(void)
122 {
123 	machine_shutdown();
124 	if (ppc_md.power_off)
125 		ppc_md.power_off();
126 #ifdef CONFIG_SMP
127 	smp_send_stop();
128 #endif
129 	printk(KERN_EMERG "System Halted, OK to turn off power\n");
130 	local_irq_disable();
131 	while (1) ;
132 }
133 /* Used by the G5 thermal driver */
134 EXPORT_SYMBOL_GPL(machine_power_off);
135 
136 void (*pm_power_off)(void) = machine_power_off;
137 EXPORT_SYMBOL_GPL(pm_power_off);
138 
139 void machine_halt(void)
140 {
141 	machine_shutdown();
142 	if (ppc_md.halt)
143 		ppc_md.halt();
144 #ifdef CONFIG_SMP
145 	smp_send_stop();
146 #endif
147 	printk(KERN_EMERG "System Halted, OK to turn off power\n");
148 	local_irq_disable();
149 	while (1) ;
150 }
151 
152 
153 #ifdef CONFIG_TAU
154 extern u32 cpu_temp(unsigned long cpu);
155 extern u32 cpu_temp_both(unsigned long cpu);
156 #endif /* CONFIG_TAU */
157 
158 #ifdef CONFIG_SMP
159 DEFINE_PER_CPU(unsigned int, pvr);
160 #endif
161 
162 static int show_cpuinfo(struct seq_file *m, void *v)
163 {
164 	unsigned long cpu_id = (unsigned long)v - 1;
165 	unsigned int pvr;
166 	unsigned short maj;
167 	unsigned short min;
168 
169 	if (cpu_id == NR_CPUS) {
170 #if defined(CONFIG_SMP) && defined(CONFIG_PPC32)
171 		unsigned long bogosum = 0;
172 		int i;
173 		for_each_online_cpu(i)
174 			bogosum += loops_per_jiffy;
175 		seq_printf(m, "total bogomips\t: %lu.%02lu\n",
176 			   bogosum/(500000/HZ), bogosum/(5000/HZ) % 100);
177 #endif /* CONFIG_SMP && CONFIG_PPC32 */
178 		seq_printf(m, "timebase\t: %lu\n", ppc_tb_freq);
179 		if (ppc_md.name)
180 			seq_printf(m, "platform\t: %s\n", ppc_md.name);
181 		if (ppc_md.show_cpuinfo != NULL)
182 			ppc_md.show_cpuinfo(m);
183 
184 		return 0;
185 	}
186 
187 	/* We only show online cpus: disable preempt (overzealous, I
188 	 * knew) to prevent cpu going down. */
189 	preempt_disable();
190 	if (!cpu_online(cpu_id)) {
191 		preempt_enable();
192 		return 0;
193 	}
194 
195 #ifdef CONFIG_SMP
196 	pvr = per_cpu(pvr, cpu_id);
197 #else
198 	pvr = mfspr(SPRN_PVR);
199 #endif
200 	maj = (pvr >> 8) & 0xFF;
201 	min = pvr & 0xFF;
202 
203 	seq_printf(m, "processor\t: %lu\n", cpu_id);
204 	seq_printf(m, "cpu\t\t: ");
205 
206 	if (cur_cpu_spec->pvr_mask)
207 		seq_printf(m, "%s", cur_cpu_spec->cpu_name);
208 	else
209 		seq_printf(m, "unknown (%08x)", pvr);
210 
211 #ifdef CONFIG_ALTIVEC
212 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
213 		seq_printf(m, ", altivec supported");
214 #endif /* CONFIG_ALTIVEC */
215 
216 	seq_printf(m, "\n");
217 
218 #ifdef CONFIG_TAU
219 	if (cur_cpu_spec->cpu_features & CPU_FTR_TAU) {
220 #ifdef CONFIG_TAU_AVERAGE
221 		/* more straightforward, but potentially misleading */
222 		seq_printf(m,  "temperature \t: %u C (uncalibrated)\n",
223 			   cpu_temp(cpu_id));
224 #else
225 		/* show the actual temp sensor range */
226 		u32 temp;
227 		temp = cpu_temp_both(cpu_id);
228 		seq_printf(m, "temperature \t: %u-%u C (uncalibrated)\n",
229 			   temp & 0xff, temp >> 16);
230 #endif
231 	}
232 #endif /* CONFIG_TAU */
233 
234 	/*
235 	 * Assume here that all clock rates are the same in a
236 	 * smp system.  -- Cort
237 	 */
238 	if (ppc_proc_freq)
239 		seq_printf(m, "clock\t\t: %lu.%06luMHz\n",
240 			   ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
241 
242 	if (ppc_md.show_percpuinfo != NULL)
243 		ppc_md.show_percpuinfo(m, cpu_id);
244 
245 	/* If we are a Freescale core do a simple check so
246 	 * we dont have to keep adding cases in the future */
247 	if (PVR_VER(pvr) & 0x8000) {
248 		maj = PVR_MAJ(pvr);
249 		min = PVR_MIN(pvr);
250 	} else {
251 		switch (PVR_VER(pvr)) {
252 			case 0x0020:	/* 403 family */
253 				maj = PVR_MAJ(pvr) + 1;
254 				min = PVR_MIN(pvr);
255 				break;
256 			case 0x1008:	/* 740P/750P ?? */
257 				maj = ((pvr >> 8) & 0xFF) - 1;
258 				min = pvr & 0xFF;
259 				break;
260 			default:
261 				maj = (pvr >> 8) & 0xFF;
262 				min = pvr & 0xFF;
263 				break;
264 		}
265 	}
266 
267 	seq_printf(m, "revision\t: %hd.%hd (pvr %04x %04x)\n",
268 		   maj, min, PVR_VER(pvr), PVR_REV(pvr));
269 
270 #ifdef CONFIG_PPC32
271 	seq_printf(m, "bogomips\t: %lu.%02lu\n",
272 		   loops_per_jiffy / (500000/HZ),
273 		   (loops_per_jiffy / (5000/HZ)) % 100);
274 #endif
275 
276 #ifdef CONFIG_SMP
277 	seq_printf(m, "\n");
278 #endif
279 
280 	preempt_enable();
281 	return 0;
282 }
283 
284 static void *c_start(struct seq_file *m, loff_t *pos)
285 {
286 	unsigned long i = *pos;
287 
288 	return i <= NR_CPUS ? (void *)(i + 1) : NULL;
289 }
290 
291 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
292 {
293 	++*pos;
294 	return c_start(m, pos);
295 }
296 
297 static void c_stop(struct seq_file *m, void *v)
298 {
299 }
300 
301 struct seq_operations cpuinfo_op = {
302 	.start =c_start,
303 	.next =	c_next,
304 	.stop =	c_stop,
305 	.show =	show_cpuinfo,
306 };
307 
308 void __init check_for_initrd(void)
309 {
310 #ifdef CONFIG_BLK_DEV_INITRD
311 	DBG(" -> check_for_initrd()  initrd_start=0x%lx  initrd_end=0x%lx\n",
312 	    initrd_start, initrd_end);
313 
314 	/* If we were passed an initrd, set the ROOT_DEV properly if the values
315 	 * look sensible. If not, clear initrd reference.
316 	 */
317 	if (is_kernel_addr(initrd_start) && is_kernel_addr(initrd_end) &&
318 	    initrd_end > initrd_start)
319 		ROOT_DEV = Root_RAM0;
320 	else
321 		initrd_start = initrd_end = 0;
322 
323 	if (initrd_start)
324 		printk("Found initrd at 0x%lx:0x%lx\n", initrd_start, initrd_end);
325 
326 	DBG(" <- check_for_initrd()\n");
327 #endif /* CONFIG_BLK_DEV_INITRD */
328 }
329 
330 #ifdef CONFIG_SMP
331 
332 int threads_per_core, threads_shift;
333 cpumask_t threads_core_mask;
334 
335 static void __init cpu_init_thread_core_maps(int tpc)
336 {
337 	int i;
338 
339 	threads_per_core = tpc;
340 	threads_core_mask = CPU_MASK_NONE;
341 
342 	/* This implementation only supports power of 2 number of threads
343 	 * for simplicity and performance
344 	 */
345 	threads_shift = ilog2(tpc);
346 	BUG_ON(tpc != (1 << threads_shift));
347 
348 	for (i = 0; i < tpc; i++)
349 		cpu_set(i, threads_core_mask);
350 
351 	printk(KERN_INFO "CPU maps initialized for %d thread%s per core\n",
352 	       tpc, tpc > 1 ? "s" : "");
353 	printk(KERN_DEBUG " (thread shift is %d)\n", threads_shift);
354 }
355 
356 
357 /**
358  * setup_cpu_maps - initialize the following cpu maps:
359  *                  cpu_possible_map
360  *                  cpu_present_map
361  *                  cpu_sibling_map
362  *
363  * Having the possible map set up early allows us to restrict allocations
364  * of things like irqstacks to num_possible_cpus() rather than NR_CPUS.
365  *
366  * We do not initialize the online map here; cpus set their own bits in
367  * cpu_online_map as they come up.
368  *
369  * This function is valid only for Open Firmware systems.  finish_device_tree
370  * must be called before using this.
371  *
372  * While we're here, we may as well set the "physical" cpu ids in the paca.
373  *
374  * NOTE: This must match the parsing done in early_init_dt_scan_cpus.
375  */
376 void __init smp_setup_cpu_maps(void)
377 {
378 	struct device_node *dn = NULL;
379 	int cpu = 0;
380 	int nthreads = 1;
381 
382 	DBG("smp_setup_cpu_maps()\n");
383 
384 	while ((dn = of_find_node_by_type(dn, "cpu")) && cpu < NR_CPUS) {
385 		const int *intserv;
386 		int j, len;
387 
388 		DBG("  * %s...\n", dn->full_name);
389 
390 		intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s",
391 				&len);
392 		if (intserv) {
393 			nthreads = len / sizeof(int);
394 			DBG("    ibm,ppc-interrupt-server#s -> %d threads\n",
395 			    nthreads);
396 		} else {
397 			DBG("    no ibm,ppc-interrupt-server#s -> 1 thread\n");
398 			intserv = of_get_property(dn, "reg", NULL);
399 			if (!intserv)
400 				intserv = &cpu;	/* assume logical == phys */
401 		}
402 
403 		for (j = 0; j < nthreads && cpu < NR_CPUS; j++) {
404 			DBG("    thread %d -> cpu %d (hard id %d)\n",
405 			    j, cpu, intserv[j]);
406 			cpu_set(cpu, cpu_present_map);
407 			set_hard_smp_processor_id(cpu, intserv[j]);
408 			cpu_set(cpu, cpu_possible_map);
409 			cpu++;
410 		}
411 	}
412 
413 	/* If no SMT supported, nthreads is forced to 1 */
414 	if (!cpu_has_feature(CPU_FTR_SMT)) {
415 		DBG("  SMT disabled ! nthreads forced to 1\n");
416 		nthreads = 1;
417 	}
418 
419 #ifdef CONFIG_PPC64
420 	/*
421 	 * On pSeries LPAR, we need to know how many cpus
422 	 * could possibly be added to this partition.
423 	 */
424 	if (machine_is(pseries) && firmware_has_feature(FW_FEATURE_LPAR) &&
425 	    (dn = of_find_node_by_path("/rtas"))) {
426 		int num_addr_cell, num_size_cell, maxcpus;
427 		const unsigned int *ireg;
428 
429 		num_addr_cell = of_n_addr_cells(dn);
430 		num_size_cell = of_n_size_cells(dn);
431 
432 		ireg = of_get_property(dn, "ibm,lrdr-capacity", NULL);
433 
434 		if (!ireg)
435 			goto out;
436 
437 		maxcpus = ireg[num_addr_cell + num_size_cell];
438 
439 		/* Double maxcpus for processors which have SMT capability */
440 		if (cpu_has_feature(CPU_FTR_SMT))
441 			maxcpus *= nthreads;
442 
443 		if (maxcpus > NR_CPUS) {
444 			printk(KERN_WARNING
445 			       "Partition configured for %d cpus, "
446 			       "operating system maximum is %d.\n",
447 			       maxcpus, NR_CPUS);
448 			maxcpus = NR_CPUS;
449 		} else
450 			printk(KERN_INFO "Partition configured for %d cpus.\n",
451 			       maxcpus);
452 
453 		for (cpu = 0; cpu < maxcpus; cpu++)
454 			cpu_set(cpu, cpu_possible_map);
455 	out:
456 		of_node_put(dn);
457 	}
458 	vdso_data->processorCount = num_present_cpus();
459 #endif /* CONFIG_PPC64 */
460 
461         /* Initialize CPU <=> thread mapping/
462 	 *
463 	 * WARNING: We assume that the number of threads is the same for
464 	 * every CPU in the system. If that is not the case, then some code
465 	 * here will have to be reworked
466 	 */
467 	cpu_init_thread_core_maps(nthreads);
468 }
469 
470 /*
471  * Being that cpu_sibling_map is now a per_cpu array, then it cannot
472  * be initialized until the per_cpu areas have been created.  This
473  * function is now called from setup_per_cpu_areas().
474  */
475 void __init smp_setup_cpu_sibling_map(void)
476 {
477 #ifdef CONFIG_PPC64
478 	int i, cpu, base;
479 
480 	for_each_possible_cpu(cpu) {
481 		DBG("Sibling map for CPU %d:", cpu);
482 		base = cpu_first_thread_in_core(cpu);
483 		for (i = 0; i < threads_per_core; i++) {
484 			cpu_set(base + i, per_cpu(cpu_sibling_map, cpu));
485 			DBG(" %d", base + i);
486 		}
487 		DBG("\n");
488 	}
489 
490 #endif /* CONFIG_PPC64 */
491 }
492 #endif /* CONFIG_SMP */
493 
494 static __init int add_pcspkr(void)
495 {
496 	struct device_node *np;
497 	struct platform_device *pd;
498 	int ret;
499 
500 	np = of_find_compatible_node(NULL, NULL, "pnpPNP,100");
501 	of_node_put(np);
502 	if (!np)
503 		return -ENODEV;
504 
505 	pd = platform_device_alloc("pcspkr", -1);
506 	if (!pd)
507 		return -ENOMEM;
508 
509 	ret = platform_device_add(pd);
510 	if (ret)
511 		platform_device_put(pd);
512 
513 	return ret;
514 }
515 device_initcall(add_pcspkr);
516 
517 void probe_machine(void)
518 {
519 	extern struct machdep_calls __machine_desc_start;
520 	extern struct machdep_calls __machine_desc_end;
521 
522 	/*
523 	 * Iterate all ppc_md structures until we find the proper
524 	 * one for the current machine type
525 	 */
526 	DBG("Probing machine type ...\n");
527 
528 	for (machine_id = &__machine_desc_start;
529 	     machine_id < &__machine_desc_end;
530 	     machine_id++) {
531 		DBG("  %s ...", machine_id->name);
532 		memcpy(&ppc_md, machine_id, sizeof(struct machdep_calls));
533 		if (ppc_md.probe()) {
534 			DBG(" match !\n");
535 			break;
536 		}
537 		DBG("\n");
538 	}
539 	/* What can we do if we didn't find ? */
540 	if (machine_id >= &__machine_desc_end) {
541 		DBG("No suitable machine found !\n");
542 		for (;;);
543 	}
544 
545 	printk(KERN_INFO "Using %s machine description\n", ppc_md.name);
546 }
547 
548 /* Match a class of boards, not a specific device configuration. */
549 int check_legacy_ioport(unsigned long base_port)
550 {
551 	struct device_node *parent, *np = NULL;
552 	int ret = -ENODEV;
553 
554 	switch(base_port) {
555 	case I8042_DATA_REG:
556 		if (!(np = of_find_compatible_node(NULL, NULL, "pnpPNP,303")))
557 			np = of_find_compatible_node(NULL, NULL, "pnpPNP,f03");
558 		if (np) {
559 			parent = of_get_parent(np);
560 			of_node_put(np);
561 			np = parent;
562 			break;
563 		}
564 		np = of_find_node_by_type(NULL, "8042");
565 		/* Pegasos has no device_type on its 8042 node, look for the
566 		 * name instead */
567 		if (!np)
568 			np = of_find_node_by_name(NULL, "8042");
569 		break;
570 	case FDC_BASE: /* FDC1 */
571 		np = of_find_node_by_type(NULL, "fdc");
572 		break;
573 #ifdef CONFIG_PPC_PREP
574 	case _PIDXR:
575 	case _PNPWRP:
576 	case PNPBIOS_BASE:
577 		/* implement me */
578 #endif
579 	default:
580 		/* ipmi is supposed to fail here */
581 		break;
582 	}
583 	if (!np)
584 		return ret;
585 	parent = of_get_parent(np);
586 	if (parent) {
587 		if (strcmp(parent->type, "isa") == 0)
588 			ret = 0;
589 		of_node_put(parent);
590 	}
591 	of_node_put(np);
592 	return ret;
593 }
594 EXPORT_SYMBOL(check_legacy_ioport);
595 
596 static int ppc_panic_event(struct notifier_block *this,
597                              unsigned long event, void *ptr)
598 {
599 	ppc_md.panic(ptr);  /* May not return */
600 	return NOTIFY_DONE;
601 }
602 
603 static struct notifier_block ppc_panic_block = {
604 	.notifier_call = ppc_panic_event,
605 	.priority = INT_MIN /* may not return; must be done last */
606 };
607 
608 void __init setup_panic(void)
609 {
610 	atomic_notifier_chain_register(&panic_notifier_list, &ppc_panic_block);
611 }
612 
613 #ifdef CONFIG_CHECK_CACHE_COHERENCY
614 /*
615  * For platforms that have configurable cache-coherency.  This function
616  * checks that the cache coherency setting of the kernel matches the setting
617  * left by the firmware, as indicated in the device tree.  Since a mismatch
618  * will eventually result in DMA failures, we print * and error and call
619  * BUG() in that case.
620  */
621 
622 #ifdef CONFIG_NOT_COHERENT_CACHE
623 #define KERNEL_COHERENCY	0
624 #else
625 #define KERNEL_COHERENCY	1
626 #endif
627 
628 static int __init check_cache_coherency(void)
629 {
630 	struct device_node *np;
631 	const void *prop;
632 	int devtree_coherency;
633 
634 	np = of_find_node_by_path("/");
635 	prop = of_get_property(np, "coherency-off", NULL);
636 	of_node_put(np);
637 
638 	devtree_coherency = prop ? 0 : 1;
639 
640 	if (devtree_coherency != KERNEL_COHERENCY) {
641 		printk(KERN_ERR
642 			"kernel coherency:%s != device tree_coherency:%s\n",
643 			KERNEL_COHERENCY ? "on" : "off",
644 			devtree_coherency ? "on" : "off");
645 		BUG();
646 	}
647 
648 	return 0;
649 }
650 
651 late_initcall(check_cache_coherency);
652 #endif /* CONFIG_CHECK_CACHE_COHERENCY */
653 
654 #ifdef CONFIG_DEBUG_FS
655 struct dentry *powerpc_debugfs_root;
656 
657 static int powerpc_debugfs_init(void)
658 {
659 	powerpc_debugfs_root = debugfs_create_dir("powerpc", NULL);
660 
661 	return powerpc_debugfs_root == NULL;
662 }
663 arch_initcall(powerpc_debugfs_init);
664 #endif
665