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>
2037576cb0SMichael Ellerman #include <linux/seq_buf.h>
2155190f88SBenjamin Herrenschmidt #include <linux/seq_file.h>
2255190f88SBenjamin Herrenschmidt #include <linux/of.h>
2326a2056eSRob Herring #include <linux/of_fdt.h>
2455190f88SBenjamin Herrenschmidt #include <linux/interrupt.h>
2555190f88SBenjamin Herrenschmidt #include <linux/bug.h>
26cd15b048SBenjamin Herrenschmidt #include <linux/pci.h>
27fb5153d0SGautham R. Shenoy #include <linux/cpufreq.h>
287290f3b3SNicholas Piggin #include <linux/memblock.h>
2955190f88SBenjamin Herrenschmidt
3055190f88SBenjamin Herrenschmidt #include <asm/machdep.h>
3155190f88SBenjamin Herrenschmidt #include <asm/firmware.h>
3255190f88SBenjamin Herrenschmidt #include <asm/xics.h>
33243e2511SBenjamin Herrenschmidt #include <asm/xive.h>
34daea1175SBenjamin Herrenschmidt #include <asm/opal.h>
3513906db6SBenjamin Herrenschmidt #include <asm/kexec.h>
36b2a80878SShreyas B. Prabhu #include <asm/smp.h>
3754820530SMichael Ellerman #include <asm/tm.h>
386e032b35SOliver O'Halloran #include <asm/setup.h>
3977addf6eSMichael Ellerman #include <asm/security_features.h>
4055190f88SBenjamin Herrenschmidt
4155190f88SBenjamin Herrenschmidt #include "powernv.h"
4255190f88SBenjamin Herrenschmidt
4377addf6eSMichael Ellerman
fw_feature_is(const char * state,const char * name,struct device_node * fw_features)44e5913db1SNick Child static bool __init fw_feature_is(const char *state, const char *name,
4577addf6eSMichael Ellerman struct device_node *fw_features)
4677addf6eSMichael Ellerman {
4777addf6eSMichael Ellerman struct device_node *np;
4877addf6eSMichael Ellerman bool rc = false;
4977addf6eSMichael Ellerman
5077addf6eSMichael Ellerman np = of_get_child_by_name(fw_features, name);
5177addf6eSMichael Ellerman if (np) {
5277addf6eSMichael Ellerman rc = of_property_read_bool(np, state);
5377addf6eSMichael Ellerman of_node_put(np);
5477addf6eSMichael Ellerman }
5577addf6eSMichael Ellerman
5677addf6eSMichael Ellerman return rc;
5777addf6eSMichael Ellerman }
5877addf6eSMichael Ellerman
init_fw_feat_flags(struct device_node * np)59e5913db1SNick Child static void __init init_fw_feat_flags(struct device_node *np)
6077addf6eSMichael Ellerman {
6177addf6eSMichael Ellerman if (fw_feature_is("enabled", "inst-spec-barrier-ori31,31,0", np))
6277addf6eSMichael Ellerman security_ftr_set(SEC_FTR_SPEC_BAR_ORI31);
6377addf6eSMichael Ellerman
6477addf6eSMichael Ellerman if (fw_feature_is("enabled", "fw-bcctrl-serialized", np))
6577addf6eSMichael Ellerman security_ftr_set(SEC_FTR_BCCTRL_SERIALISED);
6677addf6eSMichael Ellerman
6737c0bdd0SMichael Ellerman if (fw_feature_is("enabled", "inst-l1d-flush-ori30,30,0", np))
6877addf6eSMichael Ellerman security_ftr_set(SEC_FTR_L1D_FLUSH_ORI30);
6977addf6eSMichael Ellerman
7077addf6eSMichael Ellerman if (fw_feature_is("enabled", "inst-l1d-flush-trig2", np))
7177addf6eSMichael Ellerman security_ftr_set(SEC_FTR_L1D_FLUSH_TRIG2);
7277addf6eSMichael Ellerman
7377addf6eSMichael Ellerman if (fw_feature_is("enabled", "fw-l1d-thread-split", np))
7477addf6eSMichael Ellerman security_ftr_set(SEC_FTR_L1D_THREAD_PRIV);
7577addf6eSMichael Ellerman
7677addf6eSMichael Ellerman if (fw_feature_is("enabled", "fw-count-cache-disabled", np))
7777addf6eSMichael Ellerman security_ftr_set(SEC_FTR_COUNT_CACHE_DISABLED);
7877addf6eSMichael Ellerman
7999d54754SMichael Ellerman if (fw_feature_is("enabled", "fw-count-cache-flush-bcctr2,0,0", np))
8099d54754SMichael Ellerman security_ftr_set(SEC_FTR_BCCTR_FLUSH_ASSIST);
8199d54754SMichael Ellerman
8299d54754SMichael Ellerman if (fw_feature_is("enabled", "needs-count-cache-flush-on-context-switch", np))
8399d54754SMichael Ellerman security_ftr_set(SEC_FTR_FLUSH_COUNT_CACHE);
8499d54754SMichael Ellerman
8577addf6eSMichael Ellerman /*
8677addf6eSMichael Ellerman * The features below are enabled by default, so we instead look to see
8777addf6eSMichael Ellerman * if firmware has *disabled* them, and clear them if so.
8877addf6eSMichael Ellerman */
8977addf6eSMichael Ellerman if (fw_feature_is("disabled", "speculation-policy-favor-security", np))
9077addf6eSMichael Ellerman security_ftr_clear(SEC_FTR_FAVOUR_SECURITY);
9177addf6eSMichael Ellerman
9277addf6eSMichael Ellerman if (fw_feature_is("disabled", "needs-l1d-flush-msr-pr-0-to-1", np))
9377addf6eSMichael Ellerman security_ftr_clear(SEC_FTR_L1D_FLUSH_PR);
9477addf6eSMichael Ellerman
9577addf6eSMichael Ellerman if (fw_feature_is("disabled", "needs-l1d-flush-msr-hv-1-to-0", np))
9677addf6eSMichael Ellerman security_ftr_clear(SEC_FTR_L1D_FLUSH_HV);
9777addf6eSMichael Ellerman
9877addf6eSMichael Ellerman if (fw_feature_is("disabled", "needs-spec-barrier-for-bound-checks", np))
9977addf6eSMichael Ellerman security_ftr_clear(SEC_FTR_BNDS_CHK_SPEC_BAR);
1002efee6adSRussell Currey
1012efee6adSRussell Currey if (fw_feature_is("enabled", "no-need-l1d-flush-msr-pr-1-to-0", np))
1022efee6adSRussell Currey security_ftr_clear(SEC_FTR_L1D_FLUSH_ENTRY);
1032efee6adSRussell Currey
1042efee6adSRussell Currey if (fw_feature_is("enabled", "no-need-l1d-flush-kernel-on-user-access", np))
1052efee6adSRussell Currey security_ftr_clear(SEC_FTR_L1D_FLUSH_UACCESS);
106d2a3c131SRussell Currey
107d2a3c131SRussell Currey if (fw_feature_is("enabled", "no-need-store-drain-on-priv-state-switch", np))
108d2a3c131SRussell Currey security_ftr_clear(SEC_FTR_STF_BARRIER);
10977addf6eSMichael Ellerman }
11077addf6eSMichael Ellerman
pnv_setup_security_mitigations(void)111e5913db1SNick Child static void __init pnv_setup_security_mitigations(void)
1126e032b35SOliver O'Halloran {
1136e032b35SOliver O'Halloran struct device_node *np, *fw_features;
1146e032b35SOliver O'Halloran enum l1d_flush_type type;
11537c0bdd0SMichael Ellerman bool enable;
1166e032b35SOliver O'Halloran
1176e032b35SOliver O'Halloran /* Default to fallback in case fw-features are not available */
1186e032b35SOliver O'Halloran type = L1D_FLUSH_FALLBACK;
1196e032b35SOliver O'Halloran
1206e032b35SOliver O'Halloran np = of_find_node_by_name(NULL, "ibm,opal");
1216e032b35SOliver O'Halloran fw_features = of_get_child_by_name(np, "fw-features");
1226e032b35SOliver O'Halloran of_node_put(np);
1236e032b35SOliver O'Halloran
1246e032b35SOliver O'Halloran if (fw_features) {
12577addf6eSMichael Ellerman init_fw_feat_flags(fw_features);
12637c0bdd0SMichael Ellerman of_node_put(fw_features);
12777addf6eSMichael Ellerman
12837c0bdd0SMichael Ellerman if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_TRIG2))
1296e032b35SOliver O'Halloran type = L1D_FLUSH_MTTRIG;
1306e032b35SOliver O'Halloran
13137c0bdd0SMichael Ellerman if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_ORI30))
1326e032b35SOliver O'Halloran type = L1D_FLUSH_ORI;
1336e032b35SOliver O'Halloran }
1346e032b35SOliver O'Halloran
135f7964378SNicholas Piggin /*
136d02fa40dSNicholas Piggin * The issues addressed by the entry and uaccess flush don't affect P7
137d02fa40dSNicholas Piggin * or P8, so on bare metal disable them explicitly in case firmware does
138d02fa40dSNicholas Piggin * not include the features to disable them. POWER9 and newer processors
139d02fa40dSNicholas Piggin * should have the appropriate firmware flags.
140f7964378SNicholas Piggin */
141d02fa40dSNicholas Piggin if (pvr_version_is(PVR_POWER7) || pvr_version_is(PVR_POWER7p) ||
142d02fa40dSNicholas Piggin pvr_version_is(PVR_POWER8E) || pvr_version_is(PVR_POWER8NVL) ||
143d02fa40dSNicholas Piggin pvr_version_is(PVR_POWER8)) {
144f7964378SNicholas Piggin security_ftr_clear(SEC_FTR_L1D_FLUSH_ENTRY);
1459a32a7e7SNicholas Piggin security_ftr_clear(SEC_FTR_L1D_FLUSH_UACCESS);
1469a32a7e7SNicholas Piggin }
147f7964378SNicholas Piggin
14837c0bdd0SMichael Ellerman enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) && \
14937c0bdd0SMichael Ellerman (security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR) || \
15037c0bdd0SMichael Ellerman security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV));
15137c0bdd0SMichael Ellerman
15237c0bdd0SMichael Ellerman setup_rfi_flush(type, enable);
15399d54754SMichael Ellerman setup_count_cache_flush();
154f7964378SNicholas Piggin
155f7964378SNicholas Piggin enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
156f7964378SNicholas Piggin security_ftr_enabled(SEC_FTR_L1D_FLUSH_ENTRY);
157f7964378SNicholas Piggin setup_entry_flush(enable);
1589a32a7e7SNicholas Piggin
1599a32a7e7SNicholas Piggin enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
1609a32a7e7SNicholas Piggin security_ftr_enabled(SEC_FTR_L1D_FLUSH_UACCESS);
1619a32a7e7SNicholas Piggin setup_uaccess_flush(enable);
162da631f7fSDaniel Axtens
163da631f7fSDaniel Axtens setup_stf_barrier();
1646e032b35SOliver O'Halloran }
1656e032b35SOliver O'Halloran
pnv_check_guarded_cores(void)1668f55984fSJoel Stanley static void __init pnv_check_guarded_cores(void)
1678f55984fSJoel Stanley {
1688f55984fSJoel Stanley struct device_node *dn;
1698f55984fSJoel Stanley int bad_count = 0;
1708f55984fSJoel Stanley
1718f55984fSJoel Stanley for_each_node_by_type(dn, "cpu") {
1728f55984fSJoel Stanley if (of_property_match_string(dn, "status", "bad") >= 0)
1738f55984fSJoel Stanley bad_count++;
174caea7b83SYang Li }
1758f55984fSJoel Stanley
1768f55984fSJoel Stanley if (bad_count) {
1778f55984fSJoel Stanley printk(" _ _______________\n");
1788f55984fSJoel Stanley pr_cont(" | | / \\\n");
1798f55984fSJoel Stanley pr_cont(" | | | WARNING! |\n");
1808f55984fSJoel Stanley pr_cont(" | | | |\n");
1818f55984fSJoel Stanley pr_cont(" | | | It looks like |\n");
1828f55984fSJoel Stanley pr_cont(" |_| | you have %*d |\n", 3, bad_count);
1838f55984fSJoel Stanley pr_cont(" _ | guarded cores |\n");
1848f55984fSJoel Stanley pr_cont(" (_) \\_______________/\n");
1858f55984fSJoel Stanley }
1868f55984fSJoel Stanley }
1878f55984fSJoel Stanley
pnv_setup_arch(void)18855190f88SBenjamin Herrenschmidt static void __init pnv_setup_arch(void)
18955190f88SBenjamin Herrenschmidt {
1904817fc32SAnton Blanchard set_arch_panic_timeout(10, ARCH_PANIC_TIMEOUT);
1914817fc32SAnton Blanchard
192da631f7fSDaniel Axtens pnv_setup_security_mitigations();
1936e032b35SOliver O'Halloran
19455190f88SBenjamin Herrenschmidt /* Initialize SMP */
19555190f88SBenjamin Herrenschmidt pnv_smp_init();
19655190f88SBenjamin Herrenschmidt
197628daa8dSBenjamin Herrenschmidt /* Setup RTC and NVRAM callbacks */
198628daa8dSBenjamin Herrenschmidt if (firmware_has_feature(FW_FEATURE_OPAL))
199628daa8dSBenjamin Herrenschmidt opal_nvram_init();
20055190f88SBenjamin Herrenschmidt
20155190f88SBenjamin Herrenschmidt /* Enable NAP mode */
20255190f88SBenjamin Herrenschmidt powersave_nap = 1;
20355190f88SBenjamin Herrenschmidt
2048f55984fSJoel Stanley pnv_check_guarded_cores();
2058f55984fSJoel Stanley
20655190f88SBenjamin Herrenschmidt /* XXX PMCS */
207f3eac426SJason A. Donenfeld
208f3eac426SJason A. Donenfeld pnv_rng_init();
20955190f88SBenjamin Herrenschmidt }
21055190f88SBenjamin Herrenschmidt
pnv_add_hw_description(void)21137576cb0SMichael Ellerman static void __init pnv_add_hw_description(void)
21237576cb0SMichael Ellerman {
21337576cb0SMichael Ellerman struct device_node *dn;
21437576cb0SMichael Ellerman const char *s;
21537576cb0SMichael Ellerman
21637576cb0SMichael Ellerman dn = of_find_node_by_path("/ibm,opal/firmware");
21737576cb0SMichael Ellerman if (!dn)
21837576cb0SMichael Ellerman return;
21937576cb0SMichael Ellerman
22037576cb0SMichael Ellerman if (of_property_read_string(dn, "version", &s) == 0 ||
22137576cb0SMichael Ellerman of_property_read_string(dn, "git-id", &s) == 0)
22237576cb0SMichael Ellerman seq_buf_printf(&ppc_hw_desc, "opal:%s ", s);
22337576cb0SMichael Ellerman
22437576cb0SMichael Ellerman if (of_property_read_string(dn, "mi-version", &s) == 0)
22537576cb0SMichael Ellerman seq_buf_printf(&ppc_hw_desc, "mi:%s ", s);
22637576cb0SMichael Ellerman
22737576cb0SMichael Ellerman of_node_put(dn);
22837576cb0SMichael Ellerman }
22937576cb0SMichael Ellerman
pnv_init(void)230f2d57694SBenjamin Herrenschmidt static void __init pnv_init(void)
23155190f88SBenjamin Herrenschmidt {
23237576cb0SMichael Ellerman pnv_add_hw_description();
23337576cb0SMichael Ellerman
2343fafe9c2SBenjamin Herrenschmidt /*
2353fafe9c2SBenjamin Herrenschmidt * Initialize the LPC bus now so that legacy serial
2363fafe9c2SBenjamin Herrenschmidt * ports can be found on it
2373fafe9c2SBenjamin Herrenschmidt */
2383fafe9c2SBenjamin Herrenschmidt opal_lpc_init();
2393fafe9c2SBenjamin Herrenschmidt
240daea1175SBenjamin Herrenschmidt #ifdef CONFIG_HVC_OPAL
241daea1175SBenjamin Herrenschmidt if (firmware_has_feature(FW_FEATURE_OPAL))
242daea1175SBenjamin Herrenschmidt hvc_opal_init_early();
243daea1175SBenjamin Herrenschmidt else
244daea1175SBenjamin Herrenschmidt #endif
245daea1175SBenjamin Herrenschmidt add_preferred_console("hvc", 0, NULL);
2467290f3b3SNicholas Piggin
247387e220aSNicholas Piggin #ifdef CONFIG_PPC_64S_HASH_MMU
2487290f3b3SNicholas Piggin if (!radix_enabled()) {
249a1ee2811SNicholas Piggin size_t size = sizeof(struct slb_entry) * mmu_slb_size;
2507290f3b3SNicholas Piggin int i;
2517290f3b3SNicholas Piggin
2527290f3b3SNicholas Piggin /* Allocate per cpu area to save old slb contents during MCE */
253a1ee2811SNicholas Piggin for_each_possible_cpu(i) {
254a1ee2811SNicholas Piggin paca_ptrs[i]->mce_faulty_slbs =
255a1ee2811SNicholas Piggin memblock_alloc_node(size,
256a1ee2811SNicholas Piggin __alignof__(struct slb_entry),
257a1ee2811SNicholas Piggin cpu_to_node(i));
258a1ee2811SNicholas Piggin }
2597290f3b3SNicholas Piggin }
260387e220aSNicholas Piggin #endif
26155190f88SBenjamin Herrenschmidt }
26255190f88SBenjamin Herrenschmidt
pnv_init_IRQ(void)26355190f88SBenjamin Herrenschmidt static void __init pnv_init_IRQ(void)
26455190f88SBenjamin Herrenschmidt {
265243e2511SBenjamin Herrenschmidt /* Try using a XIVE if available, otherwise use a XICS */
266243e2511SBenjamin Herrenschmidt if (!xive_native_init())
26755190f88SBenjamin Herrenschmidt xics_init();
26855190f88SBenjamin Herrenschmidt
26955190f88SBenjamin Herrenschmidt WARN_ON(!ppc_md.get_irq);
27055190f88SBenjamin Herrenschmidt }
27155190f88SBenjamin Herrenschmidt
pnv_show_cpuinfo(struct seq_file * m)27255190f88SBenjamin Herrenschmidt static void pnv_show_cpuinfo(struct seq_file *m)
27355190f88SBenjamin Herrenschmidt {
27455190f88SBenjamin Herrenschmidt struct device_node *root;
27555190f88SBenjamin Herrenschmidt const char *model = "";
27655190f88SBenjamin Herrenschmidt
27755190f88SBenjamin Herrenschmidt root = of_find_node_by_path("/");
27855190f88SBenjamin Herrenschmidt if (root)
27955190f88SBenjamin Herrenschmidt model = of_get_property(root, "model", NULL);
28055190f88SBenjamin Herrenschmidt seq_printf(m, "machine\t\t: PowerNV %s\n", model);
281e4d54f71SStewart Smith if (firmware_has_feature(FW_FEATURE_OPAL))
282e4d54f71SStewart Smith seq_printf(m, "firmware\t: OPAL\n");
28314a43e69SBenjamin Herrenschmidt else
28414a43e69SBenjamin Herrenschmidt seq_printf(m, "firmware\t: BML\n");
28555190f88SBenjamin Herrenschmidt of_node_put(root);
2863a4c2601SAneesh Kumar K.V if (radix_enabled())
2873a4c2601SAneesh Kumar K.V seq_printf(m, "MMU\t\t: Radix\n");
2883a4c2601SAneesh Kumar K.V else
2893a4c2601SAneesh Kumar K.V seq_printf(m, "MMU\t\t: Hash\n");
29055190f88SBenjamin Herrenschmidt }
29155190f88SBenjamin Herrenschmidt
pnv_prepare_going_down(void)2922196c6f1SVasant Hegde static void pnv_prepare_going_down(void)
2932196c6f1SVasant Hegde {
2942196c6f1SVasant Hegde /*
2952196c6f1SVasant Hegde * Disable all notifiers from OPAL, we can't
2962196c6f1SVasant Hegde * service interrupts anymore anyway
2972196c6f1SVasant Hegde */
29881f2f7ceSAlistair Popple opal_event_shutdown();
2992196c6f1SVasant Hegde
300f2748bdfSNicholas Piggin /* Print flash update message if one is scheduled. */
301f2748bdfSNicholas Piggin opal_flash_update_print_message();
3022196c6f1SVasant Hegde
303f2748bdfSNicholas Piggin smp_send_stop();
304f2748bdfSNicholas Piggin
305f2748bdfSNicholas Piggin hard_irq_disable();
3062196c6f1SVasant Hegde }
3072196c6f1SVasant Hegde
pnv_restart(char * cmd)308ec27329fSBenjamin Herrenschmidt static void __noreturn pnv_restart(char *cmd)
30955190f88SBenjamin Herrenschmidt {
3108139046aSVaibhav Jain long rc;
311ec27329fSBenjamin Herrenschmidt
3122196c6f1SVasant Hegde pnv_prepare_going_down();
313e8e71fa4SGavin Shan
3148139046aSVaibhav Jain do {
31516985f2dSOliver O'Halloran if (!cmd || !strlen(cmd))
316ec27329fSBenjamin Herrenschmidt rc = opal_cec_reboot();
3178139046aSVaibhav Jain else if (strcmp(cmd, "full") == 0)
3188139046aSVaibhav Jain rc = opal_cec_reboot2(OPAL_REBOOT_FULL_IPL, NULL);
31937f6f8e8SOliver O'Halloran else if (strcmp(cmd, "mpipl") == 0)
32037f6f8e8SOliver O'Halloran rc = opal_cec_reboot2(OPAL_REBOOT_MPIPL, NULL);
32137f6f8e8SOliver O'Halloran else if (strcmp(cmd, "error") == 0)
32237f6f8e8SOliver O'Halloran rc = opal_cec_reboot2(OPAL_REBOOT_PLATFORM_ERROR, NULL);
323672e480aSOliver O'Halloran else if (strcmp(cmd, "fast") == 0)
324672e480aSOliver O'Halloran rc = opal_cec_reboot2(OPAL_REBOOT_FAST, NULL);
325ec27329fSBenjamin Herrenschmidt else
3268139046aSVaibhav Jain rc = OPAL_UNSUPPORTED;
3278139046aSVaibhav Jain
3288139046aSVaibhav Jain if (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
3298139046aSVaibhav Jain /* Opal is busy wait for some time and retry */
3308139046aSVaibhav Jain opal_poll_events(NULL);
331ec27329fSBenjamin Herrenschmidt mdelay(10);
3328139046aSVaibhav Jain
3338139046aSVaibhav Jain } else if (cmd && rc) {
3348139046aSVaibhav Jain /* Unknown error while issuing reboot */
3358139046aSVaibhav Jain if (rc == OPAL_UNSUPPORTED)
3368139046aSVaibhav Jain pr_err("Unsupported '%s' reboot.\n", cmd);
3378139046aSVaibhav Jain else
3388139046aSVaibhav Jain pr_err("Unable to issue '%s' reboot. Err=%ld\n",
3398139046aSVaibhav Jain cmd, rc);
3408139046aSVaibhav Jain pr_info("Forcing a cec-reboot\n");
3418139046aSVaibhav Jain cmd = NULL;
3428139046aSVaibhav Jain rc = OPAL_BUSY;
3438139046aSVaibhav Jain
3448139046aSVaibhav Jain } else if (rc != OPAL_SUCCESS) {
3458139046aSVaibhav Jain /* Unknown error while issuing cec-reboot */
3468139046aSVaibhav Jain pr_err("Unable to reboot. Err=%ld\n", rc);
347ec27329fSBenjamin Herrenschmidt }
3488139046aSVaibhav Jain
3498139046aSVaibhav Jain } while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT);
3508139046aSVaibhav Jain
351ec27329fSBenjamin Herrenschmidt for (;;)
352ec27329fSBenjamin Herrenschmidt opal_poll_events(NULL);
35355190f88SBenjamin Herrenschmidt }
35455190f88SBenjamin Herrenschmidt
pnv_power_off(void)355ec27329fSBenjamin Herrenschmidt static void __noreturn pnv_power_off(void)
35655190f88SBenjamin Herrenschmidt {
357ec27329fSBenjamin Herrenschmidt long rc = OPAL_BUSY;
358ec27329fSBenjamin Herrenschmidt
3592196c6f1SVasant Hegde pnv_prepare_going_down();
360e8e71fa4SGavin Shan
361ec27329fSBenjamin Herrenschmidt while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
362ec27329fSBenjamin Herrenschmidt rc = opal_cec_power_down(0);
363ec27329fSBenjamin Herrenschmidt if (rc == OPAL_BUSY_EVENT)
364ec27329fSBenjamin Herrenschmidt opal_poll_events(NULL);
365ec27329fSBenjamin Herrenschmidt else
366ec27329fSBenjamin Herrenschmidt mdelay(10);
367ec27329fSBenjamin Herrenschmidt }
368ec27329fSBenjamin Herrenschmidt for (;;)
369ec27329fSBenjamin Herrenschmidt opal_poll_events(NULL);
37055190f88SBenjamin Herrenschmidt }
37155190f88SBenjamin Herrenschmidt
pnv_halt(void)372ec27329fSBenjamin Herrenschmidt static void __noreturn pnv_halt(void)
37355190f88SBenjamin Herrenschmidt {
374ec27329fSBenjamin Herrenschmidt pnv_power_off();
37555190f88SBenjamin Herrenschmidt }
37655190f88SBenjamin Herrenschmidt
pnv_progress(char * s,unsigned short hex)37755190f88SBenjamin Herrenschmidt static void pnv_progress(char *s, unsigned short hex)
37855190f88SBenjamin Herrenschmidt {
37955190f88SBenjamin Herrenschmidt }
38055190f88SBenjamin Herrenschmidt
pnv_shutdown(void)38173ed148aSBenjamin Herrenschmidt static void pnv_shutdown(void)
38273ed148aSBenjamin Herrenschmidt {
38373ed148aSBenjamin Herrenschmidt /* Let the PCI code clear up IODA tables */
38473ed148aSBenjamin Herrenschmidt pnv_pci_shutdown();
38573ed148aSBenjamin Herrenschmidt
386f7d98d18SVasant Hegde /*
387f7d98d18SVasant Hegde * Stop OPAL activity: Unregister all OPAL interrupts so they
388f7d98d18SVasant Hegde * don't fire up while we kexec and make sure all potentially
389f7d98d18SVasant Hegde * DMA'ing ops are complete (such as dump retrieval).
39073ed148aSBenjamin Herrenschmidt */
39173ed148aSBenjamin Herrenschmidt opal_shutdown();
39273ed148aSBenjamin Herrenschmidt }
39373ed148aSBenjamin Herrenschmidt
394da665885SThiago Jung Bauermann #ifdef CONFIG_KEXEC_CORE
pnv_kexec_wait_secondaries_down(void)395298b34d7SBenjamin Herrenschmidt static void pnv_kexec_wait_secondaries_down(void)
396298b34d7SBenjamin Herrenschmidt {
397298b34d7SBenjamin Herrenschmidt int my_cpu, i, notified = -1;
398298b34d7SBenjamin Herrenschmidt
399298b34d7SBenjamin Herrenschmidt my_cpu = get_cpu();
400298b34d7SBenjamin Herrenschmidt
401298b34d7SBenjamin Herrenschmidt for_each_online_cpu(i) {
402298b34d7SBenjamin Herrenschmidt uint8_t status;
4031b70386cSSamuel Mendoza-Jonas int64_t rc, timeout = 1000;
404298b34d7SBenjamin Herrenschmidt
405298b34d7SBenjamin Herrenschmidt if (i == my_cpu)
406298b34d7SBenjamin Herrenschmidt continue;
407298b34d7SBenjamin Herrenschmidt
408298b34d7SBenjamin Herrenschmidt for (;;) {
409298b34d7SBenjamin Herrenschmidt rc = opal_query_cpu_status(get_hard_smp_processor_id(i),
410298b34d7SBenjamin Herrenschmidt &status);
411298b34d7SBenjamin Herrenschmidt if (rc != OPAL_SUCCESS || status != OPAL_THREAD_STARTED)
412298b34d7SBenjamin Herrenschmidt break;
413298b34d7SBenjamin Herrenschmidt barrier();
414298b34d7SBenjamin Herrenschmidt if (i != notified) {
415298b34d7SBenjamin Herrenschmidt printk(KERN_INFO "kexec: waiting for cpu %d "
416298b34d7SBenjamin Herrenschmidt "(physical %d) to enter OPAL\n",
417d2e60075SNicholas Piggin i, paca_ptrs[i]->hw_cpu_id);
418298b34d7SBenjamin Herrenschmidt notified = i;
419298b34d7SBenjamin Herrenschmidt }
4201b70386cSSamuel Mendoza-Jonas
4211b70386cSSamuel Mendoza-Jonas /*
4221b70386cSSamuel Mendoza-Jonas * On crash secondaries might be unreachable or hung,
4231b70386cSSamuel Mendoza-Jonas * so timeout if we've waited too long
4241b70386cSSamuel Mendoza-Jonas * */
4251b70386cSSamuel Mendoza-Jonas mdelay(1);
4261b70386cSSamuel Mendoza-Jonas if (timeout-- == 0) {
4271b70386cSSamuel Mendoza-Jonas printk(KERN_ERR "kexec: timed out waiting for "
4281b70386cSSamuel Mendoza-Jonas "cpu %d (physical %d) to enter OPAL\n",
429d2e60075SNicholas Piggin i, paca_ptrs[i]->hw_cpu_id);
4301b70386cSSamuel Mendoza-Jonas break;
4311b70386cSSamuel Mendoza-Jonas }
432298b34d7SBenjamin Herrenschmidt }
433298b34d7SBenjamin Herrenschmidt }
434298b34d7SBenjamin Herrenschmidt }
435298b34d7SBenjamin Herrenschmidt
pnv_kexec_cpu_down(int crash_shutdown,int secondary)43655190f88SBenjamin Herrenschmidt static void pnv_kexec_cpu_down(int crash_shutdown, int secondary)
43755190f88SBenjamin Herrenschmidt {
4381c0eaf0fSBenjamin Herrenschmidt u64 reinit_flags;
4391c0eaf0fSBenjamin Herrenschmidt
440243e2511SBenjamin Herrenschmidt if (xive_enabled())
441e27e0a94SBenjamin Herrenschmidt xive_teardown_cpu();
442243e2511SBenjamin Herrenschmidt else
44355190f88SBenjamin Herrenschmidt xics_kexec_teardown_cpu(secondary);
44413906db6SBenjamin Herrenschmidt
445e4d54f71SStewart Smith /* On OPAL, we return all CPUs to firmware */
446e4d54f71SStewart Smith if (!firmware_has_feature(FW_FEATURE_OPAL))
447298b34d7SBenjamin Herrenschmidt return;
448298b34d7SBenjamin Herrenschmidt
449298b34d7SBenjamin Herrenschmidt if (secondary) {
45013906db6SBenjamin Herrenschmidt /* Return secondary CPUs to firmware on OPAL v3 */
45113906db6SBenjamin Herrenschmidt mb();
45213906db6SBenjamin Herrenschmidt get_paca()->kexec_state = KEXEC_STATE_REAL_MODE;
45313906db6SBenjamin Herrenschmidt mb();
45413906db6SBenjamin Herrenschmidt
45513906db6SBenjamin Herrenschmidt /* Return the CPU to OPAL */
45613906db6SBenjamin Herrenschmidt opal_return_cpu();
457298b34d7SBenjamin Herrenschmidt } else {
458298b34d7SBenjamin Herrenschmidt /* Primary waits for the secondaries to have reached OPAL */
459298b34d7SBenjamin Herrenschmidt pnv_kexec_wait_secondaries_down();
460e72bb8a5SSamuel Mendoza-Jonas
461243e2511SBenjamin Herrenschmidt /* Switch XIVE back to emulation mode */
462243e2511SBenjamin Herrenschmidt if (xive_enabled())
463243e2511SBenjamin Herrenschmidt xive_shutdown();
464243e2511SBenjamin Herrenschmidt
465e72bb8a5SSamuel Mendoza-Jonas /*
466e72bb8a5SSamuel Mendoza-Jonas * We might be running as little-endian - now that interrupts
467e72bb8a5SSamuel Mendoza-Jonas * are disabled, reset the HILE bit to big-endian so we don't
468e72bb8a5SSamuel Mendoza-Jonas * take interrupts in the wrong endian later
4691c0eaf0fSBenjamin Herrenschmidt *
4701c0eaf0fSBenjamin Herrenschmidt * We reinit to enable both radix and hash on P9 to ensure
4711c0eaf0fSBenjamin Herrenschmidt * the mode used by the next kernel is always supported.
472e72bb8a5SSamuel Mendoza-Jonas */
4731c0eaf0fSBenjamin Herrenschmidt reinit_flags = OPAL_REINIT_CPUS_HILE_BE;
4741c0eaf0fSBenjamin Herrenschmidt if (cpu_has_feature(CPU_FTR_ARCH_300))
4751c0eaf0fSBenjamin Herrenschmidt reinit_flags |= OPAL_REINIT_CPUS_MMU_RADIX |
4761c0eaf0fSBenjamin Herrenschmidt OPAL_REINIT_CPUS_MMU_HASH;
4771c0eaf0fSBenjamin Herrenschmidt opal_reinit_cpus(reinit_flags);
47813906db6SBenjamin Herrenschmidt }
47955190f88SBenjamin Herrenschmidt }
480da665885SThiago Jung Bauermann #endif /* CONFIG_KEXEC_CORE */
48155190f88SBenjamin Herrenschmidt
48250f9481eSDavid Hildenbrand #ifdef CONFIG_MEMORY_HOTPLUG
pnv_memory_block_size(void)4836d97d7a2SAnton Blanchard static unsigned long pnv_memory_block_size(void)
4846d97d7a2SAnton Blanchard {
485*4d157211SAneesh Kumar K.V return memory_block_size;
4866d97d7a2SAnton Blanchard }
4876d97d7a2SAnton Blanchard #endif
4886d97d7a2SAnton Blanchard
pnv_setup_machdep_opal(void)489628daa8dSBenjamin Herrenschmidt static void __init pnv_setup_machdep_opal(void)
490628daa8dSBenjamin Herrenschmidt {
491628daa8dSBenjamin Herrenschmidt ppc_md.get_boot_time = opal_get_boot_time;
492628daa8dSBenjamin Herrenschmidt ppc_md.restart = pnv_restart;
4939178ba29SAlexander Graf pm_power_off = pnv_power_off;
494628daa8dSBenjamin Herrenschmidt ppc_md.halt = pnv_halt;
495e36d0a2eSNicholas Piggin /* ppc_md.system_reset_exception gets filled in by pnv_smp_init() */
496ed79ba9eSBenjamin Herrenschmidt ppc_md.machine_check_exception = opal_machine_check;
49755672ecfSMahesh Salgaonkar ppc_md.mce_check_early_recovery = opal_mce_check_early_recovery;
498de269129SMahesh Salgaonkar if (opal_check_token(OPAL_HANDLE_HMI2))
499de269129SMahesh Salgaonkar ppc_md.hmi_exception_early = opal_hmi_exception_early2;
500de269129SMahesh Salgaonkar else
5010869b6fdSMahesh Salgaonkar ppc_md.hmi_exception_early = opal_hmi_exception_early;
5020869b6fdSMahesh Salgaonkar ppc_md.handle_hmi_exception = opal_handle_hmi_exception;
503628daa8dSBenjamin Herrenschmidt }
504628daa8dSBenjamin Herrenschmidt
pnv_probe(void)50555190f88SBenjamin Herrenschmidt static int __init pnv_probe(void)
50655190f88SBenjamin Herrenschmidt {
507628daa8dSBenjamin Herrenschmidt if (firmware_has_feature(FW_FEATURE_OPAL))
508628daa8dSBenjamin Herrenschmidt pnv_setup_machdep_opal();
509628daa8dSBenjamin Herrenschmidt
51055190f88SBenjamin Herrenschmidt pr_debug("PowerNV detected !\n");
51155190f88SBenjamin Herrenschmidt
512f2d57694SBenjamin Herrenschmidt pnv_init();
513f2d57694SBenjamin Herrenschmidt
51455190f88SBenjamin Herrenschmidt return 1;
51555190f88SBenjamin Herrenschmidt }
51655190f88SBenjamin Herrenschmidt
51754820530SMichael Ellerman #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
pnv_tm_init(void)51854820530SMichael Ellerman void __init pnv_tm_init(void)
51954820530SMichael Ellerman {
52054820530SMichael Ellerman if (!firmware_has_feature(FW_FEATURE_OPAL) ||
52154820530SMichael Ellerman !pvr_version_is(PVR_POWER9) ||
52254820530SMichael Ellerman early_cpu_has_feature(CPU_FTR_TM))
52354820530SMichael Ellerman return;
52454820530SMichael Ellerman
52554820530SMichael Ellerman if (opal_reinit_cpus(OPAL_REINIT_CPUS_TM_SUSPEND_DISABLED) != OPAL_SUCCESS)
52654820530SMichael Ellerman return;
52754820530SMichael Ellerman
52854820530SMichael Ellerman pr_info("Enabling TM (Transactional Memory) with Suspend Disabled\n");
52954820530SMichael Ellerman cur_cpu_spec->cpu_features |= CPU_FTR_TM;
53054820530SMichael Ellerman /* Make sure "normal" HTM is off (it should be) */
53154820530SMichael Ellerman cur_cpu_spec->cpu_user_features2 &= ~PPC_FEATURE2_HTM;
53254820530SMichael Ellerman /* Turn on no suspend mode, and HTM no SC */
53354820530SMichael Ellerman cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_HTM_NO_SUSPEND | \
53454820530SMichael Ellerman PPC_FEATURE2_HTM_NOSC;
53554820530SMichael Ellerman tm_suspend_disabled = true;
53654820530SMichael Ellerman }
53754820530SMichael Ellerman #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
53854820530SMichael Ellerman
539fb5153d0SGautham R. Shenoy /*
540fb5153d0SGautham R. Shenoy * Returns the cpu frequency for 'cpu' in Hz. This is used by
541fb5153d0SGautham R. Shenoy * /proc/cpuinfo
542fb5153d0SGautham R. Shenoy */
pnv_get_proc_freq(unsigned int cpu)543e51df2c1SAnton Blanchard static unsigned long pnv_get_proc_freq(unsigned int cpu)
544fb5153d0SGautham R. Shenoy {
545fb5153d0SGautham R. Shenoy unsigned long ret_freq;
546fb5153d0SGautham R. Shenoy
547cd77b5ceSShriya ret_freq = cpufreq_get(cpu) * 1000ul;
548fb5153d0SGautham R. Shenoy
549fb5153d0SGautham R. Shenoy /*
550fb5153d0SGautham R. Shenoy * If the backend cpufreq driver does not exist,
551fb5153d0SGautham R. Shenoy * then fallback to old way of reporting the clockrate.
552fb5153d0SGautham R. Shenoy */
553fb5153d0SGautham R. Shenoy if (!ret_freq)
554fb5153d0SGautham R. Shenoy ret_freq = ppc_proc_freq;
555fb5153d0SGautham R. Shenoy return ret_freq;
556fb5153d0SGautham R. Shenoy }
557fb5153d0SGautham R. Shenoy
pnv_machine_check_early(struct pt_regs * regs)558a43c1590SMahesh Salgaonkar static long pnv_machine_check_early(struct pt_regs *regs)
559a43c1590SMahesh Salgaonkar {
560a43c1590SMahesh Salgaonkar long handled = 0;
561a43c1590SMahesh Salgaonkar
562a43c1590SMahesh Salgaonkar if (cur_cpu_spec && cur_cpu_spec->machine_check_early)
563a43c1590SMahesh Salgaonkar handled = cur_cpu_spec->machine_check_early(regs);
564a43c1590SMahesh Salgaonkar
565a43c1590SMahesh Salgaonkar return handled;
566a43c1590SMahesh Salgaonkar }
567a43c1590SMahesh Salgaonkar
define_machine(powernv)56855190f88SBenjamin Herrenschmidt define_machine(powernv) {
56955190f88SBenjamin Herrenschmidt .name = "PowerNV",
5701c96fcdeSChristophe Leroy .compatible = "ibm,powernv",
57155190f88SBenjamin Herrenschmidt .probe = pnv_probe,
572628daa8dSBenjamin Herrenschmidt .setup_arch = pnv_setup_arch,
57355190f88SBenjamin Herrenschmidt .init_IRQ = pnv_init_IRQ,
57455190f88SBenjamin Herrenschmidt .show_cpuinfo = pnv_show_cpuinfo,
575fb5153d0SGautham R. Shenoy .get_proc_freq = pnv_get_proc_freq,
576fbbefb32SOliver O'Halloran .discover_phbs = pnv_pci_init,
57755190f88SBenjamin Herrenschmidt .progress = pnv_progress,
57873ed148aSBenjamin Herrenschmidt .machine_shutdown = pnv_shutdown,
5795593e303SShreyas B. Prabhu .power_save = NULL,
580a43c1590SMahesh Salgaonkar .machine_check_early = pnv_machine_check_early,
581da665885SThiago Jung Bauermann #ifdef CONFIG_KEXEC_CORE
58255190f88SBenjamin Herrenschmidt .kexec_cpu_down = pnv_kexec_cpu_down,
58355190f88SBenjamin Herrenschmidt #endif
58450f9481eSDavid Hildenbrand #ifdef CONFIG_MEMORY_HOTPLUG
5856d97d7a2SAnton Blanchard .memory_block_size = pnv_memory_block_size,
5866d97d7a2SAnton Blanchard #endif
58755190f88SBenjamin Herrenschmidt };
588