10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #define PSMI_1_5 300Sstevel@tonic-gate #include <sys/smp_impldefs.h> 310Sstevel@tonic-gate #include <sys/psm.h> 320Sstevel@tonic-gate #include <sys/psm_modctl.h> 330Sstevel@tonic-gate #include <sys/pit.h> 340Sstevel@tonic-gate #include <sys/cmn_err.h> 350Sstevel@tonic-gate #include <sys/strlog.h> 360Sstevel@tonic-gate #include <sys/clock.h> 370Sstevel@tonic-gate #include <sys/debug.h> 380Sstevel@tonic-gate #include <sys/rtc.h> 390Sstevel@tonic-gate #include <sys/x86_archext.h> 400Sstevel@tonic-gate #include <sys/cpupart.h> 410Sstevel@tonic-gate #include <sys/cpuvar.h> 420Sstevel@tonic-gate #include <sys/chip.h> 430Sstevel@tonic-gate #include <sys/disp.h> 440Sstevel@tonic-gate #include <sys/cpu.h> 450Sstevel@tonic-gate #include <sys/archsystm.h> 460Sstevel@tonic-gate 470Sstevel@tonic-gate #define OFFSETOF(s, m) (size_t)(&(((s *)0)->m)) 480Sstevel@tonic-gate 490Sstevel@tonic-gate /* 500Sstevel@tonic-gate * Local function prototypes 510Sstevel@tonic-gate */ 520Sstevel@tonic-gate static int mp_disable_intr(processorid_t cpun); 530Sstevel@tonic-gate static void mp_enable_intr(processorid_t cpun); 540Sstevel@tonic-gate static void mach_init(); 550Sstevel@tonic-gate static void mach_picinit(); 560Sstevel@tonic-gate static uint64_t mach_calchz(uint32_t pit_counter, uint64_t *processor_clks); 570Sstevel@tonic-gate static int machhztomhz(uint64_t cpu_freq_hz); 580Sstevel@tonic-gate static uint64_t mach_getcpufreq(void); 590Sstevel@tonic-gate static void mach_fixcpufreq(void); 600Sstevel@tonic-gate static int mach_clkinit(int, int *); 610Sstevel@tonic-gate static void mach_smpinit(void); 620Sstevel@tonic-gate static void mach_set_softintr(int ipl); 630Sstevel@tonic-gate static void mach_cpu_start(int cpun); 640Sstevel@tonic-gate static int mach_softlvl_to_vect(int ipl); 650Sstevel@tonic-gate static void mach_get_platform(int owner); 660Sstevel@tonic-gate static void mach_construct_info(); 670Sstevel@tonic-gate static int mach_translate_irq(dev_info_t *dip, int irqno); 680Sstevel@tonic-gate static int mach_intr_ops(dev_info_t *, ddi_intr_handle_impl_t *, 690Sstevel@tonic-gate psm_intr_op_t, int *); 700Sstevel@tonic-gate static timestruc_t mach_tod_get(void); 710Sstevel@tonic-gate static void mach_tod_set(timestruc_t ts); 720Sstevel@tonic-gate static void mach_notify_error(int level, char *errmsg); 730Sstevel@tonic-gate static hrtime_t dummy_hrtime(void); 740Sstevel@tonic-gate static void dummy_scalehrtime(hrtime_t *); 750Sstevel@tonic-gate static void cpu_halt(void); 760Sstevel@tonic-gate static void cpu_wakeup(cpu_t *, int); 770Sstevel@tonic-gate /* 780Sstevel@tonic-gate * External reference functions 790Sstevel@tonic-gate */ 800Sstevel@tonic-gate extern void return_instr(); 810Sstevel@tonic-gate extern timestruc_t (*todgetf)(void); 820Sstevel@tonic-gate extern void (*todsetf)(timestruc_t); 830Sstevel@tonic-gate extern long gmt_lag; 840Sstevel@tonic-gate extern uint64_t freq_tsc(uint32_t *); 850Sstevel@tonic-gate #if defined(__i386) 860Sstevel@tonic-gate extern uint64_t freq_notsc(uint32_t *); 870Sstevel@tonic-gate #endif 880Sstevel@tonic-gate extern void pc_gethrestime(timestruc_t *); 890Sstevel@tonic-gate 900Sstevel@tonic-gate /* 910Sstevel@tonic-gate * PSM functions initialization 920Sstevel@tonic-gate */ 930Sstevel@tonic-gate void (*psm_shutdownf)(int, int) = return_instr; 940Sstevel@tonic-gate void (*psm_preshutdownf)(int, int) = return_instr; 950Sstevel@tonic-gate void (*psm_notifyf)(int) = return_instr; 960Sstevel@tonic-gate void (*psm_set_idle_cpuf)(int) = return_instr; 970Sstevel@tonic-gate void (*psm_unset_idle_cpuf)(int) = return_instr; 980Sstevel@tonic-gate void (*psminitf)() = mach_init; 990Sstevel@tonic-gate void (*picinitf)() = return_instr; 1000Sstevel@tonic-gate int (*clkinitf)(int, int *) = (int (*)(int, int *))return_instr; 1010Sstevel@tonic-gate void (*cpu_startf)() = return_instr; 1020Sstevel@tonic-gate int (*ap_mlsetup)() = (int (*)(void))return_instr; 1030Sstevel@tonic-gate void (*send_dirintf)() = return_instr; 1040Sstevel@tonic-gate void (*setspl)(int) = return_instr; 1050Sstevel@tonic-gate int (*addspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr; 1060Sstevel@tonic-gate int (*delspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr; 1070Sstevel@tonic-gate void (*setsoftint)(int) = (void (*)(int))return_instr; 1080Sstevel@tonic-gate int (*slvltovect)(int) = (int (*)(int))return_instr; 1090Sstevel@tonic-gate int (*setlvl)(int, int *) = (int (*)(int, int *))return_instr; 1100Sstevel@tonic-gate void (*setlvlx)(int, int) = (void (*)(int, int))return_instr; 1110Sstevel@tonic-gate int (*psm_disable_intr)(int) = mp_disable_intr; 1120Sstevel@tonic-gate void (*psm_enable_intr)(int) = mp_enable_intr; 1130Sstevel@tonic-gate hrtime_t (*gethrtimef)(void) = dummy_hrtime; 1140Sstevel@tonic-gate hrtime_t (*gethrtimeunscaledf)(void) = dummy_hrtime; 1150Sstevel@tonic-gate void (*scalehrtimef)(hrtime_t *) = dummy_scalehrtime; 1160Sstevel@tonic-gate int (*psm_translate_irq)(dev_info_t *, int) = mach_translate_irq; 1170Sstevel@tonic-gate void (*gethrestimef)(timestruc_t *) = pc_gethrestime; 1180Sstevel@tonic-gate int (*psm_todgetf)(todinfo_t *) = (int (*)(todinfo_t *))return_instr; 1190Sstevel@tonic-gate int (*psm_todsetf)(todinfo_t *) = (int (*)(todinfo_t *))return_instr; 1200Sstevel@tonic-gate void (*psm_notify_error)(int, char *) = (void (*)(int, char *))NULL; 1210Sstevel@tonic-gate int (*psm_get_clockirq)(int) = NULL; 1220Sstevel@tonic-gate int (*psm_get_ipivect)(int, int) = NULL; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate int (*psm_clkinit)(int) = NULL; 1250Sstevel@tonic-gate void (*psm_timer_reprogram)(hrtime_t) = NULL; 1260Sstevel@tonic-gate void (*psm_timer_enable)(void) = NULL; 1270Sstevel@tonic-gate void (*psm_timer_disable)(void) = NULL; 1280Sstevel@tonic-gate void (*psm_post_cyclic_setup)(void *arg) = NULL; 1290Sstevel@tonic-gate int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *, psm_intr_op_t, 1300Sstevel@tonic-gate int *) = mach_intr_ops; 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate void (*notify_error)(int, char *) = (void (*)(int, char *))return_instr; 1330Sstevel@tonic-gate void (*hrtime_tick)(void) = return_instr; 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate int tsc_gethrtime_enable = 1; 1360Sstevel@tonic-gate int tsc_gethrtime_initted = 0; 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate /* 1390Sstevel@tonic-gate * Local Static Data 1400Sstevel@tonic-gate */ 1410Sstevel@tonic-gate static struct psm_ops mach_ops; 1420Sstevel@tonic-gate static struct psm_ops *mach_set[4] = {&mach_ops, NULL, NULL, NULL}; 1430Sstevel@tonic-gate static ushort_t mach_ver[4] = {0, 0, 0, 0}; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate /* 1460Sstevel@tonic-gate * If non-zero, idle cpus will "halted" when there's 1470Sstevel@tonic-gate * no work to do. 1480Sstevel@tonic-gate */ 1490Sstevel@tonic-gate int halt_idle_cpus = 1; 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate #if defined(__amd64) 1520Sstevel@tonic-gate /* 1530Sstevel@tonic-gate * If non-zero, will use cr8 for interrupt priority masking 1540Sstevel@tonic-gate * We declare this here since install_spl is called from here 1550Sstevel@tonic-gate * (where this is checked). 1560Sstevel@tonic-gate */ 1570Sstevel@tonic-gate int intpri_use_cr8 = 0; 1580Sstevel@tonic-gate #endif /* __amd64 */ 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate #ifdef _SIMULATOR_SUPPORT 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate int simulator_run = 0; /* patch to non-zero if running under simics */ 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate #endif /* _SIMULATOR_SUPPORT */ 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate /* ARGSUSED */ 1670Sstevel@tonic-gate void 1680Sstevel@tonic-gate chip_plat_define_chip(cpu_t *cp, chip_def_t *cd) 1690Sstevel@tonic-gate { 1700Sstevel@tonic-gate if (x86_feature & (X86_HTT|X86_CMP)) 1710Sstevel@tonic-gate /* 1720Sstevel@tonic-gate * Hyperthreading is SMT 1730Sstevel@tonic-gate */ 1740Sstevel@tonic-gate cd->chipd_type = CHIP_SMT; 1750Sstevel@tonic-gate else 1760Sstevel@tonic-gate cd->chipd_type = CHIP_DEFAULT; 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate cd->chipd_rechoose_adj = 0; 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate /* 1820Sstevel@tonic-gate * Routine to ensure initial callers to hrtime gets 0 as return 1830Sstevel@tonic-gate */ 1840Sstevel@tonic-gate static hrtime_t 1850Sstevel@tonic-gate dummy_hrtime(void) 1860Sstevel@tonic-gate { 1870Sstevel@tonic-gate return (0); 1880Sstevel@tonic-gate } 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate /* ARGSUSED */ 1910Sstevel@tonic-gate static void 1920Sstevel@tonic-gate dummy_scalehrtime(hrtime_t *ticks) 1930Sstevel@tonic-gate {} 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate /* 1960Sstevel@tonic-gate * Halt the present CPU until awoken via an interrupt 1970Sstevel@tonic-gate */ 1980Sstevel@tonic-gate static void 1990Sstevel@tonic-gate cpu_halt(void) 2000Sstevel@tonic-gate { 2010Sstevel@tonic-gate cpu_t *cpup = CPU; 2020Sstevel@tonic-gate processorid_t cpun = cpup->cpu_id; 203*711Sesaxe cpupart_t *cp = cpup->cpu_part; 2040Sstevel@tonic-gate int hset_update = 1; 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate /* 2070Sstevel@tonic-gate * If this CPU is online, and there's multiple CPUs 2080Sstevel@tonic-gate * in the system, then we should notate our halting 2090Sstevel@tonic-gate * by adding ourselves to the partition's halted CPU 2100Sstevel@tonic-gate * bitmap. This allows other CPUs to find/awaken us when 2110Sstevel@tonic-gate * work becomes available. 2120Sstevel@tonic-gate */ 2130Sstevel@tonic-gate if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1) 2140Sstevel@tonic-gate hset_update = 0; 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* 2170Sstevel@tonic-gate * Add ourselves to the partition's halted CPUs bitmask 2180Sstevel@tonic-gate * and set our HALTED flag, if necessary. 2190Sstevel@tonic-gate * 220*711Sesaxe * When a thread becomes runnable, it is placed on the queue 221*711Sesaxe * and then the halted cpuset is checked to determine who 222*711Sesaxe * (if anyone) should be awoken. We therefore need to first 223*711Sesaxe * add ourselves to the halted cpuset, and and then check if there 224*711Sesaxe * is any work available. 225*711Sesaxe * 2260Sstevel@tonic-gate * Note that memory barriers after updating the HALTED flag 2270Sstevel@tonic-gate * are not necessary since an atomic operation (updating the bitmap) 2280Sstevel@tonic-gate * immediately follows. On x86 the atomic operation acts as a 2290Sstevel@tonic-gate * memory barrier for the update of cpu_disp_flags. 2300Sstevel@tonic-gate */ 2310Sstevel@tonic-gate if (hset_update) { 2320Sstevel@tonic-gate cpup->cpu_disp_flags |= CPU_DISP_HALTED; 2330Sstevel@tonic-gate CPUSET_ATOMIC_ADD(cp->cp_haltset, cpun); 2340Sstevel@tonic-gate } 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate /* 2370Sstevel@tonic-gate * Check to make sure there's really nothing to do. 238*711Sesaxe * Work destined for this CPU may become available after 239*711Sesaxe * this check. We'll be notified through the clearing of our 240*711Sesaxe * bit in the halted CPU bitmask, and a poke. 2410Sstevel@tonic-gate */ 2420Sstevel@tonic-gate if (disp_anywork()) { 2430Sstevel@tonic-gate if (hset_update) { 2440Sstevel@tonic-gate cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 2450Sstevel@tonic-gate CPUSET_ATOMIC_DEL(cp->cp_haltset, cpun); 2460Sstevel@tonic-gate } 247*711Sesaxe return; 248*711Sesaxe } 249*711Sesaxe 250*711Sesaxe /* 251*711Sesaxe * We're on our way to being halted. 252*711Sesaxe * 253*711Sesaxe * Disable interrupts now, so that we'll awaken immediately 254*711Sesaxe * after halting if someone tries to poke us between now and 255*711Sesaxe * the time we actually halt. 256*711Sesaxe * 257*711Sesaxe * We check for the presence of our bit after disabling interrupts. 258*711Sesaxe * If it's cleared, we'll return. If the bit is cleared after 259*711Sesaxe * we check then the poke will pop us out of the halted state. 260*711Sesaxe * 261*711Sesaxe * This means that the ordering of the poke and the clearing 262*711Sesaxe * of the bit by cpu_wakeup is important. 263*711Sesaxe * cpu_wakeup() must clear, then poke. 264*711Sesaxe * cpu_halt() must disable interrupts, then check for the bit. 265*711Sesaxe */ 266*711Sesaxe cli(); 267*711Sesaxe 268*711Sesaxe if (hset_update && !CPU_IN_SET(cp->cp_haltset, cpun)) { 269*711Sesaxe cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 270*711Sesaxe sti(); 271*711Sesaxe return; 272*711Sesaxe } 273*711Sesaxe 274*711Sesaxe /* 275*711Sesaxe * The check for anything locally runnable is here for performance 276*711Sesaxe * and isn't needed for correctness. disp_nrunnable ought to be 277*711Sesaxe * in our cache still, so it's inexpensive to check, and if there 278*711Sesaxe * is anything runnable we won't have to wait for the poke. 279*711Sesaxe */ 280*711Sesaxe if (cpup->cpu_disp->disp_nrunnable != 0) { 281*711Sesaxe if (hset_update) { 282*711Sesaxe cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 283*711Sesaxe CPUSET_ATOMIC_DEL(cp->cp_haltset, cpun); 284*711Sesaxe } 2850Sstevel@tonic-gate sti(); 2860Sstevel@tonic-gate return; 2870Sstevel@tonic-gate } 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate /* 2900Sstevel@tonic-gate * Call the halt sequence: 2910Sstevel@tonic-gate * sti 2920Sstevel@tonic-gate * hlt 2930Sstevel@tonic-gate */ 2940Sstevel@tonic-gate i86_halt(); 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate /* 2970Sstevel@tonic-gate * We're no longer halted 2980Sstevel@tonic-gate */ 2990Sstevel@tonic-gate if (hset_update) { 3000Sstevel@tonic-gate cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 3010Sstevel@tonic-gate CPUSET_ATOMIC_DEL(cp->cp_haltset, cpun); 3020Sstevel@tonic-gate } 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate /* 3070Sstevel@tonic-gate * If "cpu" is halted, then wake it up clearing its halted bit in advance. 3080Sstevel@tonic-gate * Otherwise, see if other CPUs in the cpu partition are halted and need to 3090Sstevel@tonic-gate * be woken up so that they can steal the thread we placed on this CPU. 3100Sstevel@tonic-gate * This function is only used on MP systems. 3110Sstevel@tonic-gate */ 3120Sstevel@tonic-gate static void 3130Sstevel@tonic-gate cpu_wakeup(cpu_t *cpu, int bound) 3140Sstevel@tonic-gate { 3150Sstevel@tonic-gate uint_t cpu_found; 3160Sstevel@tonic-gate int result; 3170Sstevel@tonic-gate cpupart_t *cp; 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate cp = cpu->cpu_part; 3200Sstevel@tonic-gate if (CPU_IN_SET(cp->cp_haltset, cpu->cpu_id)) { 3210Sstevel@tonic-gate /* 3220Sstevel@tonic-gate * Clear the halted bit for that CPU since it will be 3230Sstevel@tonic-gate * poked in a moment. 3240Sstevel@tonic-gate */ 3250Sstevel@tonic-gate CPUSET_ATOMIC_DEL(cp->cp_haltset, cpu->cpu_id); 3260Sstevel@tonic-gate /* 3270Sstevel@tonic-gate * We may find the current CPU present in the halted cpuset 3280Sstevel@tonic-gate * if we're in the context of an interrupt that occurred 3290Sstevel@tonic-gate * before we had a chance to clear our bit in cpu_halt(). 3300Sstevel@tonic-gate * Poking ourself is obviously unnecessary, since if 3310Sstevel@tonic-gate * we're here, we're not halted. 3320Sstevel@tonic-gate */ 3330Sstevel@tonic-gate if (cpu != CPU) 3340Sstevel@tonic-gate poke_cpu(cpu->cpu_id); 3350Sstevel@tonic-gate return; 3360Sstevel@tonic-gate } else { 3370Sstevel@tonic-gate /* 3380Sstevel@tonic-gate * This cpu isn't halted, but it's idle or undergoing a 3390Sstevel@tonic-gate * context switch. No need to awaken anyone else. 3400Sstevel@tonic-gate */ 3410Sstevel@tonic-gate if (cpu->cpu_thread == cpu->cpu_idle_thread || 3420Sstevel@tonic-gate cpu->cpu_disp_flags & CPU_DISP_DONTSTEAL) 3430Sstevel@tonic-gate return; 3440Sstevel@tonic-gate } 3450Sstevel@tonic-gate 3460Sstevel@tonic-gate /* 3470Sstevel@tonic-gate * No need to wake up other CPUs if the thread we just enqueued 3480Sstevel@tonic-gate * is bound. 3490Sstevel@tonic-gate */ 3500Sstevel@tonic-gate if (bound) 3510Sstevel@tonic-gate return; 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate /* 3550Sstevel@tonic-gate * See if there's any other halted CPUs. If there are, then 3560Sstevel@tonic-gate * select one, and awaken it. 3570Sstevel@tonic-gate * It's possible that after we find a CPU, somebody else 3580Sstevel@tonic-gate * will awaken it before we get the chance. 3590Sstevel@tonic-gate * In that case, look again. 3600Sstevel@tonic-gate */ 3610Sstevel@tonic-gate do { 3620Sstevel@tonic-gate CPUSET_FIND(cp->cp_haltset, cpu_found); 3630Sstevel@tonic-gate if (cpu_found == CPUSET_NOTINSET) 3640Sstevel@tonic-gate return; 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate ASSERT(cpu_found >= 0 && cpu_found < NCPU); 3670Sstevel@tonic-gate CPUSET_ATOMIC_XDEL(cp->cp_haltset, cpu_found, result); 3680Sstevel@tonic-gate } while (result < 0); 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate if (cpu_found != CPU->cpu_id) 3710Sstevel@tonic-gate poke_cpu(cpu_found); 3720Sstevel@tonic-gate } 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate static int 3750Sstevel@tonic-gate mp_disable_intr(int cpun) 3760Sstevel@tonic-gate { 3770Sstevel@tonic-gate /* 3780Sstevel@tonic-gate * switch to the offline cpu 3790Sstevel@tonic-gate */ 3800Sstevel@tonic-gate affinity_set(cpun); 3810Sstevel@tonic-gate /* 3820Sstevel@tonic-gate * raise ipl to just below cross call 3830Sstevel@tonic-gate */ 3840Sstevel@tonic-gate splx(XC_MED_PIL-1); 3850Sstevel@tonic-gate /* 3860Sstevel@tonic-gate * set base spl to prevent the next swtch to idle from 3870Sstevel@tonic-gate * lowering back to ipl 0 3880Sstevel@tonic-gate */ 3890Sstevel@tonic-gate CPU->cpu_intr_actv |= (1 << (XC_MED_PIL-1)); 3900Sstevel@tonic-gate set_base_spl(); 3910Sstevel@tonic-gate affinity_clear(); 3920Sstevel@tonic-gate return (DDI_SUCCESS); 3930Sstevel@tonic-gate } 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate static void 3960Sstevel@tonic-gate mp_enable_intr(int cpun) 3970Sstevel@tonic-gate { 3980Sstevel@tonic-gate /* 3990Sstevel@tonic-gate * switch to the online cpu 4000Sstevel@tonic-gate */ 4010Sstevel@tonic-gate affinity_set(cpun); 4020Sstevel@tonic-gate /* 4030Sstevel@tonic-gate * clear the interrupt active mask 4040Sstevel@tonic-gate */ 4050Sstevel@tonic-gate CPU->cpu_intr_actv &= ~(1 << (XC_MED_PIL-1)); 4060Sstevel@tonic-gate set_base_spl(); 4070Sstevel@tonic-gate (void) spl0(); 4080Sstevel@tonic-gate affinity_clear(); 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate static void 4120Sstevel@tonic-gate mach_get_platform(int owner) 4130Sstevel@tonic-gate { 4140Sstevel@tonic-gate void **srv_opsp; 4150Sstevel@tonic-gate void **clt_opsp; 4160Sstevel@tonic-gate int i; 4170Sstevel@tonic-gate int total_ops; 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate /* fix up psm ops */ 4200Sstevel@tonic-gate srv_opsp = (void **)mach_set[0]; 4210Sstevel@tonic-gate clt_opsp = (void **)mach_set[owner]; 4220Sstevel@tonic-gate if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01) 4230Sstevel@tonic-gate total_ops = sizeof (struct psm_ops_ver01) / 4240Sstevel@tonic-gate sizeof (void (*)(void)); 4250Sstevel@tonic-gate else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_1) 4260Sstevel@tonic-gate /* no psm_notify_func */ 4270Sstevel@tonic-gate total_ops = OFFSETOF(struct psm_ops, psm_notify_func) / 4280Sstevel@tonic-gate sizeof (void (*)(void)); 4290Sstevel@tonic-gate else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_2) 4300Sstevel@tonic-gate /* no psm_timer funcs */ 4310Sstevel@tonic-gate total_ops = OFFSETOF(struct psm_ops, psm_timer_reprogram) / 4320Sstevel@tonic-gate sizeof (void (*)(void)); 4330Sstevel@tonic-gate else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_3) 4340Sstevel@tonic-gate /* no psm_preshutdown function */ 4350Sstevel@tonic-gate total_ops = OFFSETOF(struct psm_ops, psm_preshutdown) / 4360Sstevel@tonic-gate sizeof (void (*)(void)); 4370Sstevel@tonic-gate else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_4) 4380Sstevel@tonic-gate /* no psm_preshutdown function */ 4390Sstevel@tonic-gate total_ops = OFFSETOF(struct psm_ops, psm_intr_ops) / 4400Sstevel@tonic-gate sizeof (void (*)(void)); 4410Sstevel@tonic-gate else 4420Sstevel@tonic-gate total_ops = sizeof (struct psm_ops) / sizeof (void (*)(void)); 4430Sstevel@tonic-gate 4440Sstevel@tonic-gate /* 4450Sstevel@tonic-gate * Save the version of the PSM module, in case we need to 4460Sstevel@tonic-gate * bahave differently based on version. 4470Sstevel@tonic-gate */ 4480Sstevel@tonic-gate mach_ver[0] = mach_ver[owner]; 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate for (i = 0; i < total_ops; i++) 4510Sstevel@tonic-gate if (clt_opsp[i] != NULL) 4520Sstevel@tonic-gate srv_opsp[i] = clt_opsp[i]; 4530Sstevel@tonic-gate } 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate static void 4560Sstevel@tonic-gate mach_construct_info() 4570Sstevel@tonic-gate { 4580Sstevel@tonic-gate register struct psm_sw *swp; 4590Sstevel@tonic-gate int mach_cnt[PSM_OWN_OVERRIDE+1] = {0}; 4600Sstevel@tonic-gate int conflict_owner = 0; 4610Sstevel@tonic-gate 4620Sstevel@tonic-gate if (psmsw->psw_forw == psmsw) 4630Sstevel@tonic-gate panic("No valid PSM modules found"); 4640Sstevel@tonic-gate mutex_enter(&psmsw_lock); 4650Sstevel@tonic-gate for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) { 4660Sstevel@tonic-gate if (!(swp->psw_flag & PSM_MOD_IDENTIFY)) 4670Sstevel@tonic-gate continue; 4680Sstevel@tonic-gate mach_set[swp->psw_infop->p_owner] = swp->psw_infop->p_ops; 4690Sstevel@tonic-gate mach_ver[swp->psw_infop->p_owner] = swp->psw_infop->p_version; 4700Sstevel@tonic-gate mach_cnt[swp->psw_infop->p_owner]++; 4710Sstevel@tonic-gate } 4720Sstevel@tonic-gate mutex_exit(&psmsw_lock); 4730Sstevel@tonic-gate 4740Sstevel@tonic-gate mach_get_platform(PSM_OWN_SYS_DEFAULT); 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate /* check to see are there any conflicts */ 4770Sstevel@tonic-gate if (mach_cnt[PSM_OWN_EXCLUSIVE] > 1) 4780Sstevel@tonic-gate conflict_owner = PSM_OWN_EXCLUSIVE; 4790Sstevel@tonic-gate if (mach_cnt[PSM_OWN_OVERRIDE] > 1) 4800Sstevel@tonic-gate conflict_owner = PSM_OWN_OVERRIDE; 4810Sstevel@tonic-gate if (conflict_owner) { 4820Sstevel@tonic-gate /* remove all psm modules except uppc */ 4830Sstevel@tonic-gate cmn_err(CE_WARN, 4840Sstevel@tonic-gate "Conflicts detected on the following PSM modules:"); 4850Sstevel@tonic-gate mutex_enter(&psmsw_lock); 4860Sstevel@tonic-gate for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) { 4870Sstevel@tonic-gate if (swp->psw_infop->p_owner == conflict_owner) 4880Sstevel@tonic-gate cmn_err(CE_WARN, "%s ", 4890Sstevel@tonic-gate swp->psw_infop->p_mach_idstring); 4900Sstevel@tonic-gate } 4910Sstevel@tonic-gate mutex_exit(&psmsw_lock); 4920Sstevel@tonic-gate cmn_err(CE_WARN, 4930Sstevel@tonic-gate "Setting the system back to SINGLE processor mode!"); 4940Sstevel@tonic-gate cmn_err(CE_WARN, 4950Sstevel@tonic-gate "Please edit /etc/mach to remove the invalid PSM module."); 4960Sstevel@tonic-gate return; 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate if (mach_set[PSM_OWN_EXCLUSIVE]) 5000Sstevel@tonic-gate mach_get_platform(PSM_OWN_EXCLUSIVE); 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate if (mach_set[PSM_OWN_OVERRIDE]) 5030Sstevel@tonic-gate mach_get_platform(PSM_OWN_OVERRIDE); 5040Sstevel@tonic-gate } 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate static void 5070Sstevel@tonic-gate mach_init() 5080Sstevel@tonic-gate { 5090Sstevel@tonic-gate register struct psm_ops *pops; 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate mach_construct_info(); 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate pops = mach_set[0]; 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate /* register the interrupt and clock initialization rotuines */ 5160Sstevel@tonic-gate picinitf = mach_picinit; 5170Sstevel@tonic-gate clkinitf = mach_clkinit; 5180Sstevel@tonic-gate psm_get_clockirq = pops->psm_get_clockirq; 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate /* register the interrupt setup code */ 5210Sstevel@tonic-gate slvltovect = mach_softlvl_to_vect; 5220Sstevel@tonic-gate addspl = pops->psm_addspl; 5230Sstevel@tonic-gate delspl = pops->psm_delspl; 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate if (pops->psm_translate_irq) 5260Sstevel@tonic-gate psm_translate_irq = pops->psm_translate_irq; 5270Sstevel@tonic-gate if (pops->psm_intr_ops) 5280Sstevel@tonic-gate psm_intr_ops = pops->psm_intr_ops; 5290Sstevel@tonic-gate if (pops->psm_tod_get) { 5300Sstevel@tonic-gate todgetf = mach_tod_get; 5310Sstevel@tonic-gate psm_todgetf = pops->psm_tod_get; 5320Sstevel@tonic-gate } 5330Sstevel@tonic-gate if (pops->psm_tod_set) { 5340Sstevel@tonic-gate todsetf = mach_tod_set; 5350Sstevel@tonic-gate psm_todsetf = pops->psm_tod_set; 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate if (pops->psm_notify_error) { 5380Sstevel@tonic-gate psm_notify_error = mach_notify_error; 5390Sstevel@tonic-gate notify_error = pops->psm_notify_error; 5400Sstevel@tonic-gate } 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate (*pops->psm_softinit)(); 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate /* 5450Sstevel@tonic-gate * Initialize the dispatcher's function hooks 5460Sstevel@tonic-gate * to enable CPU halting when idle 5470Sstevel@tonic-gate */ 5480Sstevel@tonic-gate #if defined(_SIMULATOR_SUPPORT) 5490Sstevel@tonic-gate if (halt_idle_cpus && !simulator_run) 5500Sstevel@tonic-gate idle_cpu = cpu_halt; 5510Sstevel@tonic-gate #else 5520Sstevel@tonic-gate if (halt_idle_cpus) 5530Sstevel@tonic-gate idle_cpu = cpu_halt; 5540Sstevel@tonic-gate #endif /* _SIMULATOR_SUPPORT */ 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate mach_smpinit(); 5570Sstevel@tonic-gate } 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate static void 5600Sstevel@tonic-gate mach_smpinit(void) 5610Sstevel@tonic-gate { 5620Sstevel@tonic-gate register struct psm_ops *pops; 5630Sstevel@tonic-gate register processorid_t cpu_id; 5640Sstevel@tonic-gate int cnt; 5650Sstevel@tonic-gate int cpumask; 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate pops = mach_set[0]; 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate cpu_id = -1; 5700Sstevel@tonic-gate cpu_id = (*pops->psm_get_next_processorid)(cpu_id); 5710Sstevel@tonic-gate for (cnt = 0, cpumask = 0; cpu_id != -1; cnt++) { 5720Sstevel@tonic-gate cpumask |= 1 << cpu_id; 5730Sstevel@tonic-gate cpu_id = (*pops->psm_get_next_processorid)(cpu_id); 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate mp_cpus = cpumask; 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate /* MP related routines */ 5790Sstevel@tonic-gate cpu_startf = mach_cpu_start; 5800Sstevel@tonic-gate ap_mlsetup = pops->psm_post_cpu_start; 5810Sstevel@tonic-gate send_dirintf = pops->psm_send_ipi; 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate /* optional MP related routines */ 5840Sstevel@tonic-gate if (pops->psm_shutdown) 5850Sstevel@tonic-gate psm_shutdownf = pops->psm_shutdown; 5860Sstevel@tonic-gate if (pops->psm_preshutdown) 5870Sstevel@tonic-gate psm_preshutdownf = pops->psm_preshutdown; 5880Sstevel@tonic-gate if (pops->psm_notify_func) 5890Sstevel@tonic-gate psm_notifyf = pops->psm_notify_func; 5900Sstevel@tonic-gate if (pops->psm_set_idlecpu) 5910Sstevel@tonic-gate psm_set_idle_cpuf = pops->psm_set_idlecpu; 5920Sstevel@tonic-gate if (pops->psm_unset_idlecpu) 5930Sstevel@tonic-gate psm_unset_idle_cpuf = pops->psm_unset_idlecpu; 5940Sstevel@tonic-gate 5950Sstevel@tonic-gate psm_clkinit = pops->psm_clkinit; 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate if (pops->psm_timer_reprogram) 5980Sstevel@tonic-gate psm_timer_reprogram = pops->psm_timer_reprogram; 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate if (pops->psm_timer_enable) 6010Sstevel@tonic-gate psm_timer_enable = pops->psm_timer_enable; 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate if (pops->psm_timer_disable) 6040Sstevel@tonic-gate psm_timer_disable = pops->psm_timer_disable; 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate if (pops->psm_post_cyclic_setup) 6070Sstevel@tonic-gate psm_post_cyclic_setup = pops->psm_post_cyclic_setup; 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate /* check for multiple cpu's */ 6100Sstevel@tonic-gate if (cnt < 2) 6110Sstevel@tonic-gate return; 6120Sstevel@tonic-gate 6130Sstevel@tonic-gate /* check for MP platforms */ 6140Sstevel@tonic-gate if (pops->psm_cpu_start == NULL) 6150Sstevel@tonic-gate return; 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate /* 6180Sstevel@tonic-gate * Set the dispatcher hook to enable cpu "wake up" 6190Sstevel@tonic-gate * when a thread becomes runnable. 6200Sstevel@tonic-gate */ 6210Sstevel@tonic-gate #if defined(_SIMULATOR_SUPPORT) 6220Sstevel@tonic-gate if (halt_idle_cpus && !simulator_run) { 6230Sstevel@tonic-gate disp_enq_thread = cpu_wakeup; 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate #else 6260Sstevel@tonic-gate if (halt_idle_cpus) { 6270Sstevel@tonic-gate disp_enq_thread = cpu_wakeup; 6280Sstevel@tonic-gate } 6290Sstevel@tonic-gate #endif /* _SIMULATOR_SUPPORT */ 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate if (pops->psm_disable_intr) 6320Sstevel@tonic-gate psm_disable_intr = pops->psm_disable_intr; 6330Sstevel@tonic-gate if (pops->psm_enable_intr) 6340Sstevel@tonic-gate psm_enable_intr = pops->psm_enable_intr; 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate psm_get_ipivect = pops->psm_get_ipivect; 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate (void) add_avintr((void *)NULL, XC_HI_PIL, xc_serv, "xc_hi_intr", 6390Sstevel@tonic-gate (*pops->psm_get_ipivect)(XC_HI_PIL, PSM_INTR_IPI_HI), 6400Sstevel@tonic-gate (caddr_t)X_CALL_HIPRI, NULL, NULL); 6410Sstevel@tonic-gate (void) add_avintr((void *)NULL, XC_MED_PIL, xc_serv, "xc_med_intr", 6420Sstevel@tonic-gate (*pops->psm_get_ipivect)(XC_MED_PIL, PSM_INTR_IPI_LO), 6430Sstevel@tonic-gate (caddr_t)X_CALL_MEDPRI, NULL, NULL); 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate (void) (*pops->psm_get_ipivect)(XC_CPUPOKE_PIL, PSM_INTR_POKE); 6460Sstevel@tonic-gate } 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate static void 6490Sstevel@tonic-gate mach_picinit() 6500Sstevel@tonic-gate { 6510Sstevel@tonic-gate register struct psm_ops *pops; 6520Sstevel@tonic-gate extern void install_spl(void); /* XXX: belongs in a header file */ 6530Sstevel@tonic-gate #if defined(__amd64) && defined(DEBUG) 6540Sstevel@tonic-gate extern void *spl_patch, *slow_spl, *setsplhi_patch, *slow_setsplhi; 6550Sstevel@tonic-gate #endif 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate pops = mach_set[0]; 6580Sstevel@tonic-gate 6590Sstevel@tonic-gate /* register the interrupt handlers */ 6600Sstevel@tonic-gate setlvl = pops->psm_intr_enter; 6610Sstevel@tonic-gate setlvlx = pops->psm_intr_exit; 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate /* initialize the interrupt hardware */ 6640Sstevel@tonic-gate (*pops->psm_picinit)(); 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate /* set interrupt mask for current ipl */ 6670Sstevel@tonic-gate setspl = pops->psm_setspl; 6680Sstevel@tonic-gate setspl(CPU->cpu_pri); 6690Sstevel@tonic-gate 6700Sstevel@tonic-gate /* Install proper spl routine now that we can Program the PIC */ 6710Sstevel@tonic-gate #if defined(__amd64) 6720Sstevel@tonic-gate /* 6730Sstevel@tonic-gate * It would be better if we could check this at compile time 6740Sstevel@tonic-gate */ 6750Sstevel@tonic-gate ASSERT(((uintptr_t)&slow_setsplhi - (uintptr_t)&setsplhi_patch < 128) && 6760Sstevel@tonic-gate ((uintptr_t)&slow_spl - (uintptr_t)&spl_patch < 128)); 6770Sstevel@tonic-gate #endif 6780Sstevel@tonic-gate install_spl(); 6790Sstevel@tonic-gate } 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate uint_t cpu_freq; /* MHz */ 6820Sstevel@tonic-gate uint64_t cpu_freq_hz; /* measured (in hertz) */ 6830Sstevel@tonic-gate 6840Sstevel@tonic-gate #define MEGA_HZ 1000000 6850Sstevel@tonic-gate 6860Sstevel@tonic-gate static uint64_t 6870Sstevel@tonic-gate mach_calchz(uint32_t pit_counter, uint64_t *processor_clks) 6880Sstevel@tonic-gate { 6890Sstevel@tonic-gate uint64_t cpu_hz; 6900Sstevel@tonic-gate 6910Sstevel@tonic-gate if ((pit_counter == 0) || (*processor_clks == 0) || 6920Sstevel@tonic-gate (*processor_clks > (((uint64_t)-1) / PIT_HZ))) 6930Sstevel@tonic-gate return (0); 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate cpu_hz = ((uint64_t)PIT_HZ * *processor_clks) / pit_counter; 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate return (cpu_hz); 6980Sstevel@tonic-gate } 6990Sstevel@tonic-gate 7000Sstevel@tonic-gate static uint64_t 7010Sstevel@tonic-gate mach_getcpufreq(void) 7020Sstevel@tonic-gate { 7030Sstevel@tonic-gate uint32_t pit_counter; 7040Sstevel@tonic-gate uint64_t processor_clks; 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate if (x86_feature & X86_TSC) { 7070Sstevel@tonic-gate /* 7080Sstevel@tonic-gate * We have a TSC. freq_tsc() knows how to measure the number 7090Sstevel@tonic-gate * of clock cycles sampled against the PIT. 7100Sstevel@tonic-gate */ 7110Sstevel@tonic-gate processor_clks = freq_tsc(&pit_counter); 7120Sstevel@tonic-gate return (mach_calchz(pit_counter, &processor_clks)); 7130Sstevel@tonic-gate } else if (x86_vendor == X86_VENDOR_Cyrix || x86_type == X86_TYPE_P5) { 7140Sstevel@tonic-gate #if defined(__amd64) 7150Sstevel@tonic-gate panic("mach_getcpufreq: no TSC!"); 7160Sstevel@tonic-gate #elif defined(__i386) 7170Sstevel@tonic-gate /* 7180Sstevel@tonic-gate * We are a Cyrix based on a 6x86 core or an Intel Pentium 7190Sstevel@tonic-gate * for which freq_notsc() knows how to measure the number of 7200Sstevel@tonic-gate * elapsed clock cycles sampled against the PIT 7210Sstevel@tonic-gate */ 7220Sstevel@tonic-gate processor_clks = freq_notsc(&pit_counter); 7230Sstevel@tonic-gate return (mach_calchz(pit_counter, &processor_clks)); 7240Sstevel@tonic-gate #endif /* __i386 */ 7250Sstevel@tonic-gate } 7260Sstevel@tonic-gate 7270Sstevel@tonic-gate /* We do not know how to calculate cpu frequency for this cpu. */ 7280Sstevel@tonic-gate return (0); 7290Sstevel@tonic-gate } 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate /* 7320Sstevel@tonic-gate * If the clock speed of a cpu is found to be reported incorrectly, do not add 7330Sstevel@tonic-gate * to this array, instead improve the accuracy of the algorithm that determines 7340Sstevel@tonic-gate * the clock speed of the processor or extend the implementation to support the 7350Sstevel@tonic-gate * vendor as appropriate. This is here only to support adjusting the speed on 7360Sstevel@tonic-gate * older slower processors that mach_fixcpufreq() would not be able to account 7370Sstevel@tonic-gate * for otherwise. 7380Sstevel@tonic-gate */ 7390Sstevel@tonic-gate static int x86_cpu_freq[] = { 60, 75, 80, 90, 120, 160, 166, 175, 180, 233 }; 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate /* 7420Sstevel@tonic-gate * On fast processors the clock frequency that is measured may be off by 7430Sstevel@tonic-gate * a few MHz from the value printed on the part. This is a combination of 7440Sstevel@tonic-gate * the factors that for such fast parts being off by this much is within 7450Sstevel@tonic-gate * the tolerances for manufacture and because of the difficulties in the 7460Sstevel@tonic-gate * measurement that can lead to small error. This function uses some 7470Sstevel@tonic-gate * heuristics in order to tweak the value that was measured to match what 7480Sstevel@tonic-gate * is most likely printed on the part. 7490Sstevel@tonic-gate * 7500Sstevel@tonic-gate * Some examples: 7510Sstevel@tonic-gate * AMD Athlon 1000 mhz measured as 998 mhz 7520Sstevel@tonic-gate * Intel Pentium III Xeon 733 mhz measured as 731 mhz 7530Sstevel@tonic-gate * Intel Pentium IV 1500 mhz measured as 1495mhz 7540Sstevel@tonic-gate * 7550Sstevel@tonic-gate * If in the future this function is no longer sufficient to correct 7560Sstevel@tonic-gate * for the error in the measurement, then the algorithm used to perform 7570Sstevel@tonic-gate * the measurement will have to be improved in order to increase accuracy 7580Sstevel@tonic-gate * rather than adding horrible and questionable kludges here. 7590Sstevel@tonic-gate * 7600Sstevel@tonic-gate * This is called after the cyclics subsystem because of the potential 7610Sstevel@tonic-gate * that the heuristics within may give a worse estimate of the clock 7620Sstevel@tonic-gate * frequency than the value that was measured. 7630Sstevel@tonic-gate */ 7640Sstevel@tonic-gate static void 7650Sstevel@tonic-gate mach_fixcpufreq(void) 7660Sstevel@tonic-gate { 7670Sstevel@tonic-gate uint32_t freq, mul, near66, delta66, near50, delta50, fixed, delta, i; 7680Sstevel@tonic-gate 7690Sstevel@tonic-gate freq = (uint32_t)cpu_freq; 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate /* 7720Sstevel@tonic-gate * Find the nearest integer multiple of 200/3 (about 66) MHz to the 7730Sstevel@tonic-gate * measured speed taking into account that the 667 MHz parts were 7740Sstevel@tonic-gate * the first to round-up. 7750Sstevel@tonic-gate */ 7760Sstevel@tonic-gate mul = (uint32_t)((3 * (uint64_t)freq + 100) / 200); 7770Sstevel@tonic-gate near66 = (uint32_t)((200 * (uint64_t)mul + ((mul >= 10) ? 1 : 0)) / 3); 7780Sstevel@tonic-gate delta66 = (near66 > freq) ? (near66 - freq) : (freq - near66); 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate /* Find the nearest integer multiple of 50 MHz to the measured speed */ 7810Sstevel@tonic-gate mul = (freq + 25) / 50; 7820Sstevel@tonic-gate near50 = mul * 50; 7830Sstevel@tonic-gate delta50 = (near50 > freq) ? (near50 - freq) : (freq - near50); 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate /* Find the closer of the two */ 7860Sstevel@tonic-gate if (delta66 < delta50) { 7870Sstevel@tonic-gate fixed = near66; 7880Sstevel@tonic-gate delta = delta66; 7890Sstevel@tonic-gate } else { 7900Sstevel@tonic-gate fixed = near50; 7910Sstevel@tonic-gate delta = delta50; 7920Sstevel@tonic-gate } 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate if (fixed > INT_MAX) 7950Sstevel@tonic-gate return; 7960Sstevel@tonic-gate 7970Sstevel@tonic-gate /* 7980Sstevel@tonic-gate * Some older parts have a core clock frequency that is not an 7990Sstevel@tonic-gate * integral multiple of 50 or 66 MHz. Check if one of the old 8000Sstevel@tonic-gate * clock frequencies is closer to the measured value than any 8010Sstevel@tonic-gate * of the integral multiples of 50 an 66, and if so set fixed 8020Sstevel@tonic-gate * and delta appropriately to represent the closest value. 8030Sstevel@tonic-gate */ 8040Sstevel@tonic-gate i = sizeof (x86_cpu_freq) / sizeof (int); 8050Sstevel@tonic-gate while (i > 0) { 8060Sstevel@tonic-gate i--; 8070Sstevel@tonic-gate 8080Sstevel@tonic-gate if (x86_cpu_freq[i] <= freq) { 8090Sstevel@tonic-gate mul = freq - x86_cpu_freq[i]; 8100Sstevel@tonic-gate 8110Sstevel@tonic-gate if (mul < delta) { 8120Sstevel@tonic-gate fixed = x86_cpu_freq[i]; 8130Sstevel@tonic-gate delta = mul; 8140Sstevel@tonic-gate } 8150Sstevel@tonic-gate 8160Sstevel@tonic-gate break; 8170Sstevel@tonic-gate } 8180Sstevel@tonic-gate 8190Sstevel@tonic-gate mul = x86_cpu_freq[i] - freq; 8200Sstevel@tonic-gate 8210Sstevel@tonic-gate if (mul < delta) { 8220Sstevel@tonic-gate fixed = x86_cpu_freq[i]; 8230Sstevel@tonic-gate delta = mul; 8240Sstevel@tonic-gate } 8250Sstevel@tonic-gate } 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate /* 8280Sstevel@tonic-gate * Set a reasonable maximum for how much to correct the measured 8290Sstevel@tonic-gate * result by. This check is here to prevent the adjustment made 8300Sstevel@tonic-gate * by this function from being more harm than good. It is entirely 8310Sstevel@tonic-gate * possible that in the future parts will be made that are not 8320Sstevel@tonic-gate * integral multiples of 66 or 50 in clock frequency or that 8330Sstevel@tonic-gate * someone may overclock a part to some odd frequency. If the 8340Sstevel@tonic-gate * measured value is farther from the corrected value than 8350Sstevel@tonic-gate * allowed, then assume the corrected value is in error and use 8360Sstevel@tonic-gate * the measured value. 8370Sstevel@tonic-gate */ 8380Sstevel@tonic-gate if (6 < delta) 8390Sstevel@tonic-gate return; 8400Sstevel@tonic-gate 8410Sstevel@tonic-gate cpu_freq = (int)fixed; 8420Sstevel@tonic-gate } 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate 8450Sstevel@tonic-gate static int 8460Sstevel@tonic-gate machhztomhz(uint64_t cpu_freq_hz) 8470Sstevel@tonic-gate { 8480Sstevel@tonic-gate uint64_t cpu_mhz; 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate /* Round to nearest MHZ */ 8510Sstevel@tonic-gate cpu_mhz = (cpu_freq_hz + (MEGA_HZ / 2)) / MEGA_HZ; 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate if (cpu_mhz > INT_MAX) 8540Sstevel@tonic-gate return (0); 8550Sstevel@tonic-gate 8560Sstevel@tonic-gate return ((int)cpu_mhz); 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate 8610Sstevel@tonic-gate static int 8620Sstevel@tonic-gate mach_clkinit(int preferred_mode, int *set_mode) 8630Sstevel@tonic-gate { 8640Sstevel@tonic-gate register struct psm_ops *pops; 8650Sstevel@tonic-gate int resolution; 8660Sstevel@tonic-gate 8670Sstevel@tonic-gate pops = mach_set[0]; 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate #ifdef _SIMULATOR_SUPPORT 8700Sstevel@tonic-gate if (!simulator_run) 8710Sstevel@tonic-gate cpu_freq_hz = mach_getcpufreq(); 8720Sstevel@tonic-gate else 8730Sstevel@tonic-gate cpu_freq_hz = 40000000; /* use 40 Mhz (hack for simulator) */ 8740Sstevel@tonic-gate #else 8750Sstevel@tonic-gate cpu_freq_hz = mach_getcpufreq(); 8760Sstevel@tonic-gate #endif /* _SIMULATOR_SUPPORT */ 8770Sstevel@tonic-gate 8780Sstevel@tonic-gate cpu_freq = machhztomhz(cpu_freq_hz); 8790Sstevel@tonic-gate 8800Sstevel@tonic-gate if (!(x86_feature & X86_TSC) || (cpu_freq == 0)) 8810Sstevel@tonic-gate tsc_gethrtime_enable = 0; 8820Sstevel@tonic-gate 8830Sstevel@tonic-gate if (tsc_gethrtime_enable) { 8840Sstevel@tonic-gate tsc_hrtimeinit(cpu_freq_hz); 8850Sstevel@tonic-gate gethrtimef = tsc_gethrtime; 8860Sstevel@tonic-gate gethrtimeunscaledf = tsc_gethrtimeunscaled; 8870Sstevel@tonic-gate scalehrtimef = tsc_scalehrtime; 8880Sstevel@tonic-gate hrtime_tick = tsc_tick; 8890Sstevel@tonic-gate tsc_gethrtime_initted = 1; 8900Sstevel@tonic-gate } else { 8910Sstevel@tonic-gate if (pops->psm_hrtimeinit) 8920Sstevel@tonic-gate (*pops->psm_hrtimeinit)(); 8930Sstevel@tonic-gate gethrtimef = pops->psm_gethrtime; 8940Sstevel@tonic-gate gethrtimeunscaledf = gethrtimef; 8950Sstevel@tonic-gate /* scalehrtimef will remain dummy */ 8960Sstevel@tonic-gate } 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate mach_fixcpufreq(); 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate if (mach_ver[0] >= PSM_INFO_VER01_3) { 9010Sstevel@tonic-gate if ((preferred_mode == TIMER_ONESHOT) && 9020Sstevel@tonic-gate (tsc_gethrtime_enable)) { 9030Sstevel@tonic-gate 9040Sstevel@tonic-gate resolution = (*pops->psm_clkinit)(0); 9050Sstevel@tonic-gate if (resolution != 0) { 9060Sstevel@tonic-gate *set_mode = TIMER_ONESHOT; 9070Sstevel@tonic-gate return (resolution); 9080Sstevel@tonic-gate } 9090Sstevel@tonic-gate 9100Sstevel@tonic-gate } 9110Sstevel@tonic-gate 9120Sstevel@tonic-gate /* 9130Sstevel@tonic-gate * either periodic mode was requested or could not set to 9140Sstevel@tonic-gate * one-shot mode 9150Sstevel@tonic-gate */ 9160Sstevel@tonic-gate resolution = (*pops->psm_clkinit)(hz); 9170Sstevel@tonic-gate /* 9180Sstevel@tonic-gate * psm should be able to do periodic, so we do not check 9190Sstevel@tonic-gate * for return value of psm_clkinit here. 9200Sstevel@tonic-gate */ 9210Sstevel@tonic-gate *set_mode = TIMER_PERIODIC; 9220Sstevel@tonic-gate return (resolution); 9230Sstevel@tonic-gate } else { 9240Sstevel@tonic-gate /* 9250Sstevel@tonic-gate * PSMI interface prior to PSMI_3 does not define a return 9260Sstevel@tonic-gate * value for psm_clkinit, so the return value is ignored. 9270Sstevel@tonic-gate */ 9280Sstevel@tonic-gate (void) (*pops->psm_clkinit)(hz); 9290Sstevel@tonic-gate *set_mode = TIMER_PERIODIC; 9300Sstevel@tonic-gate return (nsec_per_tick); 9310Sstevel@tonic-gate } 9320Sstevel@tonic-gate } 9330Sstevel@tonic-gate 9340Sstevel@tonic-gate static int 9350Sstevel@tonic-gate mach_softlvl_to_vect(register int ipl) 9360Sstevel@tonic-gate { 9370Sstevel@tonic-gate register int softvect; 9380Sstevel@tonic-gate register struct psm_ops *pops; 9390Sstevel@tonic-gate 9400Sstevel@tonic-gate pops = mach_set[0]; 9410Sstevel@tonic-gate 9420Sstevel@tonic-gate /* check for null handler for set soft interrupt call */ 9430Sstevel@tonic-gate if (pops->psm_set_softintr == NULL) { 9440Sstevel@tonic-gate setsoftint = set_pending; 9450Sstevel@tonic-gate return (PSM_SV_SOFTWARE); 9460Sstevel@tonic-gate } 9470Sstevel@tonic-gate 9480Sstevel@tonic-gate softvect = (*pops->psm_softlvl_to_irq)(ipl); 9490Sstevel@tonic-gate /* check for hardware scheme */ 9500Sstevel@tonic-gate if (softvect > PSM_SV_SOFTWARE) { 9510Sstevel@tonic-gate setsoftint = pops->psm_set_softintr; 9520Sstevel@tonic-gate return (softvect); 9530Sstevel@tonic-gate } 9540Sstevel@tonic-gate 9550Sstevel@tonic-gate if (softvect == PSM_SV_SOFTWARE) 9560Sstevel@tonic-gate setsoftint = set_pending; 9570Sstevel@tonic-gate else /* hardware and software mixed scheme */ 9580Sstevel@tonic-gate setsoftint = mach_set_softintr; 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate return (PSM_SV_SOFTWARE); 9610Sstevel@tonic-gate } 9620Sstevel@tonic-gate 9630Sstevel@tonic-gate static void 9640Sstevel@tonic-gate mach_set_softintr(register int ipl) 9650Sstevel@tonic-gate { 9660Sstevel@tonic-gate register struct psm_ops *pops; 9670Sstevel@tonic-gate 9680Sstevel@tonic-gate /* set software pending bits */ 9690Sstevel@tonic-gate set_pending(ipl); 9700Sstevel@tonic-gate 9710Sstevel@tonic-gate /* check if dosoftint will be called at the end of intr */ 9720Sstevel@tonic-gate if (CPU_ON_INTR(CPU) || (curthread->t_intr)) 9730Sstevel@tonic-gate return; 9740Sstevel@tonic-gate 9750Sstevel@tonic-gate /* invoke hardware interrupt */ 9760Sstevel@tonic-gate pops = mach_set[0]; 9770Sstevel@tonic-gate (*pops->psm_set_softintr)(ipl); 9780Sstevel@tonic-gate } 9790Sstevel@tonic-gate 9800Sstevel@tonic-gate static void 9810Sstevel@tonic-gate mach_cpu_start(register int cpun) 9820Sstevel@tonic-gate { 9830Sstevel@tonic-gate register struct psm_ops *pops; 9840Sstevel@tonic-gate int i; 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate pops = mach_set[0]; 9870Sstevel@tonic-gate 9880Sstevel@tonic-gate (*pops->psm_cpu_start)(cpun, rm_platter_va); 9890Sstevel@tonic-gate 9900Sstevel@tonic-gate /* wait for the auxillary cpu to be ready */ 9910Sstevel@tonic-gate for (i = 20000; i; i--) { 9920Sstevel@tonic-gate if (cpu[cpun]->cpu_flags & CPU_READY) 9930Sstevel@tonic-gate return; 9940Sstevel@tonic-gate drv_usecwait(100); 9950Sstevel@tonic-gate } 9960Sstevel@tonic-gate } 9970Sstevel@tonic-gate 9980Sstevel@tonic-gate /*ARGSUSED*/ 9990Sstevel@tonic-gate static int 10000Sstevel@tonic-gate mach_translate_irq(dev_info_t *dip, int irqno) 10010Sstevel@tonic-gate { 10020Sstevel@tonic-gate return (irqno); /* default to NO translation */ 10030Sstevel@tonic-gate } 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate static timestruc_t 10060Sstevel@tonic-gate mach_tod_get(void) 10070Sstevel@tonic-gate { 10080Sstevel@tonic-gate timestruc_t ts; 10090Sstevel@tonic-gate todinfo_t tod; 10100Sstevel@tonic-gate static int mach_range_warn = 1; /* warn only once */ 10110Sstevel@tonic-gate 10120Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 10130Sstevel@tonic-gate 10140Sstevel@tonic-gate /* The year returned from is the last 2 digit only */ 10150Sstevel@tonic-gate if ((*psm_todgetf)(&tod)) { 10160Sstevel@tonic-gate ts.tv_sec = 0; 10170Sstevel@tonic-gate ts.tv_nsec = 0; 10180Sstevel@tonic-gate tod_fault_reset(); 10190Sstevel@tonic-gate return (ts); 10200Sstevel@tonic-gate } 10210Sstevel@tonic-gate 10220Sstevel@tonic-gate /* assume that we wrap the rtc year back to zero at 2000 */ 10230Sstevel@tonic-gate if (tod.tod_year < 69) { 10240Sstevel@tonic-gate if (mach_range_warn && tod.tod_year > 38) { 10250Sstevel@tonic-gate cmn_err(CE_WARN, "hardware real-time clock is out " 10260Sstevel@tonic-gate "of range -- time needs to be reset"); 10270Sstevel@tonic-gate mach_range_warn = 0; 10280Sstevel@tonic-gate } 10290Sstevel@tonic-gate tod.tod_year += 100; 10300Sstevel@tonic-gate } 10310Sstevel@tonic-gate 10320Sstevel@tonic-gate /* tod_to_utc uses 1900 as base for the year */ 10330Sstevel@tonic-gate ts.tv_sec = tod_to_utc(tod) + gmt_lag; 10340Sstevel@tonic-gate ts.tv_nsec = 0; 10350Sstevel@tonic-gate 10360Sstevel@tonic-gate return (ts); 10370Sstevel@tonic-gate } 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate static void 10400Sstevel@tonic-gate mach_tod_set(timestruc_t ts) 10410Sstevel@tonic-gate { 10420Sstevel@tonic-gate todinfo_t tod = utc_to_tod(ts.tv_sec - gmt_lag); 10430Sstevel@tonic-gate 10440Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 10450Sstevel@tonic-gate 10460Sstevel@tonic-gate if (tod.tod_year >= 100) 10470Sstevel@tonic-gate tod.tod_year -= 100; 10480Sstevel@tonic-gate 10490Sstevel@tonic-gate (*psm_todsetf)(&tod); 10500Sstevel@tonic-gate } 10510Sstevel@tonic-gate 10520Sstevel@tonic-gate static void 10530Sstevel@tonic-gate mach_notify_error(int level, char *errmsg) 10540Sstevel@tonic-gate { 10550Sstevel@tonic-gate /* 10560Sstevel@tonic-gate * SL_FATAL is pass in once panicstr is set, deliver it 10570Sstevel@tonic-gate * as CE_PANIC. Also, translate SL_ codes back to CE_ 10580Sstevel@tonic-gate * codes for the psmi handler 10590Sstevel@tonic-gate */ 10600Sstevel@tonic-gate if (level & SL_FATAL) 10610Sstevel@tonic-gate (*notify_error)(CE_PANIC, errmsg); 10620Sstevel@tonic-gate else if (level & SL_WARN) 10630Sstevel@tonic-gate (*notify_error)(CE_WARN, errmsg); 10640Sstevel@tonic-gate else if (level & SL_NOTE) 10650Sstevel@tonic-gate (*notify_error)(CE_NOTE, errmsg); 10660Sstevel@tonic-gate else if (level & SL_CONSOLE) 10670Sstevel@tonic-gate (*notify_error)(CE_CONT, errmsg); 10680Sstevel@tonic-gate } 10690Sstevel@tonic-gate 10700Sstevel@tonic-gate /* 10710Sstevel@tonic-gate * It provides the default basic intr_ops interface for the new DDI 10720Sstevel@tonic-gate * interrupt framework if the PSM doesn't have one. 10730Sstevel@tonic-gate * 10740Sstevel@tonic-gate * Input: 10750Sstevel@tonic-gate * dip - pointer to the dev_info structure of the requested device 10760Sstevel@tonic-gate * hdlp - pointer to the internal interrupt handle structure for the 10770Sstevel@tonic-gate * requested interrupt 10780Sstevel@tonic-gate * intr_op - opcode for this call 10790Sstevel@tonic-gate * result - pointer to the integer that will hold the result to be 10800Sstevel@tonic-gate * passed back if return value is PSM_SUCCESS 10810Sstevel@tonic-gate * 10820Sstevel@tonic-gate * Output: 10830Sstevel@tonic-gate * return value is either PSM_SUCCESS or PSM_FAILURE 10840Sstevel@tonic-gate */ 10850Sstevel@tonic-gate static int 10860Sstevel@tonic-gate mach_intr_ops(dev_info_t *dip, ddi_intr_handle_impl_t *hdlp, 10870Sstevel@tonic-gate psm_intr_op_t intr_op, int *result) 10880Sstevel@tonic-gate { 10890Sstevel@tonic-gate struct intrspec *ispec; 10900Sstevel@tonic-gate 10910Sstevel@tonic-gate switch (intr_op) { 10920Sstevel@tonic-gate case PSM_INTR_OP_CHECK_MSI: 10930Sstevel@tonic-gate *result = hdlp->ih_type & ~(DDI_INTR_TYPE_MSI | 10940Sstevel@tonic-gate DDI_INTR_TYPE_MSIX); 10950Sstevel@tonic-gate break; 10960Sstevel@tonic-gate case PSM_INTR_OP_ALLOC_VECTORS: 10970Sstevel@tonic-gate if (hdlp->ih_type == DDI_INTR_TYPE_FIXED) 10980Sstevel@tonic-gate *result = 1; 10990Sstevel@tonic-gate else 11000Sstevel@tonic-gate *result = 0; 11010Sstevel@tonic-gate break; 11020Sstevel@tonic-gate case PSM_INTR_OP_FREE_VECTORS: 11030Sstevel@tonic-gate break; 11040Sstevel@tonic-gate case PSM_INTR_OP_NAVAIL_VECTORS: 11050Sstevel@tonic-gate if (hdlp->ih_type == DDI_INTR_TYPE_FIXED) 11060Sstevel@tonic-gate *result = 1; 11070Sstevel@tonic-gate else 11080Sstevel@tonic-gate *result = 0; 11090Sstevel@tonic-gate break; 11100Sstevel@tonic-gate case PSM_INTR_OP_XLATE_VECTOR: 11110Sstevel@tonic-gate ispec = (struct intrspec *)hdlp->ih_private; 11120Sstevel@tonic-gate *result = psm_translate_irq(dip, ispec->intrspec_vec); 11130Sstevel@tonic-gate break; 11140Sstevel@tonic-gate case PSM_INTR_OP_GET_CAP: 11150Sstevel@tonic-gate *result = 0; 11160Sstevel@tonic-gate break; 11170Sstevel@tonic-gate case PSM_INTR_OP_GET_PENDING: 11180Sstevel@tonic-gate case PSM_INTR_OP_CLEAR_MASK: 11190Sstevel@tonic-gate case PSM_INTR_OP_SET_MASK: 11200Sstevel@tonic-gate case PSM_INTR_OP_GET_SHARED: 11210Sstevel@tonic-gate case PSM_INTR_OP_SET_PRI: 11220Sstevel@tonic-gate case PSM_INTR_OP_SET_CAP: 11230Sstevel@tonic-gate default: 11240Sstevel@tonic-gate return (PSM_FAILURE); 11250Sstevel@tonic-gate } 11260Sstevel@tonic-gate return (PSM_SUCCESS); 11270Sstevel@tonic-gate } 1128