setup.c 6.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * PowerNV setup code.
 *
 * Copyright 2011 IBM Corp.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#undef DEBUG

#include <linux/cpu.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/tty.h>
#include <linux/reboot.h>
#include <linux/init.h>
#include <linux/console.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <linux/seq_file.h>
#include <linux/of.h>
R
Rob Herring 已提交
26
#include <linux/of_fdt.h>
27 28
#include <linux/interrupt.h>
#include <linux/bug.h>
29
#include <linux/pci.h>
30
#include <linux/cpufreq.h>
31 32 33 34

#include <asm/machdep.h>
#include <asm/firmware.h>
#include <asm/xics.h>
35
#include <asm/opal.h>
36
#include <asm/kexec.h>
37
#include <asm/smp.h>
38 39 40 41 42

#include "powernv.h"

static void __init pnv_setup_arch(void)
{
43 44
	set_arch_panic_timeout(10, ARCH_PANIC_TIMEOUT);

45 46 47
	/* Initialize SMP */
	pnv_smp_init();

48 49
	/* Setup PCI */
	pnv_pci_init();
50

51 52 53
	/* Setup RTC and NVRAM callbacks */
	if (firmware_has_feature(FW_FEATURE_OPAL))
		opal_nvram_init();
54 55 56 57 58 59 60 61 62

	/* Enable NAP mode */
	powersave_nap = 1;

	/* XXX PMCS */
}

static void __init pnv_init_early(void)
{
63 64 65 66 67 68
	/*
	 * Initialize the LPC bus now so that legacy serial
	 * ports can be found on it
	 */
	opal_lpc_init();

69 70 71 72 73 74
#ifdef CONFIG_HVC_OPAL
	if (firmware_has_feature(FW_FEATURE_OPAL))
		hvc_opal_init_early();
	else
#endif
		add_preferred_console("hvc", 0, NULL);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
}

static void __init pnv_init_IRQ(void)
{
	xics_init();

	WARN_ON(!ppc_md.get_irq);
}

static void pnv_show_cpuinfo(struct seq_file *m)
{
	struct device_node *root;
	const char *model = "";

	root = of_find_node_by_path("/");
	if (root)
		model = of_get_property(root, "model", NULL);
	seq_printf(m, "machine\t\t: PowerNV %s\n", model);
93 94
	if (firmware_has_feature(FW_FEATURE_OPAL))
		seq_printf(m, "firmware\t: OPAL\n");
95 96
	else
		seq_printf(m, "firmware\t: BML\n");
97 98 99
	of_node_put(root);
}

100 101 102 103 104 105
static void pnv_prepare_going_down(void)
{
	/*
	 * Disable all notifiers from OPAL, we can't
	 * service interrupts anymore anyway
	 */
A
Alistair Popple 已提交
106
	opal_event_shutdown();
107 108 109 110 111 112 113 114 115 116 117 118 119 120

	/* Soft disable interrupts */
	local_irq_disable();

	/*
	 * Return secondary CPUs to firwmare if a flash update
	 * is pending otherwise we will get all sort of error
	 * messages about CPU being stuck etc.. This will also
	 * have the side effect of hard disabling interrupts so
	 * past this point, the kernel is effectively dead.
	 */
	opal_flash_term_callback();
}

121
static void  __noreturn pnv_restart(char *cmd)
122
{
123 124
	long rc = OPAL_BUSY;

125
	pnv_prepare_going_down();
126

127 128 129 130 131 132 133 134 135
	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
		rc = opal_cec_reboot();
		if (rc == OPAL_BUSY_EVENT)
			opal_poll_events(NULL);
		else
			mdelay(10);
	}
	for (;;)
		opal_poll_events(NULL);
136 137
}

138
static void __noreturn pnv_power_off(void)
139
{
140 141
	long rc = OPAL_BUSY;

142
	pnv_prepare_going_down();
143

144 145 146 147 148 149 150 151 152
	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
		rc = opal_cec_power_down(0);
		if (rc == OPAL_BUSY_EVENT)
			opal_poll_events(NULL);
		else
			mdelay(10);
	}
	for (;;)
		opal_poll_events(NULL);
153 154
}

155
static void __noreturn pnv_halt(void)
156
{
157
	pnv_power_off();
158 159
}

160
static void pnv_progress(char *s, unsigned short hex)
161 162 163
{
}

164 165 166 167 168
static void pnv_shutdown(void)
{
	/* Let the PCI code clear up IODA tables */
	pnv_pci_shutdown();

169 170 171 172
	/*
	 * Stop OPAL activity: Unregister all OPAL interrupts so they
	 * don't fire up while we kexec and make sure all potentially
	 * DMA'ing ops are complete (such as dump retrieval).
173 174 175 176
	 */
	opal_shutdown();
}

