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> 30*fb5153d0SGautham 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> 35628daa8dSBenjamin Herrenschmidt #include <asm/rtas.h> 36daea1175SBenjamin Herrenschmidt #include <asm/opal.h> 3713906db6SBenjamin Herrenschmidt #include <asm/kexec.h> 3855190f88SBenjamin Herrenschmidt 3955190f88SBenjamin Herrenschmidt #include "powernv.h" 4055190f88SBenjamin Herrenschmidt 4155190f88SBenjamin Herrenschmidt static void __init pnv_setup_arch(void) 4255190f88SBenjamin Herrenschmidt { 4355190f88SBenjamin Herrenschmidt /* Initialize SMP */ 4455190f88SBenjamin Herrenschmidt pnv_smp_init(); 4555190f88SBenjamin Herrenschmidt 4661305a96SBenjamin Herrenschmidt /* Setup PCI */ 4761305a96SBenjamin Herrenschmidt pnv_pci_init(); 4855190f88SBenjamin Herrenschmidt 49628daa8dSBenjamin Herrenschmidt /* Setup RTC and NVRAM callbacks */ 50628daa8dSBenjamin Herrenschmidt if (firmware_has_feature(FW_FEATURE_OPAL)) 51628daa8dSBenjamin Herrenschmidt opal_nvram_init(); 5255190f88SBenjamin Herrenschmidt 5355190f88SBenjamin Herrenschmidt /* Enable NAP mode */ 5455190f88SBenjamin Herrenschmidt powersave_nap = 1; 5555190f88SBenjamin Herrenschmidt 5655190f88SBenjamin Herrenschmidt /* XXX PMCS */ 5755190f88SBenjamin Herrenschmidt } 5855190f88SBenjamin Herrenschmidt 5955190f88SBenjamin Herrenschmidt static void __init pnv_init_early(void) 6055190f88SBenjamin Herrenschmidt { 613fafe9c2SBenjamin Herrenschmidt /* 623fafe9c2SBenjamin Herrenschmidt * Initialize the LPC bus now so that legacy serial 633fafe9c2SBenjamin Herrenschmidt * ports can be found on it 643fafe9c2SBenjamin Herrenschmidt */ 653fafe9c2SBenjamin Herrenschmidt opal_lpc_init(); 663fafe9c2SBenjamin Herrenschmidt 67daea1175SBenjamin Herrenschmidt #ifdef CONFIG_HVC_OPAL 68daea1175SBenjamin Herrenschmidt if (firmware_has_feature(FW_FEATURE_OPAL)) 69daea1175SBenjamin Herrenschmidt hvc_opal_init_early(); 70daea1175SBenjamin Herrenschmidt else 71daea1175SBenjamin Herrenschmidt #endif 72daea1175SBenjamin Herrenschmidt add_preferred_console("hvc", 0, NULL); 7355190f88SBenjamin Herrenschmidt } 7455190f88SBenjamin Herrenschmidt 7555190f88SBenjamin Herrenschmidt static void __init pnv_init_IRQ(void) 7655190f88SBenjamin Herrenschmidt { 7755190f88SBenjamin Herrenschmidt xics_init(); 7855190f88SBenjamin Herrenschmidt 7955190f88SBenjamin Herrenschmidt WARN_ON(!ppc_md.get_irq); 8055190f88SBenjamin Herrenschmidt } 8155190f88SBenjamin Herrenschmidt 8255190f88SBenjamin Herrenschmidt static void pnv_show_cpuinfo(struct seq_file *m) 8355190f88SBenjamin Herrenschmidt { 8455190f88SBenjamin Herrenschmidt struct device_node *root; 8555190f88SBenjamin Herrenschmidt const char *model = ""; 8655190f88SBenjamin Herrenschmidt 8755190f88SBenjamin Herrenschmidt root = of_find_node_by_path("/"); 8855190f88SBenjamin Herrenschmidt if (root) 8955190f88SBenjamin Herrenschmidt model = of_get_property(root, "model", NULL); 9055190f88SBenjamin Herrenschmidt seq_printf(m, "machine\t\t: PowerNV %s\n", model); 9175b93da4SBenjamin Herrenschmidt if (firmware_has_feature(FW_FEATURE_OPALv3)) 9275b93da4SBenjamin Herrenschmidt seq_printf(m, "firmware\t: OPAL v3\n"); 9375b93da4SBenjamin Herrenschmidt else if (firmware_has_feature(FW_FEATURE_OPALv2)) 9414a43e69SBenjamin Herrenschmidt seq_printf(m, "firmware\t: OPAL v2\n"); 9514a43e69SBenjamin Herrenschmidt else if (firmware_has_feature(FW_FEATURE_OPAL)) 9614a43e69SBenjamin Herrenschmidt seq_printf(m, "firmware\t: OPAL v1\n"); 9714a43e69SBenjamin Herrenschmidt else 9814a43e69SBenjamin Herrenschmidt seq_printf(m, "firmware\t: BML\n"); 9955190f88SBenjamin Herrenschmidt of_node_put(root); 10055190f88SBenjamin Herrenschmidt } 10155190f88SBenjamin Herrenschmidt 1022196c6f1SVasant Hegde static void pnv_prepare_going_down(void) 1032196c6f1SVasant Hegde { 1042196c6f1SVasant Hegde /* 1052196c6f1SVasant Hegde * Disable all notifiers from OPAL, we can't 1062196c6f1SVasant Hegde * service interrupts anymore anyway 1072196c6f1SVasant Hegde */ 1082196c6f1SVasant Hegde opal_notifier_disable(); 1092196c6f1SVasant Hegde 1102196c6f1SVasant Hegde /* Soft disable interrupts */ 1112196c6f1SVasant Hegde local_irq_disable(); 1122196c6f1SVasant Hegde 1132196c6f1SVasant Hegde /* 1142196c6f1SVasant Hegde * Return secondary CPUs to firwmare if a flash update 1152196c6f1SVasant Hegde * is pending otherwise we will get all sort of error 1162196c6f1SVasant Hegde * messages about CPU being stuck etc.. This will also 1172196c6f1SVasant Hegde * have the side effect of hard disabling interrupts so 1182196c6f1SVasant Hegde * past this point, the kernel is effectively dead. 1192196c6f1SVasant Hegde */ 1202196c6f1SVasant Hegde opal_flash_term_callback(); 1212196c6f1SVasant Hegde } 1222196c6f1SVasant Hegde 123ec27329fSBenjamin Herrenschmidt static void __noreturn pnv_restart(char *cmd) 12455190f88SBenjamin Herrenschmidt { 125ec27329fSBenjamin Herrenschmidt long rc = OPAL_BUSY; 126ec27329fSBenjamin Herrenschmidt 1272196c6f1SVasant Hegde pnv_prepare_going_down(); 128e8e71fa4SGavin Shan 129ec27329fSBenjamin Herrenschmidt while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { 130ec27329fSBenjamin Herrenschmidt rc = opal_cec_reboot(); 131ec27329fSBenjamin Herrenschmidt if (rc == OPAL_BUSY_EVENT) 132ec27329fSBenjamin Herrenschmidt opal_poll_events(NULL); 133ec27329fSBenjamin Herrenschmidt else 134ec27329fSBenjamin Herrenschmidt mdelay(10); 135ec27329fSBenjamin Herrenschmidt } 136ec27329fSBenjamin Herrenschmidt for (;;) 137ec27329fSBenjamin Herrenschmidt opal_poll_events(NULL); 13855190f88SBenjamin Herrenschmidt } 13955190f88SBenjamin Herrenschmidt 140ec27329fSBenjamin Herrenschmidt static void __noreturn pnv_power_off(void) 14155190f88SBenjamin Herrenschmidt { 142ec27329fSBenjamin Herrenschmidt long rc = OPAL_BUSY; 143ec27329fSBenjamin Herrenschmidt 1442196c6f1SVasant Hegde pnv_prepare_going_down(); 145e8e71fa4SGavin Shan 146ec27329fSBenjamin Herrenschmidt while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { 147ec27329fSBenjamin Herrenschmidt rc = opal_cec_power_down(0); 148ec27329fSBenjamin Herrenschmidt if (rc == OPAL_BUSY_EVENT) 149ec27329fSBenjamin Herrenschmidt opal_poll_events(NULL); 150ec27329fSBenjamin Herrenschmidt else 151ec27329fSBenjamin Herrenschmidt mdelay(10); 152ec27329fSBenjamin Herrenschmidt } 153ec27329fSBenjamin Herrenschmidt for (;;) 154ec27329fSBenjamin Herrenschmidt opal_poll_events(NULL); 15555190f88SBenjamin Herrenschmidt } 15655190f88SBenjamin Herrenschmidt 157ec27329fSBenjamin Herrenschmidt static void __noreturn pnv_halt(void) 15855190f88SBenjamin Herrenschmidt { 159ec27329fSBenjamin Herrenschmidt pnv_power_off(); 16055190f88SBenjamin Herrenschmidt } 16155190f88SBenjamin Herrenschmidt 16255190f88SBenjamin Herrenschmidt static void pnv_progress(char *s, unsigned short hex) 16355190f88SBenjamin Herrenschmidt { 16455190f88SBenjamin Herrenschmidt } 16555190f88SBenjamin Herrenschmidt 166cd15b048SBenjamin Herrenschmidt static int pnv_dma_set_mask(struct device *dev, u64 dma_mask) 167cd15b048SBenjamin Herrenschmidt { 168cd15b048SBenjamin Herrenschmidt if (dev_is_pci(dev)) 169cd15b048SBenjamin Herrenschmidt return pnv_pci_dma_set_mask(to_pci_dev(dev), dma_mask); 170cd15b048SBenjamin Herrenschmidt return __dma_set_mask(dev, dma_mask); 171cd15b048SBenjamin Herrenschmidt } 172cd15b048SBenjamin Herrenschmidt 17373ed148aSBenjamin Herrenschmidt static void pnv_shutdown(void) 17473ed148aSBenjamin Herrenschmidt { 17573ed148aSBenjamin Herrenschmidt /* Let the PCI code clear up IODA tables */ 17673ed148aSBenjamin Herrenschmidt pnv_pci_shutdown(); 17773ed148aSBenjamin Herrenschmidt 178f7d98d18SVasant Hegde /* 179f7d98d18SVasant Hegde * Stop OPAL activity: Unregister all OPAL interrupts so they 180f7d98d18SVasant Hegde * don't fire up while we kexec and make sure all potentially 181f7d98d18SVasant Hegde * DMA'ing ops are complete (such as dump retrieval). 18273ed148aSBenjamin Herrenschmidt */ 18373ed148aSBenjamin Herrenschmidt opal_shutdown(); 18473ed148aSBenjamin Herrenschmidt } 18573ed148aSBenjamin Herrenschmidt 18655190f88SBenjamin Herrenschmidt #ifdef CONFIG_KEXEC 187298b34d7SBenjamin Herrenschmidt static void pnv_kexec_wait_secondaries_down(void) 188298b34d7SBenjamin Herrenschmidt { 189298b34d7SBenjamin Herrenschmidt int my_cpu, i, notified = -1; 190298b34d7SBenjamin Herrenschmidt 191298b34d7SBenjamin Herrenschmidt my_cpu = get_cpu(); 192298b34d7SBenjamin Herrenschmidt 193298b34d7SBenjamin Herrenschmidt for_each_online_cpu(i) { 194298b34d7SBenjamin Herrenschmidt uint8_t status; 195298b34d7SBenjamin Herrenschmidt int64_t rc; 196298b34d7SBenjamin Herrenschmidt 197298b34d7SBenjamin Herrenschmidt if (i == my_cpu) 198298b34d7SBenjamin Herrenschmidt continue; 199298b34d7SBenjamin Herrenschmidt 200298b34d7SBenjamin Herrenschmidt for (;;) { 201298b34d7SBenjamin Herrenschmidt rc = opal_query_cpu_status(get_hard_smp_processor_id(i), 202298b34d7SBenjamin Herrenschmidt &status); 203298b34d7SBenjamin Herrenschmidt if (rc != OPAL_SUCCESS || status != OPAL_THREAD_STARTED) 204298b34d7SBenjamin Herrenschmidt break; 205298b34d7SBenjamin Herrenschmidt barrier(); 206298b34d7SBenjamin Herrenschmidt if (i != notified) { 207298b34d7SBenjamin Herrenschmidt printk(KERN_INFO "kexec: waiting for cpu %d " 208298b34d7SBenjamin Herrenschmidt "(physical %d) to enter OPAL\n", 209298b34d7SBenjamin Herrenschmidt i, paca[i].hw_cpu_id); 210298b34d7SBenjamin Herrenschmidt notified = i; 211298b34d7SBenjamin Herrenschmidt } 212298b34d7SBenjamin Herrenschmidt } 213298b34d7SBenjamin Herrenschmidt } 214298b34d7SBenjamin Herrenschmidt } 215298b34d7SBenjamin Herrenschmidt 21655190f88SBenjamin Herrenschmidt static void pnv_kexec_cpu_down(int crash_shutdown, int secondary) 21755190f88SBenjamin Herrenschmidt { 21855190f88SBenjamin Herrenschmidt xics_kexec_teardown_cpu(secondary); 21913906db6SBenjamin Herrenschmidt 220298b34d7SBenjamin Herrenschmidt /* On OPAL v3, we return all CPUs to firmware */ 221298b34d7SBenjamin Herrenschmidt 222298b34d7SBenjamin Herrenschmidt if (!firmware_has_feature(FW_FEATURE_OPALv3)) 223298b34d7SBenjamin Herrenschmidt return; 224298b34d7SBenjamin Herrenschmidt 225298b34d7SBenjamin Herrenschmidt if (secondary) { 22613906db6SBenjamin Herrenschmidt /* Return secondary CPUs to firmware on OPAL v3 */ 22713906db6SBenjamin Herrenschmidt mb(); 22813906db6SBenjamin Herrenschmidt get_paca()->kexec_state = KEXEC_STATE_REAL_MODE; 22913906db6SBenjamin Herrenschmidt mb(); 23013906db6SBenjamin Herrenschmidt 23113906db6SBenjamin Herrenschmidt /* Return the CPU to OPAL */ 23213906db6SBenjamin Herrenschmidt opal_return_cpu(); 233298b34d7SBenjamin Herrenschmidt } else if (crash_shutdown) { 234298b34d7SBenjamin Herrenschmidt /* 235298b34d7SBenjamin Herrenschmidt * On crash, we don't wait for secondaries to go 236298b34d7SBenjamin Herrenschmidt * down as they might be unreachable or hung, so 237298b34d7SBenjamin Herrenschmidt * instead we just wait a bit and move on. 238298b34d7SBenjamin Herrenschmidt */ 239298b34d7SBenjamin Herrenschmidt mdelay(1); 240298b34d7SBenjamin Herrenschmidt } else { 241298b34d7SBenjamin Herrenschmidt /* Primary waits for the secondaries to have reached OPAL */ 242298b34d7SBenjamin Herrenschmidt pnv_kexec_wait_secondaries_down(); 24313906db6SBenjamin Herrenschmidt } 24455190f88SBenjamin Herrenschmidt } 24555190f88SBenjamin Herrenschmidt #endif /* CONFIG_KEXEC */ 24655190f88SBenjamin Herrenschmidt 247628daa8dSBenjamin Herrenschmidt static void __init pnv_setup_machdep_opal(void) 248628daa8dSBenjamin Herrenschmidt { 249628daa8dSBenjamin Herrenschmidt ppc_md.get_boot_time = opal_get_boot_time; 250628daa8dSBenjamin Herrenschmidt ppc_md.get_rtc_time = opal_get_rtc_time; 251628daa8dSBenjamin Herrenschmidt ppc_md.set_rtc_time = opal_set_rtc_time; 252628daa8dSBenjamin Herrenschmidt ppc_md.restart = pnv_restart; 253628daa8dSBenjamin Herrenschmidt ppc_md.power_off = pnv_power_off; 254628daa8dSBenjamin Herrenschmidt ppc_md.halt = pnv_halt; 255ed79ba9eSBenjamin Herrenschmidt ppc_md.machine_check_exception = opal_machine_check; 25655672ecfSMahesh Salgaonkar ppc_md.mce_check_early_recovery = opal_mce_check_early_recovery; 257628daa8dSBenjamin Herrenschmidt } 258628daa8dSBenjamin Herrenschmidt 259628daa8dSBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV_RTAS 260628daa8dSBenjamin Herrenschmidt static void __init pnv_setup_machdep_rtas(void) 261628daa8dSBenjamin Herrenschmidt { 262628daa8dSBenjamin Herrenschmidt if (rtas_token("get-time-of-day") != RTAS_UNKNOWN_SERVICE) { 263628daa8dSBenjamin Herrenschmidt ppc_md.get_boot_time = rtas_get_boot_time; 264628daa8dSBenjamin Herrenschmidt ppc_md.get_rtc_time = rtas_get_rtc_time; 265628daa8dSBenjamin Herrenschmidt ppc_md.set_rtc_time = rtas_set_rtc_time; 266628daa8dSBenjamin Herrenschmidt } 267628daa8dSBenjamin Herrenschmidt ppc_md.restart = rtas_restart; 268628daa8dSBenjamin Herrenschmidt ppc_md.power_off = rtas_power_off; 269628daa8dSBenjamin Herrenschmidt ppc_md.halt = rtas_halt; 270628daa8dSBenjamin Herrenschmidt } 271628daa8dSBenjamin Herrenschmidt #endif /* CONFIG_PPC_POWERNV_RTAS */ 272628daa8dSBenjamin Herrenschmidt 27355190f88SBenjamin Herrenschmidt static int __init pnv_probe(void) 27455190f88SBenjamin Herrenschmidt { 27555190f88SBenjamin Herrenschmidt unsigned long root = of_get_flat_dt_root(); 27655190f88SBenjamin Herrenschmidt 27755190f88SBenjamin Herrenschmidt if (!of_flat_dt_is_compatible(root, "ibm,powernv")) 27855190f88SBenjamin Herrenschmidt return 0; 27955190f88SBenjamin Herrenschmidt 28055190f88SBenjamin Herrenschmidt hpte_init_native(); 28155190f88SBenjamin Herrenschmidt 282628daa8dSBenjamin Herrenschmidt if (firmware_has_feature(FW_FEATURE_OPAL)) 283628daa8dSBenjamin Herrenschmidt pnv_setup_machdep_opal(); 284628daa8dSBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV_RTAS 285628daa8dSBenjamin Herrenschmidt else if (rtas.base) 286628daa8dSBenjamin Herrenschmidt pnv_setup_machdep_rtas(); 287628daa8dSBenjamin Herrenschmidt #endif /* CONFIG_PPC_POWERNV_RTAS */ 288628daa8dSBenjamin Herrenschmidt 28955190f88SBenjamin Herrenschmidt pr_debug("PowerNV detected !\n"); 29055190f88SBenjamin Herrenschmidt 29155190f88SBenjamin Herrenschmidt return 1; 29255190f88SBenjamin Herrenschmidt } 29355190f88SBenjamin Herrenschmidt 294*fb5153d0SGautham R. Shenoy /* 295*fb5153d0SGautham R. Shenoy * Returns the cpu frequency for 'cpu' in Hz. This is used by 296*fb5153d0SGautham R. Shenoy * /proc/cpuinfo 297*fb5153d0SGautham R. Shenoy */ 298*fb5153d0SGautham R. Shenoy unsigned long pnv_get_proc_freq(unsigned int cpu) 299*fb5153d0SGautham R. Shenoy { 300*fb5153d0SGautham R. Shenoy unsigned long ret_freq; 301*fb5153d0SGautham R. Shenoy 302*fb5153d0SGautham R. Shenoy ret_freq = cpufreq_quick_get(cpu) * 1000ul; 303*fb5153d0SGautham R. Shenoy 304*fb5153d0SGautham R. Shenoy /* 305*fb5153d0SGautham R. Shenoy * If the backend cpufreq driver does not exist, 306*fb5153d0SGautham R. Shenoy * then fallback to old way of reporting the clockrate. 307*fb5153d0SGautham R. Shenoy */ 308*fb5153d0SGautham R. Shenoy if (!ret_freq) 309*fb5153d0SGautham R. Shenoy ret_freq = ppc_proc_freq; 310*fb5153d0SGautham R. Shenoy return ret_freq; 311*fb5153d0SGautham R. Shenoy } 312*fb5153d0SGautham R. Shenoy 31355190f88SBenjamin Herrenschmidt define_machine(powernv) { 31455190f88SBenjamin Herrenschmidt .name = "PowerNV", 31555190f88SBenjamin Herrenschmidt .probe = pnv_probe, 31655190f88SBenjamin Herrenschmidt .init_early = pnv_init_early, 317628daa8dSBenjamin Herrenschmidt .setup_arch = pnv_setup_arch, 31855190f88SBenjamin Herrenschmidt .init_IRQ = pnv_init_IRQ, 31955190f88SBenjamin Herrenschmidt .show_cpuinfo = pnv_show_cpuinfo, 320*fb5153d0SGautham R. Shenoy .get_proc_freq = pnv_get_proc_freq, 32155190f88SBenjamin Herrenschmidt .progress = pnv_progress, 32273ed148aSBenjamin Herrenschmidt .machine_shutdown = pnv_shutdown, 323591ac0cbSNicolas Pitre .power_save = power7_idle, 32455190f88SBenjamin Herrenschmidt .calibrate_decr = generic_calibrate_decr, 325cd15b048SBenjamin Herrenschmidt .dma_set_mask = pnv_dma_set_mask, 32655190f88SBenjamin Herrenschmidt #ifdef CONFIG_KEXEC 32755190f88SBenjamin Herrenschmidt .kexec_cpu_down = pnv_kexec_cpu_down, 32855190f88SBenjamin Herrenschmidt #endif 32955190f88SBenjamin Herrenschmidt }; 330