Path: blob/master/arch/powerpc/platforms/powernv/setup.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* PowerNV setup code.3*4* Copyright 2011 IBM Corp.5*/67#undef DEBUG89#include <linux/cpu.h>10#include <linux/errno.h>11#include <linux/sched.h>12#include <linux/kernel.h>13#include <linux/tty.h>14#include <linux/reboot.h>15#include <linux/init.h>16#include <linux/console.h>17#include <linux/delay.h>18#include <linux/irq.h>19#include <linux/seq_buf.h>20#include <linux/seq_file.h>21#include <linux/of.h>22#include <linux/of_fdt.h>23#include <linux/interrupt.h>24#include <linux/bug.h>25#include <linux/pci.h>26#include <linux/cpufreq.h>27#include <linux/memblock.h>2829#include <asm/machdep.h>30#include <asm/firmware.h>31#include <asm/xics.h>32#include <asm/xive.h>33#include <asm/opal.h>34#include <asm/kexec.h>35#include <asm/smp.h>36#include <asm/tm.h>37#include <asm/setup.h>38#include <asm/security_features.h>3940#include "powernv.h"414243static bool __init fw_feature_is(const char *state, const char *name,44struct device_node *fw_features)45{46struct device_node *np;47bool rc = false;4849np = of_get_child_by_name(fw_features, name);50if (np) {51rc = of_property_read_bool(np, state);52of_node_put(np);53}5455return rc;56}5758static void __init init_fw_feat_flags(struct device_node *np)59{60if (fw_feature_is("enabled", "inst-spec-barrier-ori31,31,0", np))61security_ftr_set(SEC_FTR_SPEC_BAR_ORI31);6263if (fw_feature_is("enabled", "fw-bcctrl-serialized", np))64security_ftr_set(SEC_FTR_BCCTRL_SERIALISED);6566if (fw_feature_is("enabled", "inst-l1d-flush-ori30,30,0", np))67security_ftr_set(SEC_FTR_L1D_FLUSH_ORI30);6869if (fw_feature_is("enabled", "inst-l1d-flush-trig2", np))70security_ftr_set(SEC_FTR_L1D_FLUSH_TRIG2);7172if (fw_feature_is("enabled", "fw-l1d-thread-split", np))73security_ftr_set(SEC_FTR_L1D_THREAD_PRIV);7475if (fw_feature_is("enabled", "fw-count-cache-disabled", np))76security_ftr_set(SEC_FTR_COUNT_CACHE_DISABLED);7778if (fw_feature_is("enabled", "fw-count-cache-flush-bcctr2,0,0", np))79security_ftr_set(SEC_FTR_BCCTR_FLUSH_ASSIST);8081if (fw_feature_is("enabled", "needs-count-cache-flush-on-context-switch", np))82security_ftr_set(SEC_FTR_FLUSH_COUNT_CACHE);8384/*85* The features below are enabled by default, so we instead look to see86* if firmware has *disabled* them, and clear them if so.87*/88if (fw_feature_is("disabled", "speculation-policy-favor-security", np))89security_ftr_clear(SEC_FTR_FAVOUR_SECURITY);9091if (fw_feature_is("disabled", "needs-l1d-flush-msr-pr-0-to-1", np))92security_ftr_clear(SEC_FTR_L1D_FLUSH_PR);9394if (fw_feature_is("disabled", "needs-l1d-flush-msr-hv-1-to-0", np))95security_ftr_clear(SEC_FTR_L1D_FLUSH_HV);9697if (fw_feature_is("disabled", "needs-spec-barrier-for-bound-checks", np))98security_ftr_clear(SEC_FTR_BNDS_CHK_SPEC_BAR);99100if (fw_feature_is("enabled", "no-need-l1d-flush-msr-pr-1-to-0", np))101security_ftr_clear(SEC_FTR_L1D_FLUSH_ENTRY);102103if (fw_feature_is("enabled", "no-need-l1d-flush-kernel-on-user-access", np))104security_ftr_clear(SEC_FTR_L1D_FLUSH_UACCESS);105106if (fw_feature_is("enabled", "no-need-store-drain-on-priv-state-switch", np))107security_ftr_clear(SEC_FTR_STF_BARRIER);108}109110static void __init pnv_setup_security_mitigations(void)111{112struct device_node *np, *fw_features;113enum l1d_flush_type type;114bool enable;115116/* Default to fallback in case fw-features are not available */117type = L1D_FLUSH_FALLBACK;118119np = of_find_node_by_name(NULL, "ibm,opal");120fw_features = of_get_child_by_name(np, "fw-features");121of_node_put(np);122123if (fw_features) {124init_fw_feat_flags(fw_features);125of_node_put(fw_features);126127if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_TRIG2))128type = L1D_FLUSH_MTTRIG;129130if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_ORI30))131type = L1D_FLUSH_ORI;132}133134/*135* The issues addressed by the entry and uaccess flush don't affect P7136* or P8, so on bare metal disable them explicitly in case firmware does137* not include the features to disable them. POWER9 and newer processors138* should have the appropriate firmware flags.139*/140if (pvr_version_is(PVR_POWER7) || pvr_version_is(PVR_POWER7p) ||141pvr_version_is(PVR_POWER8E) || pvr_version_is(PVR_POWER8NVL) ||142pvr_version_is(PVR_POWER8)) {143security_ftr_clear(SEC_FTR_L1D_FLUSH_ENTRY);144security_ftr_clear(SEC_FTR_L1D_FLUSH_UACCESS);145}146147enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) && \148(security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR) || \149security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV));150151setup_rfi_flush(type, enable);152setup_count_cache_flush();153154enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&155security_ftr_enabled(SEC_FTR_L1D_FLUSH_ENTRY);156setup_entry_flush(enable);157158enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&159security_ftr_enabled(SEC_FTR_L1D_FLUSH_UACCESS);160setup_uaccess_flush(enable);161162setup_stf_barrier();163}164165static void __init pnv_check_guarded_cores(void)166{167struct device_node *dn;168int bad_count = 0;169170for_each_node_by_type(dn, "cpu") {171if (of_property_match_string(dn, "status", "bad") >= 0)172bad_count++;173}174175if (bad_count) {176printk(" _ _______________\n");177pr_cont(" | | / \\\n");178pr_cont(" | | | WARNING! |\n");179pr_cont(" | | | |\n");180pr_cont(" | | | It looks like |\n");181pr_cont(" |_| | you have %*d |\n", 3, bad_count);182pr_cont(" _ | guarded cores |\n");183pr_cont(" (_) \\_______________/\n");184}185}186187static void __init pnv_setup_arch(void)188{189set_arch_panic_timeout(10, ARCH_PANIC_TIMEOUT);190191pnv_setup_security_mitigations();192193/* Initialize SMP */194pnv_smp_init();195196/* Setup RTC and NVRAM callbacks */197if (firmware_has_feature(FW_FEATURE_OPAL))198opal_nvram_init();199200/* Enable NAP mode */201powersave_nap = 1;202203pnv_check_guarded_cores();204205/* XXX PMCS */206207pnv_rng_init();208}209210static void __init pnv_add_hw_description(void)211{212struct device_node *dn;213const char *s;214215dn = of_find_node_by_path("/ibm,opal/firmware");216if (!dn)217return;218219if (of_property_read_string(dn, "version", &s) == 0 ||220of_property_read_string(dn, "git-id", &s) == 0)221seq_buf_printf(&ppc_hw_desc, "opal:%s ", s);222223if (of_property_read_string(dn, "mi-version", &s) == 0)224seq_buf_printf(&ppc_hw_desc, "mi:%s ", s);225226of_node_put(dn);227}228229static void __init pnv_init(void)230{231pnv_add_hw_description();232233/*234* Initialize the LPC bus now so that legacy serial235* ports can be found on it236*/237opal_lpc_init();238239#ifdef CONFIG_HVC_OPAL240if (firmware_has_feature(FW_FEATURE_OPAL))241hvc_opal_init_early();242else243#endif244add_preferred_console("hvc", 0, NULL);245246#ifdef CONFIG_PPC_64S_HASH_MMU247if (!radix_enabled()) {248size_t size = sizeof(struct slb_entry) * mmu_slb_size;249int i;250251/* Allocate per cpu area to save old slb contents during MCE */252for_each_possible_cpu(i) {253paca_ptrs[i]->mce_faulty_slbs =254memblock_alloc_node(size,255__alignof__(struct slb_entry),256cpu_to_node(i));257}258}259#endif260}261262static void __init pnv_init_IRQ(void)263{264/* Try using a XIVE if available, otherwise use a XICS */265if (!xive_native_init())266xics_init();267268WARN_ON(!ppc_md.get_irq);269}270271static void pnv_show_cpuinfo(struct seq_file *m)272{273struct device_node *root;274const char *model = "";275276root = of_find_node_by_path("/");277if (root)278model = of_get_property(root, "model", NULL);279seq_printf(m, "machine\t\t: PowerNV %s\n", model);280if (firmware_has_feature(FW_FEATURE_OPAL))281seq_printf(m, "firmware\t: OPAL\n");282else283seq_printf(m, "firmware\t: BML\n");284of_node_put(root);285if (radix_enabled())286seq_printf(m, "MMU\t\t: Radix\n");287else288seq_printf(m, "MMU\t\t: Hash\n");289}290291static void pnv_prepare_going_down(void)292{293/*294* Disable all notifiers from OPAL, we can't295* service interrupts anymore anyway296*/297opal_event_shutdown();298299/* Print flash update message if one is scheduled. */300opal_flash_update_print_message();301302smp_send_stop();303304hard_irq_disable();305}306307static void __noreturn pnv_restart(char *cmd)308{309long rc;310311pnv_prepare_going_down();312313do {314if (!cmd || !strlen(cmd))315rc = opal_cec_reboot();316else if (strcmp(cmd, "full") == 0)317rc = opal_cec_reboot2(OPAL_REBOOT_FULL_IPL, NULL);318else if (strcmp(cmd, "mpipl") == 0)319rc = opal_cec_reboot2(OPAL_REBOOT_MPIPL, NULL);320else if (strcmp(cmd, "error") == 0)321rc = opal_cec_reboot2(OPAL_REBOOT_PLATFORM_ERROR, NULL);322else if (strcmp(cmd, "fast") == 0)323rc = opal_cec_reboot2(OPAL_REBOOT_FAST, NULL);324else325rc = OPAL_UNSUPPORTED;326327if (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {328/* Opal is busy wait for some time and retry */329opal_poll_events(NULL);330mdelay(10);331332} else if (cmd && rc) {333/* Unknown error while issuing reboot */334if (rc == OPAL_UNSUPPORTED)335pr_err("Unsupported '%s' reboot.\n", cmd);336else337pr_err("Unable to issue '%s' reboot. Err=%ld\n",338cmd, rc);339pr_info("Forcing a cec-reboot\n");340cmd = NULL;341rc = OPAL_BUSY;342343} else if (rc != OPAL_SUCCESS) {344/* Unknown error while issuing cec-reboot */345pr_err("Unable to reboot. Err=%ld\n", rc);346}347348} while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT);349350for (;;)351opal_poll_events(NULL);352}353354static void __noreturn pnv_power_off(void)355{356long rc = OPAL_BUSY;357358pnv_prepare_going_down();359360while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {361rc = opal_cec_power_down(0);362if (rc == OPAL_BUSY_EVENT)363opal_poll_events(NULL);364else365mdelay(10);366}367for (;;)368opal_poll_events(NULL);369}370371static void __noreturn pnv_halt(void)372{373pnv_power_off();374}375376static void pnv_progress(char *s, unsigned short hex)377{378}379380static void pnv_shutdown(void)381{382/* Let the PCI code clear up IODA tables */383pnv_pci_shutdown();384385/*386* Stop OPAL activity: Unregister all OPAL interrupts so they387* don't fire up while we kexec and make sure all potentially388* DMA'ing ops are complete (such as dump retrieval).389*/390opal_shutdown();391}392393#ifdef CONFIG_KEXEC_CORE394static void pnv_kexec_wait_secondaries_down(void)395{396int my_cpu, i, notified = -1;397398my_cpu = get_cpu();399400for_each_online_cpu(i) {401uint8_t status;402int64_t rc, timeout = 1000;403404if (i == my_cpu)405continue;406407for (;;) {408rc = opal_query_cpu_status(get_hard_smp_processor_id(i),409&status);410if (rc != OPAL_SUCCESS || status != OPAL_THREAD_STARTED)411break;412barrier();413if (i != notified) {414printk(KERN_INFO "kexec: waiting for cpu %d "415"(physical %d) to enter OPAL\n",416i, paca_ptrs[i]->hw_cpu_id);417notified = i;418}419420/*421* On crash secondaries might be unreachable or hung,422* so timeout if we've waited too long423* */424mdelay(1);425if (timeout-- == 0) {426printk(KERN_ERR "kexec: timed out waiting for "427"cpu %d (physical %d) to enter OPAL\n",428i, paca_ptrs[i]->hw_cpu_id);429break;430}431}432}433}434435static void pnv_kexec_cpu_down(int crash_shutdown, int secondary)436{437u64 reinit_flags;438439if (xive_enabled())440xive_teardown_cpu();441else442xics_kexec_teardown_cpu(secondary);443444/* On OPAL, we return all CPUs to firmware */445if (!firmware_has_feature(FW_FEATURE_OPAL))446return;447448if (secondary) {449/* Return secondary CPUs to firmware on OPAL v3 */450mb();451get_paca()->kexec_state = KEXEC_STATE_REAL_MODE;452mb();453454/* Return the CPU to OPAL */455opal_return_cpu();456} else {457/* Primary waits for the secondaries to have reached OPAL */458pnv_kexec_wait_secondaries_down();459460/* Switch XIVE back to emulation mode */461if (xive_enabled())462xive_shutdown();463464/*465* We might be running as little-endian - now that interrupts466* are disabled, reset the HILE bit to big-endian so we don't467* take interrupts in the wrong endian later468*469* We reinit to enable both radix and hash on P9 to ensure470* the mode used by the next kernel is always supported.471*/472reinit_flags = OPAL_REINIT_CPUS_HILE_BE;473if (cpu_has_feature(CPU_FTR_ARCH_300))474reinit_flags |= OPAL_REINIT_CPUS_MMU_RADIX |475OPAL_REINIT_CPUS_MMU_HASH;476opal_reinit_cpus(reinit_flags);477}478}479#endif /* CONFIG_KEXEC_CORE */480481#ifdef CONFIG_MEMORY_HOTPLUG482static unsigned long pnv_memory_block_size(void)483{484return memory_block_size;485}486#endif487488static void __init pnv_setup_machdep_opal(void)489{490ppc_md.get_boot_time = opal_get_boot_time;491ppc_md.restart = pnv_restart;492pm_power_off = pnv_power_off;493ppc_md.halt = pnv_halt;494/* ppc_md.system_reset_exception gets filled in by pnv_smp_init() */495ppc_md.machine_check_exception = opal_machine_check;496ppc_md.mce_check_early_recovery = opal_mce_check_early_recovery;497if (opal_check_token(OPAL_HANDLE_HMI2))498ppc_md.hmi_exception_early = opal_hmi_exception_early2;499else500ppc_md.hmi_exception_early = opal_hmi_exception_early;501ppc_md.handle_hmi_exception = opal_handle_hmi_exception;502}503504static int __init pnv_probe(void)505{506if (firmware_has_feature(FW_FEATURE_OPAL))507pnv_setup_machdep_opal();508509pr_debug("PowerNV detected !\n");510511pnv_init();512513return 1;514}515516#ifdef CONFIG_PPC_TRANSACTIONAL_MEM517void __init pnv_tm_init(void)518{519if (!firmware_has_feature(FW_FEATURE_OPAL) ||520!pvr_version_is(PVR_POWER9) ||521early_cpu_has_feature(CPU_FTR_TM))522return;523524if (opal_reinit_cpus(OPAL_REINIT_CPUS_TM_SUSPEND_DISABLED) != OPAL_SUCCESS)525return;526527pr_info("Enabling TM (Transactional Memory) with Suspend Disabled\n");528cur_cpu_spec->cpu_features |= CPU_FTR_TM;529/* Make sure "normal" HTM is off (it should be) */530cur_cpu_spec->cpu_user_features2 &= ~PPC_FEATURE2_HTM;531/* Turn on no suspend mode, and HTM no SC */532cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_HTM_NO_SUSPEND | \533PPC_FEATURE2_HTM_NOSC;534tm_suspend_disabled = true;535}536#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */537538/*539* Returns the cpu frequency for 'cpu' in Hz. This is used by540* /proc/cpuinfo541*/542static unsigned long pnv_get_proc_freq(unsigned int cpu)543{544unsigned long ret_freq;545546ret_freq = cpufreq_get(cpu) * 1000ul;547548/*549* If the backend cpufreq driver does not exist,550* then fallback to old way of reporting the clockrate.551*/552if (!ret_freq)553ret_freq = ppc_proc_freq;554return ret_freq;555}556557static long pnv_machine_check_early(struct pt_regs *regs)558{559long handled = 0;560561if (cur_cpu_spec && cur_cpu_spec->machine_check_early)562handled = cur_cpu_spec->machine_check_early(regs);563564return handled;565}566567define_machine(powernv) {568.name = "PowerNV",569.compatible = "ibm,powernv",570.probe = pnv_probe,571.setup_arch = pnv_setup_arch,572.init_IRQ = pnv_init_IRQ,573.show_cpuinfo = pnv_show_cpuinfo,574.get_proc_freq = pnv_get_proc_freq,575.discover_phbs = pnv_pci_init,576.progress = pnv_progress,577.machine_shutdown = pnv_shutdown,578.power_save = NULL,579.machine_check_early = pnv_machine_check_early,580#ifdef CONFIG_KEXEC_CORE581.kexec_cpu_down = pnv_kexec_cpu_down,582#endif583#ifdef CONFIG_MEMORY_HOTPLUG584.memory_block_size = pnv_memory_block_size,585#endif586};587588589