12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
255190f88SBenjamin Herrenschmidt /*
355190f88SBenjamin Herrenschmidt  * PowerNV setup code.
455190f88SBenjamin Herrenschmidt  *
555190f88SBenjamin Herrenschmidt  * Copyright 2011 IBM Corp.
655190f88SBenjamin Herrenschmidt  */
755190f88SBenjamin Herrenschmidt 
855190f88SBenjamin Herrenschmidt #undef DEBUG
955190f88SBenjamin Herrenschmidt 
1055190f88SBenjamin Herrenschmidt #include <linux/cpu.h>
1155190f88SBenjamin Herrenschmidt #include <linux/errno.h>
1255190f88SBenjamin Herrenschmidt #include <linux/sched.h>
1355190f88SBenjamin Herrenschmidt #include <linux/kernel.h>
1455190f88SBenjamin Herrenschmidt #include <linux/tty.h>
1555190f88SBenjamin Herrenschmidt #include <linux/reboot.h>
1655190f88SBenjamin Herrenschmidt #include <linux/init.h>
1755190f88SBenjamin Herrenschmidt #include <linux/console.h>
1855190f88SBenjamin Herrenschmidt #include <linux/delay.h>
1955190f88SBenjamin Herrenschmidt #include <linux/irq.h>
2055190f88SBenjamin Herrenschmidt #include <linux/seq_file.h>
2155190f88SBenjamin Herrenschmidt #include <linux/of.h>
2226a2056eSRob Herring #include <linux/of_fdt.h>
2355190f88SBenjamin Herrenschmidt #include <linux/interrupt.h>
2455190f88SBenjamin Herrenschmidt #include <linux/bug.h>
25cd15b048SBenjamin Herrenschmidt #include <linux/pci.h>
26fb5153d0SGautham R. Shenoy #include <linux/cpufreq.h>
277290f3b3SNicholas Piggin #include <linux/memblock.h>
2855190f88SBenjamin Herrenschmidt 
2955190f88SBenjamin Herrenschmidt #include <asm/machdep.h>
3055190f88SBenjamin Herrenschmidt #include <asm/firmware.h>
3155190f88SBenjamin Herrenschmidt #include <asm/xics.h>
32243e2511SBenjamin Herrenschmidt #include <asm/xive.h>
33daea1175SBenjamin Herrenschmidt #include <asm/opal.h>
3413906db6SBenjamin Herrenschmidt #include <asm/kexec.h>
35b2a80878SShreyas B. Prabhu #include <asm/smp.h>
3654820530SMichael Ellerman #include <asm/tm.h>
376e032b35SOliver O'Halloran #include <asm/setup.h>
3877addf6eSMichael Ellerman #include <asm/security_features.h>
3955190f88SBenjamin Herrenschmidt 
4055190f88SBenjamin Herrenschmidt #include "powernv.h"
4155190f88SBenjamin Herrenschmidt 
4277addf6eSMichael Ellerman 
43e5913db1SNick Child static bool __init fw_feature_is(const char *state, const char *name,
4477addf6eSMichael Ellerman 			  struct device_node *fw_features)
4577addf6eSMichael Ellerman {
4677addf6eSMichael Ellerman 	struct device_node *np;
4777addf6eSMichael Ellerman 	bool rc = false;
4877addf6eSMichael Ellerman 
4977addf6eSMichael Ellerman 	np = of_get_child_by_name(fw_features, name);
5077addf6eSMichael Ellerman 	if (np) {
5177addf6eSMichael Ellerman 		rc = of_property_read_bool(np, state);
5277addf6eSMichael Ellerman 		of_node_put(np);
5377addf6eSMichael Ellerman 	}
5477addf6eSMichael Ellerman 
5577addf6eSMichael Ellerman 	return rc;
5677addf6eSMichael Ellerman }
5777addf6eSMichael Ellerman 
58e5913db1SNick Child static void __init init_fw_feat_flags(struct device_node *np)
5977addf6eSMichael Ellerman {
6077addf6eSMichael Ellerman 	if (fw_feature_is("enabled", "inst-spec-barrier-ori31,31,0", np))
6177addf6eSMichael Ellerman 		security_ftr_set(SEC_FTR_SPEC_BAR_ORI31);
6277addf6eSMichael Ellerman 
6377addf6eSMichael Ellerman 	if (fw_feature_is("enabled", "fw-bcctrl-serialized", np))
6477addf6eSMichael Ellerman 		security_ftr_set(SEC_FTR_BCCTRL_SERIALISED);
6577addf6eSMichael Ellerman 
6637c0bdd0SMichael Ellerman 	if (fw_feature_is("enabled", "inst-l1d-flush-ori30,30,0", np))
6777addf6eSMichael Ellerman 		security_ftr_set(SEC_FTR_L1D_FLUSH_ORI30);
6877addf6eSMichael Ellerman 
6977addf6eSMichael Ellerman 	if (fw_feature_is("enabled", "inst-l1d-flush-trig2", np))
7077addf6eSMichael Ellerman 		security_ftr_set(SEC_FTR_L1D_FLUSH_TRIG2);
7177addf6eSMichael Ellerman 
7277addf6eSMichael Ellerman 	if (fw_feature_is("enabled", "fw-l1d-thread-split", np))
7377addf6eSMichael Ellerman 		security_ftr_set(SEC_FTR_L1D_THREAD_PRIV);
7477addf6eSMichael Ellerman 
7577addf6eSMichael Ellerman 	if (fw_feature_is("enabled", "fw-count-cache-disabled", np))
7677addf6eSMichael Ellerman 		security_ftr_set(SEC_FTR_COUNT_CACHE_DISABLED);
7777addf6eSMichael Ellerman 
7899d54754SMichael Ellerman 	if (fw_feature_is("enabled", "fw-count-cache-flush-bcctr2,0,0", np))
7999d54754SMichael Ellerman 		security_ftr_set(SEC_FTR_BCCTR_FLUSH_ASSIST);
8099d54754SMichael Ellerman 
8199d54754SMichael Ellerman 	if (fw_feature_is("enabled", "needs-count-cache-flush-on-context-switch", np))
8299d54754SMichael Ellerman 		security_ftr_set(SEC_FTR_FLUSH_COUNT_CACHE);
8399d54754SMichael Ellerman 
8477addf6eSMichael Ellerman 	/*
8577addf6eSMichael Ellerman 	 * The features below are enabled by default, so we instead look to see
8677addf6eSMichael Ellerman 	 * if firmware has *disabled* them, and clear them if so.
8777addf6eSMichael Ellerman 	 */
8877addf6eSMichael Ellerman 	if (fw_feature_is("disabled", "speculation-policy-favor-security", np))
8977addf6eSMichael Ellerman 		security_ftr_clear(SEC_FTR_FAVOUR_SECURITY);
9077addf6eSMichael Ellerman 
9177addf6eSMichael Ellerman 	if (fw_feature_is("disabled", "needs-l1d-flush-msr-pr-0-to-1", np))
9277addf6eSMichael Ellerman 		security_ftr_clear(SEC_FTR_L1D_FLUSH_PR);
9377addf6eSMichael Ellerman 
9477addf6eSMichael Ellerman 	if (fw_feature_is("disabled", "needs-l1d-flush-msr-hv-1-to-0", np))
9577addf6eSMichael Ellerman 		security_ftr_clear(SEC_FTR_L1D_FLUSH_HV);
9677addf6eSMichael Ellerman 
9777addf6eSMichael Ellerman 	if (fw_feature_is("disabled", "needs-spec-barrier-for-bound-checks", np))
9877addf6eSMichael Ellerman 		security_ftr_clear(SEC_FTR_BNDS_CHK_SPEC_BAR);
99*2efee6adSRussell Currey 
100*2efee6adSRussell Currey 	if (fw_feature_is("enabled", "no-need-l1d-flush-msr-pr-1-to-0", np))
101*2efee6adSRussell Currey 		security_ftr_clear(SEC_FTR_L1D_FLUSH_ENTRY);
102*2efee6adSRussell Currey 
103*2efee6adSRussell Currey 	if (fw_feature_is("enabled", "no-need-l1d-flush-kernel-on-user-access", np))
104*2efee6adSRussell Currey 		security_ftr_clear(SEC_FTR_L1D_FLUSH_UACCESS);
10577addf6eSMichael Ellerman }
10677addf6eSMichael Ellerman 
107e5913db1SNick Child static void __init pnv_setup_security_mitigations(void)
1086e032b35SOliver O'Halloran {
1096e032b35SOliver O'Halloran 	struct device_node *np, *fw_features;
1106e032b35SOliver O'Halloran 	enum l1d_flush_type type;
11137c0bdd0SMichael Ellerman 	bool enable;
1126e032b35SOliver O'Halloran 
1136e032b35SOliver O'Halloran 	/* Default to fallback in case fw-features are not available */
1146e032b35SOliver O'Halloran 	type = L1D_FLUSH_FALLBACK;
1156e032b35SOliver O'Halloran 
1166e032b35SOliver O'Halloran 	np = of_find_node_by_name(NULL, "ibm,opal");
1176e032b35SOliver O'Halloran 	fw_features = of_get_child_by_name(np, "fw-features");
1186e032b35SOliver O'Halloran 	of_node_put(np);
1196e032b35SOliver O'Halloran 
1206e032b35SOliver O'Halloran 	if (fw_features) {
12177addf6eSMichael Ellerman 		init_fw_feat_flags(fw_features);
12237c0bdd0SMichael Ellerman 		of_node_put(fw_features);
12377addf6eSMichael Ellerman 
12437c0bdd0SMichael Ellerman 		if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_TRIG2))
1256e032b35SOliver O'Halloran 			type = L1D_FLUSH_MTTRIG;
1266e032b35SOliver O'Halloran 
12737c0bdd0SMichael Ellerman 		if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_ORI30))
1286e032b35SOliver O'Halloran 			type = L1D_FLUSH_ORI;
1296e032b35SOliver O'Halloran 	}
1306e032b35SOliver O'Halloran 
131f7964378SNicholas Piggin 	/*
132d02fa40dSNicholas Piggin 	 * The issues addressed by the entry and uaccess flush don't affect P7
133d02fa40dSNicholas Piggin 	 * or P8, so on bare metal disable them explicitly in case firmware does
134d02fa40dSNicholas Piggin 	 * not include the features to disable them. POWER9 and newer processors
135d02fa40dSNicholas Piggin 	 * should have the appropriate firmware flags.
136f7964378SNicholas Piggin 	 */
137d02fa40dSNicholas Piggin 	if (pvr_version_is(PVR_POWER7) || pvr_version_is(PVR_POWER7p) ||
138d02fa40dSNicholas Piggin 	    pvr_version_is(PVR_POWER8E) || pvr_version_is(PVR_POWER8NVL) ||
139d02fa40dSNicholas Piggin 	    pvr_version_is(PVR_POWER8)) {
140f7964378SNicholas Piggin 		security_ftr_clear(SEC_FTR_L1D_FLUSH_ENTRY);
1419a32a7e7SNicholas Piggin 		security_ftr_clear(SEC_FTR_L1D_FLUSH_UACCESS);
1429a32a7e7SNicholas Piggin 	}
143f7964378SNicholas Piggin 
14437c0bdd0SMichael Ellerman 	enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) && \
14537c0bdd0SMichael Ellerman 		 (security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR)   || \
14637c0bdd0SMichael Ellerman 		  security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV));
14737c0bdd0SMichael Ellerman 
14837c0bdd0SMichael Ellerman 	setup_rfi_flush(type, enable);
14999d54754SMichael Ellerman 	setup_count_cache_flush();
150f7964378SNicholas Piggin 
151f7964378SNicholas Piggin 	enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
152f7964378SNicholas Piggin 		 security_ftr_enabled(SEC_FTR_L1D_FLUSH_ENTRY);
153f7964378SNicholas Piggin 	setup_entry_flush(enable);
1549a32a7e7SNicholas Piggin 
1559a32a7e7SNicholas Piggin 	enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
1569a32a7e7SNicholas Piggin 		 security_ftr_enabled(SEC_FTR_L1D_FLUSH_UACCESS);
1579a32a7e7SNicholas Piggin 	setup_uaccess_flush(enable);
158da631f7fSDaniel Axtens 
159da631f7fSDaniel Axtens 	setup_stf_barrier();
1606e032b35SOliver O'Halloran }
1616e032b35SOliver O'Halloran 
1628f55984fSJoel Stanley static void __init pnv_check_guarded_cores(void)
1638f55984fSJoel Stanley {
1648f55984fSJoel Stanley 	struct device_node *dn;
1658f55984fSJoel Stanley 	int bad_count = 0;
1668f55984fSJoel Stanley 
1678f55984fSJoel Stanley 	for_each_node_by_type(dn, "cpu") {
1688f55984fSJoel Stanley 		if (of_property_match_string(dn, "status", "bad") >= 0)
1698f55984fSJoel Stanley 			bad_count++;
170caea7b83SYang Li 	}
1718f55984fSJoel Stanley 
1728f55984fSJoel Stanley 	if (bad_count) {
1738f55984fSJoel Stanley 		printk("  _     _______________\n");
1748f55984fSJoel Stanley 		pr_cont(" | |   /               \\\n");
1758f55984fSJoel Stanley 		pr_cont(" | |   |    WARNING!   |\n");
1768f55984fSJoel Stanley 		pr_cont(" | |   |               |\n");
1778f55984fSJoel Stanley 		pr_cont(" | |   | It looks like |\n");
1788f55984fSJoel Stanley 		pr_cont(" |_|   |  you have %*d |\n", 3, bad_count);
1798f55984fSJoel Stanley 		pr_cont("  _    | guarded cores |\n");
1808f55984fSJoel Stanley 		pr_cont(" (_)   \\_______________/\n");
1818f55984fSJoel Stanley 	}
1828f55984fSJoel Stanley }
1838f55984fSJoel Stanley 
18455190f88SBenjamin Herrenschmidt static void __init pnv_setup_arch(void)
18555190f88SBenjamin Herrenschmidt {
1864817fc32SAnton Blanchard 	set_arch_panic_timeout(10, ARCH_PANIC_TIMEOUT);
1874817fc32SAnton Blanchard 
188da631f7fSDaniel Axtens 	pnv_setup_security_mitigations();
1896e032b35SOliver O'Halloran 
19055190f88SBenjamin Herrenschmidt 	/* Initialize SMP */
19155190f88SBenjamin Herrenschmidt 	pnv_smp_init();
19255190f88SBenjamin Herrenschmidt 
193628daa8dSBenjamin Herrenschmidt 	/* Setup RTC and NVRAM callbacks */
194628daa8dSBenjamin Herrenschmidt 	if (firmware_has_feature(FW_FEATURE_OPAL))
195628daa8dSBenjamin Herrenschmidt 		opal_nvram_init();
19655190f88SBenjamin Herrenschmidt 
19755190f88SBenjamin Herrenschmidt 	/* Enable NAP mode */
19855190f88SBenjamin Herrenschmidt 	powersave_nap = 1;
19955190f88SBenjamin Herrenschmidt 
2008f55984fSJoel Stanley 	pnv_check_guarded_cores();
2018f55984fSJoel Stanley 
20255190f88SBenjamin Herrenschmidt 	/* XXX PMCS */
20355190f88SBenjamin Herrenschmidt }
20455190f88SBenjamin Herrenschmidt 
205f2d57694SBenjamin Herrenschmidt static void __init pnv_init(void)
20655190f88SBenjamin Herrenschmidt {
2073fafe9c2SBenjamin Herrenschmidt 	/*
2083fafe9c2SBenjamin Herrenschmidt 	 * Initialize the LPC bus now so that legacy serial
2093fafe9c2SBenjamin Herrenschmidt 	 * ports can be found on it
2103fafe9c2SBenjamin Herrenschmidt 	 */
2113fafe9c2SBenjamin Herrenschmidt 	opal_lpc_init();
2123fafe9c2SBenjamin Herrenschmidt 
213daea1175SBenjamin Herrenschmidt #ifdef CONFIG_HVC_OPAL
214daea1175SBenjamin Herrenschmidt 	if (firmware_has_feature(FW_FEATURE_OPAL))
215daea1175SBenjamin Herrenschmidt 		hvc_opal_init_early();
216daea1175SBenjamin Herrenschmidt 	else
217daea1175SBenjamin Herrenschmidt #endif
218daea1175SBenjamin Herrenschmidt 		add_preferred_console("hvc", 0, NULL);
2197290f3b3SNicholas Piggin 
220387e220aSNicholas Piggin #ifdef CONFIG_PPC_64S_HASH_MMU
2217290f3b3SNicholas Piggin 	if (!radix_enabled()) {
222a1ee2811SNicholas Piggin 		size_t size = sizeof(struct slb_entry) * mmu_slb_size;
2237290f3b3SNicholas Piggin 		int i;
2247290f3b3SNicholas Piggin 
2257290f3b3SNicholas Piggin 		/* Allocate per cpu area to save old slb contents during MCE */
226a1ee2811SNicholas Piggin 		for_each_possible_cpu(i) {
227a1ee2811SNicholas Piggin 			paca_ptrs[i]->mce_faulty_slbs =
228a1ee2811SNicholas Piggin 					memblock_alloc_node(size,
229a1ee2811SNicholas Piggin 						__alignof__(struct slb_entry),
230a1ee2811SNicholas Piggin 						cpu_to_node(i));
231a1ee2811SNicholas Piggin 		}
2327290f3b3SNicholas Piggin 	}
233387e220aSNicholas Piggin #endif
23455190f88SBenjamin Herrenschmidt }
23555190f88SBenjamin Herrenschmidt 
23655190f88SBenjamin Herrenschmidt static void __init pnv_init_IRQ(void)
23755190f88SBenjamin Herrenschmidt {
238243e2511SBenjamin Herrenschmidt 	/* Try using a XIVE if available, otherwise use a XICS */
239243e2511SBenjamin Herrenschmidt 	if (!xive_native_init())
24055190f88SBenjamin Herrenschmidt 		xics_init();
24155190f88SBenjamin Herrenschmidt 
24255190f88SBenjamin Herrenschmidt 	WARN_ON(!ppc_md.get_irq);
24355190f88SBenjamin Herrenschmidt }
24455190f88SBenjamin Herrenschmidt 
24555190f88SBenjamin Herrenschmidt static void pnv_show_cpuinfo(struct seq_file *m)
24655190f88SBenjamin Herrenschmidt {
24755190f88SBenjamin Herrenschmidt 	struct device_node *root;
24855190f88SBenjamin Herrenschmidt 	const char *model = "";
24955190f88SBenjamin Herrenschmidt 
25055190f88SBenjamin Herrenschmidt 	root = of_find_node_by_path("/");
25155190f88SBenjamin Herrenschmidt 	if (root)
25255190f88SBenjamin Herrenschmidt 		model = of_get_property(root, "model", NULL);
25355190f88SBenjamin Herrenschmidt 	seq_printf(m, "machine\t\t: PowerNV %s\n", model);
254e4d54f71SStewart Smith 	if (firmware_has_feature(FW_FEATURE_OPAL))
255e4d54f71SStewart Smith 		seq_printf(m, "firmware\t: OPAL\n");
25614a43e69SBenjamin Herrenschmidt 	else
25714a43e69SBenjamin Herrenschmidt 		seq_printf(m, "firmware\t: BML\n");
25855190f88SBenjamin Herrenschmidt 	of_node_put(root);
2593a4c2601SAneesh Kumar K.V 	if (radix_enabled())
2603a4c2601SAneesh Kumar K.V 		seq_printf(m, "MMU\t\t: Radix\n");
2613a4c2601SAneesh Kumar K.V 	else
2623a4c2601SAneesh Kumar K.V 		seq_printf(m, "MMU\t\t: Hash\n");
26355190f88SBenjamin Herrenschmidt }
26455190f88SBenjamin Herrenschmidt 
2652196c6f1SVasant Hegde static void pnv_prepare_going_down(void)
2662196c6f1SVasant Hegde {
2672196c6f1SVasant Hegde 	/*
2682196c6f1SVasant Hegde 	 * Disable all notifiers from OPAL, we can't
2692196c6f1SVasant Hegde 	 * service interrupts anymore anyway
2702196c6f1SVasant Hegde 	 */
27181f2f7ceSAlistair Popple 	opal_event_shutdown();
2722196c6f1SVasant Hegde 
273f2748bdfSNicholas Piggin 	/* Print flash update message if one is scheduled. */
274f2748bdfSNicholas Piggin 	opal_flash_update_print_message();
2752196c6f1SVasant Hegde 
276f2748bdfSNicholas Piggin 	smp_send_stop();
277f2748bdfSNicholas Piggin 
278f2748bdfSNicholas Piggin 	hard_irq_disable();
2792196c6f1SVasant Hegde }
2802196c6f1SVasant Hegde 
281ec27329fSBenjamin Herrenschmidt static void  __noreturn pnv_restart(char *cmd)
28255190f88SBenjamin Herrenschmidt {
2838139046aSVaibhav Jain 	long rc;
284ec27329fSBenjamin Herrenschmidt 
2852196c6f1SVasant Hegde 	pnv_prepare_going_down();
286e8e71fa4SGavin Shan 
2878139046aSVaibhav Jain 	do {
28816985f2dSOliver O'Halloran 		if (!cmd || !strlen(cmd))
289ec27329fSBenjamin Herrenschmidt 			rc = opal_cec_reboot();
2908139046aSVaibhav Jain 		else if (strcmp(cmd, "full") == 0)
2918139046aSVaibhav Jain 			rc = opal_cec_reboot2(OPAL_REBOOT_FULL_IPL, NULL);
29237f6f8e8SOliver O'Halloran 		else if (strcmp(cmd, "mpipl") == 0)
29337f6f8e8SOliver O'Halloran 			rc = opal_cec_reboot2(OPAL_REBOOT_MPIPL, NULL);
29437f6f8e8SOliver O'Halloran 		else if (strcmp(cmd, "error") == 0)
29537f6f8e8SOliver O'Halloran 			rc = opal_cec_reboot2(OPAL_REBOOT_PLATFORM_ERROR, NULL);
296672e480aSOliver O'Halloran 		else if (strcmp(cmd, "fast") == 0)
297672e480aSOliver O'Halloran 			rc = opal_cec_reboot2(OPAL_REBOOT_FAST, NULL);
298ec27329fSBenjamin Herrenschmidt 		else
2998139046aSVaibhav Jain 			rc = OPAL_UNSUPPORTED;
3008139046aSVaibhav Jain 
3018139046aSVaibhav Jain 		if (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
3028139046aSVaibhav Jain 			/* Opal is busy wait for some time and retry */
3038139046aSVaibhav Jain 			opal_poll_events(NULL);
304ec27329fSBenjamin Herrenschmidt 			mdelay(10);
3058139046aSVaibhav Jain 
3068139046aSVaibhav Jain 		} else	if (cmd && rc) {
3078139046aSVaibhav Jain 			/* Unknown error while issuing reboot */
3088139046aSVaibhav Jain 			if (rc == OPAL_UNSUPPORTED)
3098139046aSVaibhav Jain 				pr_err("Unsupported '%s' reboot.\n", cmd);
3108139046aSVaibhav Jain 			else
3118139046aSVaibhav Jain 				pr_err("Unable to issue '%s' reboot. Err=%ld\n",
3128139046aSVaibhav Jain 				       cmd, rc);
3138139046aSVaibhav Jain 			pr_info("Forcing a cec-reboot\n");
3148139046aSVaibhav Jain 			cmd = NULL;
3158139046aSVaibhav Jain 			rc = OPAL_BUSY;
3168139046aSVaibhav Jain 
3178139046aSVaibhav Jain 		} else if (rc != OPAL_SUCCESS) {
3188139046aSVaibhav Jain 			/* Unknown error while issuing cec-reboot */
3198139046aSVaibhav Jain 			pr_err("Unable to reboot. Err=%ld\n", rc);
320ec27329fSBenjamin Herrenschmidt 		}
3218139046aSVaibhav Jain 
3228139046aSVaibhav Jain 	} while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT);
3238139046aSVaibhav Jain 
324ec27329fSBenjamin Herrenschmidt 	for (;;)
325ec27329fSBenjamin Herrenschmidt 		opal_poll_events(NULL);
32655190f88SBenjamin Herrenschmidt }
32755190f88SBenjamin Herrenschmidt 
328ec27329fSBenjamin Herrenschmidt static void __noreturn pnv_power_off(void)
32955190f88SBenjamin Herrenschmidt {
330ec27329fSBenjamin Herrenschmidt 	long rc = OPAL_BUSY;
331ec27329fSBenjamin Herrenschmidt 
3322196c6f1SVasant Hegde 	pnv_prepare_going_down();
333e8e71fa4SGavin Shan 
334ec27329fSBenjamin Herrenschmidt 	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
335ec27329fSBenjamin Herrenschmidt 		rc = opal_cec_power_down(0);
336ec27329fSBenjamin Herrenschmidt 		if (rc == OPAL_BUSY_EVENT)
337ec27329fSBenjamin Herrenschmidt 			opal_poll_events(NULL);
338ec27329fSBenjamin Herrenschmidt 		else
339ec27329fSBenjamin Herrenschmidt 			mdelay(10);
340ec27329fSBenjamin Herrenschmidt 	}
341ec27329fSBenjamin Herrenschmidt 	for (;;)
342ec27329fSBenjamin Herrenschmidt 		opal_poll_events(NULL);
34355190f88SBenjamin Herrenschmidt }
34455190f88SBenjamin Herrenschmidt 
345ec27329fSBenjamin Herrenschmidt static void __noreturn pnv_halt(void)
34655190f88SBenjamin Herrenschmidt {
347ec27329fSBenjamin Herrenschmidt 	pnv_power_off();
34855190f88SBenjamin Herrenschmidt }
34955190f88SBenjamin Herrenschmidt 
35055190f88SBenjamin Herrenschmidt static void pnv_progress(char *s, unsigned short hex)
35155190f88SBenjamin Herrenschmidt {
35255190f88SBenjamin Herrenschmidt }
35355190f88SBenjamin Herrenschmidt 
35473ed148aSBenjamin Herrenschmidt static void pnv_shutdown(void)
35573ed148aSBenjamin Herrenschmidt {
35673ed148aSBenjamin Herrenschmidt 	/* Let the PCI code clear up IODA tables */
35773ed148aSBenjamin Herrenschmidt 	pnv_pci_shutdown();
35873ed148aSBenjamin Herrenschmidt 
359f7d98d18SVasant Hegde 	/*
360f7d98d18SVasant Hegde 	 * Stop OPAL activity: Unregister all OPAL interrupts so they
361f7d98d18SVasant Hegde 	 * don't fire up while we kexec and make sure all potentially
362f7d98d18SVasant Hegde 	 * DMA'ing ops are complete (such as dump retrieval).
36373ed148aSBenjamin Herrenschmidt 	 */
36473ed148aSBenjamin Herrenschmidt 	opal_shutdown();
36573ed148aSBenjamin Herrenschmidt }
36673ed148aSBenjamin Herrenschmidt 
367da665885SThiago Jung Bauermann #ifdef CONFIG_KEXEC_CORE
368298b34d7SBenjamin Herrenschmidt static void pnv_kexec_wait_secondaries_down(void)
369298b34d7SBenjamin Herrenschmidt {
370298b34d7SBenjamin Herrenschmidt 	int my_cpu, i, notified = -1;
371298b34d7SBenjamin Herrenschmidt 
372298b34d7SBenjamin Herrenschmidt 	my_cpu = get_cpu();
373298b34d7SBenjamin Herrenschmidt 
374298b34d7SBenjamin Herrenschmidt 	for_each_online_cpu(i) {
375298b34d7SBenjamin Herrenschmidt 		uint8_t status;
3761b70386cSSamuel Mendoza-Jonas 		int64_t rc, timeout = 1000;
377298b34d7SBenjamin Herrenschmidt 
378298b34d7SBenjamin Herrenschmidt 		if (i == my_cpu)
379298b34d7SBenjamin Herrenschmidt 			continue;
380298b34d7SBenjamin Herrenschmidt 
381298b34d7SBenjamin Herrenschmidt 		for (;;) {
382298b34d7SBenjamin Herrenschmidt 			rc = opal_query_cpu_status(get_hard_smp_processor_id(i),
383298b34d7SBenjamin Herrenschmidt 						   &status);
384298b34d7SBenjamin Herrenschmidt 			if (rc != OPAL_SUCCESS || status != OPAL_THREAD_STARTED)
385298b34d7SBenjamin Herrenschmidt 				break;
386298b34d7SBenjamin Herrenschmidt 			barrier();
387298b34d7SBenjamin Herrenschmidt 			if (i != notified) {
388298b34d7SBenjamin Herrenschmidt 				printk(KERN_INFO "kexec: waiting for cpu %d "
389298b34d7SBenjamin Herrenschmidt 				       "(physical %d) to enter OPAL\n",
390d2e60075SNicholas Piggin 				       i, paca_ptrs[i]->hw_cpu_id);
391298b34d7SBenjamin Herrenschmidt 				notified = i;
392298b34d7SBenjamin Herrenschmidt 			}
3931b70386cSSamuel Mendoza-Jonas 
3941b70386cSSamuel Mendoza-Jonas 			/*
3951b70386cSSamuel Mendoza-Jonas 			 * On crash secondaries might be unreachable or hung,
3961b70386cSSamuel Mendoza-Jonas 			 * so timeout if we've waited too long
3971b70386cSSamuel Mendoza-Jonas 			 * */
3981b70386cSSamuel Mendoza-Jonas 			mdelay(1);
3991b70386cSSamuel Mendoza-Jonas 			if (timeout-- == 0) {
4001b70386cSSamuel Mendoza-Jonas 				printk(KERN_ERR "kexec: timed out waiting for "
4011b70386cSSamuel Mendoza-Jonas 				       "cpu %d (physical %d) to enter OPAL\n",
402d2e60075SNicholas Piggin 				       i, paca_ptrs[i]->hw_cpu_id);
4031b70386cSSamuel Mendoza-Jonas 				break;
4041b70386cSSamuel Mendoza-Jonas 			}
405298b34d7SBenjamin Herrenschmidt 		}
406298b34d7SBenjamin Herrenschmidt 	}
407298b34d7SBenjamin Herrenschmidt }
408298b34d7SBenjamin Herrenschmidt 
40955190f88SBenjamin Herrenschmidt static void pnv_kexec_cpu_down(int crash_shutdown, int secondary)
41055190f88SBenjamin Herrenschmidt {
4111c0eaf0fSBenjamin Herrenschmidt 	u64 reinit_flags;
4121c0eaf0fSBenjamin Herrenschmidt 
413243e2511SBenjamin Herrenschmidt 	if (xive_enabled())
414e27e0a94SBenjamin Herrenschmidt 		xive_teardown_cpu();
415243e2511SBenjamin Herrenschmidt 	else
41655190f88SBenjamin Herrenschmidt 		xics_kexec_teardown_cpu(secondary);
41713906db6SBenjamin Herrenschmidt 
418e4d54f71SStewart Smith 	/* On OPAL, we return all CPUs to firmware */
419e4d54f71SStewart Smith 	if (!firmware_has_feature(FW_FEATURE_OPAL))
420298b34d7SBenjamin Herrenschmidt 		return;
421298b34d7SBenjamin Herrenschmidt 
422298b34d7SBenjamin Herrenschmidt 	if (secondary) {
42313906db6SBenjamin Herrenschmidt 		/* Return secondary CPUs to firmware on OPAL v3 */
42413906db6SBenjamin Herrenschmidt 		mb();
42513906db6SBenjamin Herrenschmidt 		get_paca()->kexec_state = KEXEC_STATE_REAL_MODE;
42613906db6SBenjamin Herrenschmidt 		mb();
42713906db6SBenjamin Herrenschmidt 
42813906db6SBenjamin Herrenschmidt 		/* Return the CPU to OPAL */
42913906db6SBenjamin Herrenschmidt 		opal_return_cpu();
430298b34d7SBenjamin Herrenschmidt 	} else {
431298b34d7SBenjamin Herrenschmidt 		/* Primary waits for the secondaries to have reached OPAL */
432298b34d7SBenjamin Herrenschmidt 		pnv_kexec_wait_secondaries_down();
433e72bb8a5SSamuel Mendoza-Jonas 
434243e2511SBenjamin Herrenschmidt 		/* Switch XIVE back to emulation mode */
435243e2511SBenjamin Herrenschmidt 		if (xive_enabled())
436243e2511SBenjamin Herrenschmidt 			xive_shutdown();
437243e2511SBenjamin Herrenschmidt 
438e72bb8a5SSamuel Mendoza-Jonas 		/*
439e72bb8a5SSamuel Mendoza-Jonas 		 * We might be running as little-endian - now that interrupts
440e72bb8a5SSamuel Mendoza-Jonas 		 * are disabled, reset the HILE bit to big-endian so we don't
441e72bb8a5SSamuel Mendoza-Jonas 		 * take interrupts in the wrong endian later
4421c0eaf0fSBenjamin Herrenschmidt 		 *
4431c0eaf0fSBenjamin Herrenschmidt 		 * We reinit to enable both radix and hash on P9 to ensure
4441c0eaf0fSBenjamin Herrenschmidt 		 * the mode used by the next kernel is always supported.
445e72bb8a5SSamuel Mendoza-Jonas 		 */
4461c0eaf0fSBenjamin Herrenschmidt 		reinit_flags = OPAL_REINIT_CPUS_HILE_BE;
4471c0eaf0fSBenjamin Herrenschmidt 		if (cpu_has_feature(CPU_FTR_ARCH_300))
4481c0eaf0fSBenjamin Herrenschmidt 			reinit_flags |= OPAL_REINIT_CPUS_MMU_RADIX |
4491c0eaf0fSBenjamin Herrenschmidt 				OPAL_REINIT_CPUS_MMU_HASH;
4501c0eaf0fSBenjamin Herrenschmidt 		opal_reinit_cpus(reinit_flags);
45113906db6SBenjamin Herrenschmidt 	}
45255190f88SBenjamin Herrenschmidt }
453da665885SThiago Jung Bauermann #endif /* CONFIG_KEXEC_CORE */
45455190f88SBenjamin Herrenschmidt 
45550f9481eSDavid Hildenbrand #ifdef CONFIG_MEMORY_HOTPLUG
4566d97d7a2SAnton Blanchard static unsigned long pnv_memory_block_size(void)
4576d97d7a2SAnton Blanchard {
458af9d00e9SAneesh Kumar K.V 	/*
459af9d00e9SAneesh Kumar K.V 	 * We map the kernel linear region with 1GB large pages on radix. For
460af9d00e9SAneesh Kumar K.V 	 * memory hot unplug to work our memory block size must be at least
461af9d00e9SAneesh Kumar K.V 	 * this size.
462af9d00e9SAneesh Kumar K.V 	 */
463af9d00e9SAneesh Kumar K.V 	if (radix_enabled())
464af9d00e9SAneesh Kumar K.V 		return radix_mem_block_size;
465af9d00e9SAneesh Kumar K.V 	else
4666d97d7a2SAnton Blanchard 		return 256UL * 1024 * 1024;
4676d97d7a2SAnton Blanchard }
4686d97d7a2SAnton Blanchard #endif
4696d97d7a2SAnton Blanchard 
470628daa8dSBenjamin Herrenschmidt static void __init pnv_setup_machdep_opal(void)
471628daa8dSBenjamin Herrenschmidt {
472628daa8dSBenjamin Herrenschmidt 	ppc_md.get_boot_time = opal_get_boot_time;
473628daa8dSBenjamin Herrenschmidt 	ppc_md.restart = pnv_restart;
4749178ba29SAlexander Graf 	pm_power_off = pnv_power_off;
475628daa8dSBenjamin Herrenschmidt 	ppc_md.halt = pnv_halt;
476e36d0a2eSNicholas Piggin 	/* ppc_md.system_reset_exception gets filled in by pnv_smp_init() */
477ed79ba9eSBenjamin Herrenschmidt 	ppc_md.machine_check_exception = opal_machine_check;
47855672ecfSMahesh Salgaonkar 	ppc_md.mce_check_early_recovery = opal_mce_check_early_recovery;
479de269129SMahesh Salgaonkar 	if (opal_check_token(OPAL_HANDLE_HMI2))
480de269129SMahesh Salgaonkar 		ppc_md.hmi_exception_early = opal_hmi_exception_early2;
481de269129SMahesh Salgaonkar 	else
4820869b6fdSMahesh Salgaonkar 		ppc_md.hmi_exception_early = opal_hmi_exception_early;
4830869b6fdSMahesh Salgaonkar 	ppc_md.handle_hmi_exception = opal_handle_hmi_exception;
484628daa8dSBenjamin Herrenschmidt }
485628daa8dSBenjamin Herrenschmidt 
48655190f88SBenjamin Herrenschmidt static int __init pnv_probe(void)
48755190f88SBenjamin Herrenschmidt {
488406b0b6aSBenjamin Herrenschmidt 	if (!of_machine_is_compatible("ibm,powernv"))
48955190f88SBenjamin Herrenschmidt 		return 0;
49055190f88SBenjamin Herrenschmidt 
491628daa8dSBenjamin Herrenschmidt 	if (firmware_has_feature(FW_FEATURE_OPAL))
492628daa8dSBenjamin Herrenschmidt 		pnv_setup_machdep_opal();
493628daa8dSBenjamin Herrenschmidt 
49455190f88SBenjamin Herrenschmidt 	pr_debug("PowerNV detected !\n");
49555190f88SBenjamin Herrenschmidt 
496f2d57694SBenjamin Herrenschmidt 	pnv_init();
497f2d57694SBenjamin Herrenschmidt 
49855190f88SBenjamin Herrenschmidt 	return 1;
49955190f88SBenjamin Herrenschmidt }
50055190f88SBenjamin Herrenschmidt 
50154820530SMichael Ellerman #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
50254820530SMichael Ellerman void __init pnv_tm_init(void)
50354820530SMichael Ellerman {
50454820530SMichael Ellerman 	if (!firmware_has_feature(FW_FEATURE_OPAL) ||
50554820530SMichael Ellerman 	    !pvr_version_is(PVR_POWER9) ||
50654820530SMichael Ellerman 	    early_cpu_has_feature(CPU_FTR_TM))
50754820530SMichael Ellerman 		return;
50854820530SMichael Ellerman 
50954820530SMichael Ellerman 	if (opal_reinit_cpus(OPAL_REINIT_CPUS_TM_SUSPEND_DISABLED) != OPAL_SUCCESS)
51054820530SMichael Ellerman 		return;
51154820530SMichael Ellerman 
51254820530SMichael Ellerman 	pr_info("Enabling TM (Transactional Memory) with Suspend Disabled\n");
51354820530SMichael Ellerman 	cur_cpu_spec->cpu_features |= CPU_FTR_TM;
51454820530SMichael Ellerman 	/* Make sure "normal" HTM is off (it should be) */
51554820530SMichael Ellerman 	cur_cpu_spec->cpu_user_features2 &= ~PPC_FEATURE2_HTM;
51654820530SMichael Ellerman 	/* Turn on no suspend mode, and HTM no SC */
51754820530SMichael Ellerman 	cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_HTM_NO_SUSPEND | \
51854820530SMichael Ellerman 					    PPC_FEATURE2_HTM_NOSC;
51954820530SMichael Ellerman 	tm_suspend_disabled = true;
52054820530SMichael Ellerman }
52154820530SMichael Ellerman #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
52254820530SMichael Ellerman 
523fb5153d0SGautham R. Shenoy /*
524fb5153d0SGautham R. Shenoy  * Returns the cpu frequency for 'cpu' in Hz. This is used by
525fb5153d0SGautham R. Shenoy  * /proc/cpuinfo
526fb5153d0SGautham R. Shenoy  */
527e51df2c1SAnton Blanchard static unsigned long pnv_get_proc_freq(unsigned int cpu)
528fb5153d0SGautham R. Shenoy {
529fb5153d0SGautham R. Shenoy 	unsigned long ret_freq;
530fb5153d0SGautham R. Shenoy 
531cd77b5ceSShriya 	ret_freq = cpufreq_get(cpu) * 1000ul;
532fb5153d0SGautham R. Shenoy 
533fb5153d0SGautham R. Shenoy 	/*
534fb5153d0SGautham R. Shenoy 	 * If the backend cpufreq driver does not exist,
535fb5153d0SGautham R. Shenoy          * then fallback to old way of reporting the clockrate.
536fb5153d0SGautham R. Shenoy 	 */
537fb5153d0SGautham R. Shenoy 	if (!ret_freq)
538fb5153d0SGautham R. Shenoy 		ret_freq = ppc_proc_freq;
539fb5153d0SGautham R. Shenoy 	return ret_freq;
540fb5153d0SGautham R. Shenoy }
541fb5153d0SGautham R. Shenoy 
542a43c1590SMahesh Salgaonkar static long pnv_machine_check_early(struct pt_regs *regs)
543a43c1590SMahesh Salgaonkar {
544a43c1590SMahesh Salgaonkar 	long handled = 0;
545a43c1590SMahesh Salgaonkar 
546a43c1590SMahesh Salgaonkar 	if (cur_cpu_spec && cur_cpu_spec->machine_check_early)
547a43c1590SMahesh Salgaonkar 		handled = cur_cpu_spec->machine_check_early(regs);
548a43c1590SMahesh Salgaonkar 
549a43c1590SMahesh Salgaonkar 	return handled;
550a43c1590SMahesh Salgaonkar }
551a43c1590SMahesh Salgaonkar 
55255190f88SBenjamin Herrenschmidt define_machine(powernv) {
55355190f88SBenjamin Herrenschmidt 	.name			= "PowerNV",
55455190f88SBenjamin Herrenschmidt 	.probe			= pnv_probe,
555628daa8dSBenjamin Herrenschmidt 	.setup_arch		= pnv_setup_arch,
55655190f88SBenjamin Herrenschmidt 	.init_IRQ		= pnv_init_IRQ,
55755190f88SBenjamin Herrenschmidt 	.show_cpuinfo		= pnv_show_cpuinfo,
558fb5153d0SGautham R. Shenoy 	.get_proc_freq          = pnv_get_proc_freq,
559fbbefb32SOliver O'Halloran 	.discover_phbs		= pnv_pci_init,
56055190f88SBenjamin Herrenschmidt 	.progress		= pnv_progress,
56173ed148aSBenjamin Herrenschmidt 	.machine_shutdown	= pnv_shutdown,
5625593e303SShreyas B. Prabhu 	.power_save             = NULL,
56355190f88SBenjamin Herrenschmidt 	.calibrate_decr		= generic_calibrate_decr,
564a43c1590SMahesh Salgaonkar 	.machine_check_early	= pnv_machine_check_early,
565da665885SThiago Jung Bauermann #ifdef CONFIG_KEXEC_CORE
56655190f88SBenjamin Herrenschmidt 	.kexec_cpu_down		= pnv_kexec_cpu_down,
56755190f88SBenjamin Herrenschmidt #endif
56850f9481eSDavid Hildenbrand #ifdef CONFIG_MEMORY_HOTPLUG
5696d97d7a2SAnton Blanchard 	.memory_block_size	= pnv_memory_block_size,
5706d97d7a2SAnton Blanchard #endif
57155190f88SBenjamin Herrenschmidt };
572