177
#ifdef CONFIG_KEXEC
178 179 180 181 182 183 184 185
static void pnv_kexec_wait_secondaries_down(void)
{
	int my_cpu, i, notified = -1;

	my_cpu = get_cpu();

	for_each_online_cpu(i) {
		uint8_t status;
186
		int64_t rc, timeout = 1000;
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

		if (i == my_cpu)
			continue;

		for (;;) {
			rc = opal_query_cpu_status(get_hard_smp_processor_id(i),
						   &status);
			if (rc != OPAL_SUCCESS || status != OPAL_THREAD_STARTED)
				break;
			barrier();
			if (i != notified) {
				printk(KERN_INFO "kexec: waiting for cpu %d "
				       "(physical %d) to enter OPAL\n",
				       i, paca[i].hw_cpu_id);
				notified = i;
			}
203 204 205 206 207 208 209 210 211 212 213 214

			/*
			 * On crash secondaries might be unreachable or hung,
			 * so timeout if we've waited too long
			 * */
			mdelay(1);
			if (timeout-- == 0) {
				printk(KERN_ERR "kexec: timed out waiting for "
				       "cpu %d (physical %d) to enter OPAL\n",
				       i, paca[i].hw_cpu_id);
				break;
			}
215 216 217 218
		}
	}
}

219
static void pnv_kexec_cpu_down(int crash_shutdown, int secondary)
220
{
221
	xics_kexec_teardown_cpu(secondary);
222

223
	/* On OPAL, we return all CPUs to firmware */
224

225
	if (!firmware_has_feature(FW_FEATURE_OPAL))
226 227 228 229
		return;

	if (secondary) {
		/* Return secondary CPUs to firmware on OPAL v3 */
230 231 232 233 234 235
		mb();
		get_paca()->kexec_state = KEXEC_STATE_REAL_MODE;
		mb();

		/* Return the CPU to OPAL */
		opal_return_cpu();
236 237 238
	} else {
		/* Primary waits for the secondaries to have reached OPAL */
		pnv_kexec_wait_secondaries_down();
239 240 241 242 243 244 245

		/*
		 * We might be running as little-endian - now that interrupts
		 * are disabled, reset the HILE bit to big-endian so we don't
		 * take interrupts in the wrong endian later
		 */
		opal_reinit_cpus(OPAL_REINIT_CPUS_HILE_BE);
246
	}
247
}
248
#endif /* CONFIG_KEXEC */
249

250 251 252 253 254 255 256
#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
static unsigned long pnv_memory_block_size(void)
{
	return 256UL * 1024 * 1024;
}
#endif

257
static void __init pnv_setup_machdep_opal(void)
258
{
259 260
	ppc_md.get_boot_time = opal_get_boot_time;
	ppc_md.restart = pnv_restart;
261
	pm_power_off = pnv_power_off;
262
	ppc_md.halt = pnv_halt;
263
	ppc_md.machine_check_exception = opal_machine_check;
264
	ppc_md.mce_check_early_recovery = opal_mce_check_early_recovery;
265 266
	ppc_md.hmi_exception_early = opal_hmi_exception_early;
	ppc_md.handle_hmi_exception = opal_handle_hmi_exception;
267 268 269 270 271 272 273 274 275
}

static int __init pnv_probe(void)
{
	unsigned long root = of_get_flat_dt_root();

	if (!of_flat_dt_is_compatible(root, "ibm,powernv"))
		return 0;

276 277 278
	if (firmware_has_feature(FW_FEATURE_OPAL))
		pnv_setup_machdep_opal();

279 280 281 282 283
	pr_debug("PowerNV detected !\n");

	return 1;
}

284 285 286 287
/*
 * Returns the cpu frequency for 'cpu' in Hz. This is used by
 * /proc/cpuinfo
 */
288
static unsigned long pnv_get_proc_freq(unsigned int cpu)
289 290 291 292 293 294 295 296 297 298 299 300 301 302
{
	unsigned long ret_freq;

	ret_freq = cpufreq_quick_get(cpu) * 1000ul;

	/*
	 * If the backend cpufreq driver does not exist,
         * then fallback to old way of reporting the clockrate.
	 */
	if (!ret_freq)
		ret_freq = ppc_proc_freq;
	return ret_freq;
}

303 304 305 306
define_machine(powernv) {
	.name			= "PowerNV",
	.probe			= pnv_probe,
	.init_early		= pnv_init_early,
307
	.setup_arch		= pnv_setup_arch,
308 309
	.init_IRQ		= pnv_init_IRQ,
	.show_cpuinfo		= pnv_show_cpuinfo,
310
	.get_proc_freq          = pnv_get_proc_freq,
311
	.progress		= pnv_progress,
312
	.machine_shutdown	= pnv_shutdown,
313
	.power_save             = NULL,
314 315 316 317
	.calibrate_decr		= generic_calibrate_decr,
#ifdef CONFIG_KEXEC
	.kexec_cpu_down		= pnv_kexec_cpu_down,
#endif
318 319 320
#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
	.memory_block_size	= pnv_memory_block_size,
#endif
321
};