155190f88SBenjamin Herrenschmidt /* 255190f88SBenjamin Herrenschmidt * PowerNV setup code. 355190f88SBenjamin Herrenschmidt * 455190f88SBenjamin Herrenschmidt * Copyright 2011 IBM Corp. 555190f88SBenjamin Herrenschmidt * 655190f88SBenjamin Herrenschmidt * This program is free software; you can redistribute it and/or 755190f88SBenjamin Herrenschmidt * modify it under the terms of the GNU General Public License 855190f88SBenjamin Herrenschmidt * as published by the Free Software Foundation; either version 955190f88SBenjamin Herrenschmidt * 2 of the License, or (at your option) any later version. 1055190f88SBenjamin Herrenschmidt */ 1155190f88SBenjamin Herrenschmidt 1255190f88SBenjamin Herrenschmidt #undef DEBUG 1355190f88SBenjamin Herrenschmidt 1455190f88SBenjamin Herrenschmidt #include <linux/cpu.h> 1555190f88SBenjamin Herrenschmidt #include <linux/errno.h> 1655190f88SBenjamin Herrenschmidt #include <linux/sched.h> 1755190f88SBenjamin Herrenschmidt #include <linux/kernel.h> 1855190f88SBenjamin Herrenschmidt #include <linux/tty.h> 1955190f88SBenjamin Herrenschmidt #include <linux/reboot.h> 2055190f88SBenjamin Herrenschmidt #include <linux/init.h> 2155190f88SBenjamin Herrenschmidt #include <linux/console.h> 2255190f88SBenjamin Herrenschmidt #include <linux/delay.h> 2355190f88SBenjamin Herrenschmidt #include <linux/irq.h> 2455190f88SBenjamin Herrenschmidt #include <linux/seq_file.h> 2555190f88SBenjamin Herrenschmidt #include <linux/of.h> 2626a2056eSRob Herring #include <linux/of_fdt.h> 2755190f88SBenjamin Herrenschmidt #include <linux/interrupt.h> 2855190f88SBenjamin Herrenschmidt #include <linux/bug.h> 29cd15b048SBenjamin Herrenschmidt #include <linux/pci.h> 30fb5153d0SGautham R. Shenoy #include <linux/cpufreq.h> 3155190f88SBenjamin Herrenschmidt 3255190f88SBenjamin Herrenschmidt #include <asm/machdep.h> 3355190f88SBenjamin Herrenschmidt #include <asm/firmware.h> 3455190f88SBenjamin Herrenschmidt #include <asm/xics.h> 35daea1175SBenjamin Herrenschmidt #include <asm/opal.h> 3613906db6SBenjamin Herrenschmidt #include <asm/kexec.h> 37b2a80878SShreyas B. Prabhu #include <asm/smp.h> 3855190f88SBenjamin Herrenschmidt 3955190f88SBenjamin Herrenschmidt #include "powernv.h" 4055190f88SBenjamin Herrenschmidt 4155190f88SBenjamin Herrenschmidt static void __init pnv_setup_arch(void) 4255190f88SBenjamin Herrenschmidt { 434817fc32SAnton Blanchard set_arch_panic_timeout(10, ARCH_PANIC_TIMEOUT); 444817fc32SAnton Blanchard 4555190f88SBenjamin Herrenschmidt /* Initialize SMP */ 4655190f88SBenjamin Herrenschmidt pnv_smp_init(); 4755190f88SBenjamin Herrenschmidt 4861305a96SBenjamin Herrenschmidt /* Setup PCI */ 4961305a96SBenjamin Herrenschmidt pnv_pci_init(); 5055190f88SBenjamin Herrenschmidt 51628daa8dSBenjamin Herrenschmidt /* Setup RTC and NVRAM callbacks */ 52628daa8dSBenjamin Herrenschmidt if (firmware_has_feature(FW_FEATURE_OPAL)) 53628daa8dSBenjamin Herrenschmidt opal_nvram_init(); 5455190f88SBenjamin Herrenschmidt 5555190f88SBenjamin Herrenschmidt /* Enable NAP mode */ 5655190f88SBenjamin Herrenschmidt powersave_nap = 1; 5755190f88SBenjamin Herrenschmidt 5855190f88SBenjamin Herrenschmidt /* XXX PMCS */ 5955190f88SBenjamin Herrenschmidt } 6055190f88SBenjamin Herrenschmidt 6155190f88SBenjamin Herrenschmidt static void __init pnv_init_early(void) 6255190f88SBenjamin Herrenschmidt { 633fafe9c2SBenjamin Herrenschmidt /* 643fafe9c2SBenjamin Herrenschmidt * Initialize the LPC bus now so that legacy serial 653fafe9c2SBenjamin Herrenschmidt * ports can be found on it 663fafe9c2SBenjamin Herrenschmidt */ 673fafe9c2SBenjamin Herrenschmidt opal_lpc_init(); 683fafe9c2SBenjamin Herrenschmidt 69daea1175SBenjamin Herrenschmidt #ifdef CONFIG_HVC_OPAL 70daea1175SBenjamin Herrenschmidt if (firmware_has_feature(FW_FEATURE_OPAL)) 71daea1175SBenjamin Herrenschmidt hvc_opal_init_early(); 72daea1175SBenjamin Herrenschmidt else 73daea1175SBenjamin Herrenschmidt #endif 74daea1175SBenjamin Herrenschmidt add_preferred_console("hvc", 0, NULL); 7555190f88SBenjamin Herrenschmidt } 7655190f88SBenjamin Herrenschmidt 7755190f88SBenjamin Herrenschmidt static void __init pnv_init_IRQ(void) 7855190f88SBenjamin Herrenschmidt { 7955190f88SBenjamin Herrenschmidt xics_init(); 8055190f88SBenjamin Herrenschmidt 8155190f88SBenjamin Herrenschmidt WARN_ON(!ppc_md.get_irq); 8255190f88SBenjamin Herrenschmidt } 8355190f88SBenjamin Herrenschmidt 8455190f88SBenjamin Herrenschmidt static void pnv_show_cpuinfo(struct seq_file *m) 8555190f88SBenjamin Herrenschmidt { 8655190f88SBenjamin Herrenschmidt struct device_node *root; 8755190f88SBenjamin Herrenschmidt const char *model = ""; 8855190f88SBenjamin Herrenschmidt 8955190f88SBenjamin Herrenschmidt root = of_find_node_by_path("/"); 9055190f88SBenjamin Herrenschmidt if (root) 9155190f88SBenjamin Herrenschmidt model = of_get_property(root, "model", NULL); 9255190f88SBenjamin Herrenschmidt seq_printf(m, "machine\t\t: PowerNV %s\n", model); 93e4d54f71SStewart Smith if (firmware_has_feature(FW_FEATURE_OPAL)) 94e4d54f71SStewart Smith seq_printf(m, "firmware\t: OPAL\n"); 9514a43e69SBenjamin Herrenschmidt else 9614a43e69SBenjamin Herrenschmidt seq_printf(m, "firmware\t: BML\n"); 9755190f88SBenjamin Herrenschmidt of_node_put(root); 9855190f88SBenjamin Herrenschmidt } 9955190f88SBenjamin Herrenschmidt 1002196c6f1SVasant Hegde static void pnv_prepare_going_down(void) 1012196c6f1SVasant Hegde { 1022196c6f1SVasant Hegde /* 1032196c6f1SVasant Hegde * Disable all notifiers from OPAL, we can't 1042196c6f1SVasant Hegde * service interrupts anymore anyway 1052196c6f1SVasant Hegde */ 10681f2f7ceSAlistair Popple opal_event_shutdown(); 1072196c6f1SVasant Hegde 1082196c6f1SVasant Hegde /* Soft disable interrupts */ 1092196c6f1SVasant Hegde local_irq_disable(); 1102196c6f1SVasant Hegde 1112196c6f1SVasant Hegde /* 1122196c6f1SVasant Hegde * Return secondary CPUs to firwmare if a flash update 1132196c6f1SVasant Hegde * is pending otherwise we will get all sort of error 1142196c6f1SVasant Hegde * messages about CPU being stuck etc.. This will also 1152196c6f1SVasant Hegde * have the side effect of hard disabling interrupts so 1162196c6f1SVasant Hegde * past this point, the kernel is effectively dead. 1172196c6f1SVasant Hegde */ 1182196c6f1SVasant Hegde opal_flash_term_callback(); 1192196c6f1SVasant Hegde } 1202196c6f1SVasant Hegde 121ec27329fSBenjamin Herrenschmidt static void __noreturn pnv_restart(char *cmd) 12255190f88SBenjamin Herrenschmidt { 123ec27329fSBenjamin Herrenschmidt long rc = OPAL_BUSY; 124ec27329fSBenjamin Herrenschmidt 1252196c6f1SVasant Hegde pnv_prepare_going_down(); 126e8e71fa4SGavin Shan 127ec27329fSBenjamin Herrenschmidt while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { 128ec27329fSBenjamin Herrenschmidt rc = opal_cec_reboot(); 129ec27329fSBenjamin Herrenschmidt if (rc == OPAL_BUSY_EVENT) 130ec27329fSBenjamin Herrenschmidt opal_poll_events(NULL); 131ec27329fSBenjamin Herrenschmidt else 132ec27329fSBenjamin Herrenschmidt mdelay(10); 133ec27329fSBenjamin Herrenschmidt } 134ec27329fSBenjamin Herrenschmidt for (;;) 135ec27329fSBenjamin Herrenschmidt opal_poll_events(NULL); 13655190f88SBenjamin Herrenschmidt } 13755190f88SBenjamin Herrenschmidt 138ec27329fSBenjamin Herrenschmidt static void __noreturn pnv_power_off(void) 13955190f88SBenjamin Herrenschmidt { 140ec27329fSBenjamin Herrenschmidt long rc = OPAL_BUSY; 141ec27329fSBenjamin Herrenschmidt 1422196c6f1SVasant Hegde pnv_prepare_going_down(); 143e8e71fa4SGavin Shan 144ec27329fSBenjamin Herrenschmidt while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { 145ec27329fSBenjamin Herrenschmidt rc = opal_cec_power_down(0); 146ec27329fSBenjamin Herrenschmidt if (rc == OPAL_BUSY_EVENT) 147ec27329fSBenjamin Herrenschmidt opal_poll_events(NULL); 148ec27329fSBenjamin Herrenschmidt else 149ec27329fSBenjamin Herrenschmidt mdelay(10); 150ec27329fSBenjamin Herrenschmidt } 151ec27329fSBenjamin Herrenschmidt for (;;) 152ec27329fSBenjamin Herrenschmidt opal_poll_events(NULL); 15355190f88SBenjamin Herrenschmidt } 15455190f88SBenjamin Herrenschmidt 155ec27329fSBenjamin Herrenschmidt static void __noreturn pnv_halt(void) 15655190f88SBenjamin Herrenschmidt { 157ec27329fSBenjamin Herrenschmidt pnv_power_off(); 15855190f88SBenjamin Herrenschmidt } 15955190f88SBenjamin Herrenschmidt 16055190f88SBenjamin Herrenschmidt static void pnv_progress(char *s, unsigned short hex) 16155190f88SBenjamin Herrenschmidt { 16255190f88SBenjamin Herrenschmidt } 16355190f88SBenjamin Herrenschmidt 16473ed148aSBenjamin Herrenschmidt static void pnv_shutdown(void) 16573ed148aSBenjamin Herrenschmidt { 16673ed148aSBenjamin Herrenschmidt /* Let the PCI code clear up IODA tables */ 16773ed148aSBenjamin Herrenschmidt pnv_pci_shutdown(); 16873ed148aSBenjamin Herrenschmidt 169f7d98d18SVasant Hegde /* 170f7d98d18SVasant Hegde * Stop OPAL activity: Unregister all OPAL interrupts so they 171f7d98d18SVasant Hegde * don't fire up while we kexec and make sure all potentially 172f7d98d18SVasant Hegde * DMA'ing ops are complete (such as dump retrieval). 17373ed148aSBenjamin Herrenschmidt */ 17473ed148aSBenjamin Herrenschmidt opal_shutdown(); 17573ed148aSBenjamin Herrenschmidt } 17673ed148aSBenjamin Herrenschmidt 17755190f88SBenjamin Herrenschmidt #ifdef CONFIG_KEXEC 178298b34d7SBenjamin Herrenschmidt static void pnv_kexec_wait_secondaries_down(void) 179298b34d7SBenjamin Herrenschmidt { 180298b34d7SBenjamin Herrenschmidt int my_cpu, i, notified = -1; 181298b34d7SBenjamin Herrenschmidt 182298b34d7SBenjamin Herrenschmidt my_cpu = get_cpu(); 183298b34d7SBenjamin Herrenschmidt 184298b34d7SBenjamin Herrenschmidt for_each_online_cpu(i) { 185298b34d7SBenjamin Herrenschmidt uint8_t status; 1861b70386cSSamuel Mendoza-Jonas int64_t rc, timeout = 1000; 187298b34d7SBenjamin Herrenschmidt 188298b34d7SBenjamin Herrenschmidt if (i == my_cpu) 189298b34d7SBenjamin Herrenschmidt continue; 190298b34d7SBenjamin Herrenschmidt 191298b34d7SBenjamin Herrenschmidt for (;;) { 192298b34d7SBenjamin Herrenschmidt rc = opal_query_cpu_status(get_hard_smp_processor_id(i), 193298b34d7SBenjamin Herrenschmidt &status); 194298b34d7SBenjamin Herrenschmidt if (rc != OPAL_SUCCESS || status != OPAL_THREAD_STARTED) 195298b34d7SBenjamin Herrenschmidt break; 196298b34d7SBenjamin Herrenschmidt barrier(); 197298b34d7SBenjamin Herrenschmidt if (i != notified) { 198298b34d7SBenjamin Herrenschmidt printk(KERN_INFO "kexec: waiting for cpu %d " 199298b34d7SBenjamin Herrenschmidt "(physical %d) to enter OPAL\n", 200298b34d7SBenjamin Herrenschmidt i, paca[i].hw_cpu_id); 201298b34d7SBenjamin Herrenschmidt notified = i; 202298b34d7SBenjamin Herrenschmidt } 2031b70386cSSamuel Mendoza-Jonas 2041b70386cSSamuel Mendoza-Jonas /* 2051b70386cSSamuel Mendoza-Jonas * On crash secondaries might be unreachable or hung, 2061b70386cSSamuel Mendoza-Jonas * so timeout if we've waited too long 2071b70386cSSamuel Mendoza-Jonas * */ 2081b70386cSSamuel Mendoza-Jonas mdelay(1); 2091b70386cSSamuel Mendoza-Jonas if (timeout-- == 0) { 2101b70386cSSamuel Mendoza-Jonas printk(KERN_ERR "kexec: timed out waiting for " 2111b70386cSSamuel Mendoza-Jonas "cpu %d (physical %d) to enter OPAL\n", 2121b70386cSSamuel Mendoza-Jonas i, paca[i].hw_cpu_id); 2131b70386cSSamuel Mendoza-Jonas break; 2141b70386cSSamuel Mendoza-Jonas } 215298b34d7SBenjamin Herrenschmidt } 216298b34d7SBenjamin Herrenschmidt } 217298b34d7SBenjamin Herrenschmidt } 218298b34d7SBenjamin Herrenschmidt 21955190f88SBenjamin Herrenschmidt static void pnv_kexec_cpu_down(int crash_shutdown, int secondary) 22055190f88SBenjamin Herrenschmidt { 22155190f88SBenjamin Herrenschmidt xics_kexec_teardown_cpu(secondary); 22213906db6SBenjamin Herrenschmidt 223e4d54f71SStewart Smith /* On OPAL, we return all CPUs to firmware */ 224298b34d7SBenjamin Herrenschmidt 225e4d54f71SStewart Smith if (!firmware_has_feature(FW_FEATURE_OPAL)) 226298b34d7SBenjamin Herrenschmidt return; 227298b34d7SBenjamin Herrenschmidt 228298b34d7SBenjamin Herrenschmidt if (secondary) { 22913906db6SBenjamin Herrenschmidt /* Return secondary CPUs to firmware on OPAL v3 */ 23013906db6SBenjamin Herrenschmidt mb(); 23113906db6SBenjamin Herrenschmidt get_paca()->kexec_state = KEXEC_STATE_REAL_MODE; 23213906db6SBenjamin Herrenschmidt mb(); 23313906db6SBenjamin Herrenschmidt 23413906db6SBenjamin Herrenschmidt /* Return the CPU to OPAL */ 23513906db6SBenjamin Herrenschmidt opal_return_cpu(); 236298b34d7SBenjamin Herrenschmidt } else { 237298b34d7SBenjamin Herrenschmidt /* Primary waits for the secondaries to have reached OPAL */ 238298b34d7SBenjamin Herrenschmidt pnv_kexec_wait_secondaries_down(); 239e72bb8a5SSamuel Mendoza-Jonas 240e72bb8a5SSamuel Mendoza-Jonas /* 241e72bb8a5SSamuel Mendoza-Jonas * We might be running as little-endian - now that interrupts 242e72bb8a5SSamuel Mendoza-Jonas * are disabled, reset the HILE bit to big-endian so we don't 243e72bb8a5SSamuel Mendoza-Jonas * take interrupts in the wrong endian later 244e72bb8a5SSamuel Mendoza-Jonas */ 245e72bb8a5SSamuel Mendoza-Jonas opal_reinit_cpus(OPAL_REINIT_CPUS_HILE_BE); 24613906db6SBenjamin Herrenschmidt } 24755190f88SBenjamin Herrenschmidt } 24855190f88SBenjamin Herrenschmidt #endif /* CONFIG_KEXEC */ 24955190f88SBenjamin Herrenschmidt 2506d97d7a2SAnton Blanchard #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE 2516d97d7a2SAnton Blanchard static unsigned long pnv_memory_block_size(void) 2526d97d7a2SAnton Blanchard { 2536d97d7a2SAnton Blanchard return 256UL * 1024 * 1024; 2546d97d7a2SAnton Blanchard } 2556d97d7a2SAnton Blanchard #endif 2566d97d7a2SAnton Blanchard 257628daa8dSBenjamin Herrenschmidt static void __init pnv_setup_machdep_opal(void) 258628daa8dSBenjamin Herrenschmidt { 259628daa8dSBenjamin Herrenschmidt ppc_md.get_boot_time = opal_get_boot_time; 260628daa8dSBenjamin Herrenschmidt ppc_md.restart = pnv_restart; 2619178ba29SAlexander Graf pm_power_off = pnv_power_off; 262628daa8dSBenjamin Herrenschmidt ppc_md.halt = pnv_halt; 263ed79ba9eSBenjamin Herrenschmidt ppc_md.machine_check_exception = opal_machine_check; 26455672ecfSMahesh Salgaonkar ppc_md.mce_check_early_recovery = opal_mce_check_early_recovery; 2650869b6fdSMahesh Salgaonkar ppc_md.hmi_exception_early = opal_hmi_exception_early; 2660869b6fdSMahesh Salgaonkar ppc_md.handle_hmi_exception = opal_handle_hmi_exception; 267628daa8dSBenjamin Herrenschmidt } 268628daa8dSBenjamin Herrenschmidt 26955190f88SBenjamin Herrenschmidt static int __init pnv_probe(void) 27055190f88SBenjamin Herrenschmidt { 271*406b0b6aSBenjamin Herrenschmidt if (!of_machine_is_compatible("ibm,powernv")) 27255190f88SBenjamin Herrenschmidt return 0; 27355190f88SBenjamin Herrenschmidt 274628daa8dSBenjamin Herrenschmidt if (firmware_has_feature(FW_FEATURE_OPAL)) 275628daa8dSBenjamin Herrenschmidt pnv_setup_machdep_opal(); 276628daa8dSBenjamin Herrenschmidt 27755190f88SBenjamin Herrenschmidt pr_debug("PowerNV detected !\n"); 27855190f88SBenjamin Herrenschmidt 27955190f88SBenjamin Herrenschmidt return 1; 28055190f88SBenjamin Herrenschmidt } 28155190f88SBenjamin Herrenschmidt 282fb5153d0SGautham R. Shenoy /* 283fb5153d0SGautham R. Shenoy * Returns the cpu frequency for 'cpu' in Hz. This is used by 284fb5153d0SGautham R. Shenoy * /proc/cpuinfo 285fb5153d0SGautham R. Shenoy */ 286e51df2c1SAnton Blanchard static unsigned long pnv_get_proc_freq(unsigned int cpu) 287fb5153d0SGautham R. Shenoy { 288fb5153d0SGautham R. Shenoy unsigned long ret_freq; 289fb5153d0SGautham R. Shenoy 290fb5153d0SGautham R. Shenoy ret_freq = cpufreq_quick_get(cpu) * 1000ul; 291fb5153d0SGautham R. Shenoy 292fb5153d0SGautham R. Shenoy /* 293fb5153d0SGautham R. Shenoy * If the backend cpufreq driver does not exist, 294fb5153d0SGautham R. Shenoy * then fallback to old way of reporting the clockrate. 295fb5153d0SGautham R. Shenoy */ 296fb5153d0SGautham R. Shenoy if (!ret_freq) 297fb5153d0SGautham R. Shenoy ret_freq = ppc_proc_freq; 298fb5153d0SGautham R. Shenoy return ret_freq; 299fb5153d0SGautham R. Shenoy } 300fb5153d0SGautham R. Shenoy 30155190f88SBenjamin Herrenschmidt define_machine(powernv) { 30255190f88SBenjamin Herrenschmidt .name = "PowerNV", 30355190f88SBenjamin Herrenschmidt .probe = pnv_probe, 30455190f88SBenjamin Herrenschmidt .init_early = pnv_init_early, 305628daa8dSBenjamin Herrenschmidt .setup_arch = pnv_setup_arch, 30655190f88SBenjamin Herrenschmidt .init_IRQ = pnv_init_IRQ, 30755190f88SBenjamin Herrenschmidt .show_cpuinfo = pnv_show_cpuinfo, 308fb5153d0SGautham R. Shenoy .get_proc_freq = pnv_get_proc_freq, 30955190f88SBenjamin Herrenschmidt .progress = pnv_progress, 31073ed148aSBenjamin Herrenschmidt .machine_shutdown = pnv_shutdown, 3115593e303SShreyas B. Prabhu .power_save = NULL, 31255190f88SBenjamin Herrenschmidt .calibrate_decr = generic_calibrate_decr, 31355190f88SBenjamin Herrenschmidt #ifdef CONFIG_KEXEC 31455190f88SBenjamin Herrenschmidt .kexec_cpu_down = pnv_kexec_cpu_down, 31555190f88SBenjamin Herrenschmidt #endif 3166d97d7a2SAnton Blanchard #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE 3176d97d7a2SAnton Blanchard .memory_block_size = pnv_memory_block_size, 3186d97d7a2SAnton Blanchard #endif 31955190f88SBenjamin Herrenschmidt }; 320