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