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 52006Sandrei * Common Development and Distribution License (the "License"). 62006Sandrei * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 228675SVikram.Hegde@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 259652SMichael.Corcoran@Sun.COM /* 269652SMichael.Corcoran@Sun.COM * Copyright (c) 2009, Intel Corporation. 279652SMichael.Corcoran@Sun.COM * All rights reserved. 289652SMichael.Corcoran@Sun.COM */ 290Sstevel@tonic-gate 305295Srandyf #define PSMI_1_6 310Sstevel@tonic-gate #include <sys/smp_impldefs.h> 320Sstevel@tonic-gate #include <sys/psm.h> 330Sstevel@tonic-gate #include <sys/psm_modctl.h> 340Sstevel@tonic-gate #include <sys/pit.h> 350Sstevel@tonic-gate #include <sys/cmn_err.h> 360Sstevel@tonic-gate #include <sys/strlog.h> 370Sstevel@tonic-gate #include <sys/clock.h> 380Sstevel@tonic-gate #include <sys/debug.h> 390Sstevel@tonic-gate #include <sys/rtc.h> 400Sstevel@tonic-gate #include <sys/x86_archext.h> 410Sstevel@tonic-gate #include <sys/cpupart.h> 420Sstevel@tonic-gate #include <sys/cpuvar.h> 439637SRandy.Fishel@Sun.COM #include <sys/cpu_event.h> 444606Sesaxe #include <sys/cmt.h> 454481Sbholler #include <sys/cpu.h> 460Sstevel@tonic-gate #include <sys/disp.h> 470Sstevel@tonic-gate #include <sys/archsystm.h> 483446Smrj #include <sys/machsystm.h> 494481Sbholler #include <sys/sysmacros.h> 505084Sjohnlev #include <sys/memlist.h> 513446Smrj #include <sys/param.h> 523446Smrj #include <sys/promif.h> 538906SEric.Saxe@Sun.COM #include <sys/cpu_pm.h> 545084Sjohnlev #if defined(__xpv) 555084Sjohnlev #include <sys/hypervisor.h> 565084Sjohnlev #endif 57916Sschwartz #include <sys/mach_intr.h> 584481Sbholler #include <vm/hat_i86.h> 594652Scwb #include <sys/kdi_machimpl.h> 605864Sesaxe #include <sys/sdt.h> 618906SEric.Saxe@Sun.COM #include <sys/hpet.h> 629652SMichael.Corcoran@Sun.COM #include <sys/sunddi.h> 639652SMichael.Corcoran@Sun.COM #include <sys/sunndi.h> 640Sstevel@tonic-gate 650Sstevel@tonic-gate #define OFFSETOF(s, m) (size_t)(&(((s *)0)->m)) 660Sstevel@tonic-gate 670Sstevel@tonic-gate /* 680Sstevel@tonic-gate * Local function prototypes 690Sstevel@tonic-gate */ 700Sstevel@tonic-gate static int mp_disable_intr(processorid_t cpun); 710Sstevel@tonic-gate static void mp_enable_intr(processorid_t cpun); 720Sstevel@tonic-gate static void mach_init(); 730Sstevel@tonic-gate static void mach_picinit(); 740Sstevel@tonic-gate static int machhztomhz(uint64_t cpu_freq_hz); 750Sstevel@tonic-gate static uint64_t mach_getcpufreq(void); 760Sstevel@tonic-gate static void mach_fixcpufreq(void); 770Sstevel@tonic-gate static int mach_clkinit(int, int *); 780Sstevel@tonic-gate static void mach_smpinit(void); 790Sstevel@tonic-gate static int mach_softlvl_to_vect(int ipl); 800Sstevel@tonic-gate static void mach_get_platform(int owner); 810Sstevel@tonic-gate static void mach_construct_info(); 820Sstevel@tonic-gate static int mach_translate_irq(dev_info_t *dip, int irqno); 830Sstevel@tonic-gate static int mach_intr_ops(dev_info_t *, ddi_intr_handle_impl_t *, 840Sstevel@tonic-gate psm_intr_op_t, int *); 850Sstevel@tonic-gate static void mach_notify_error(int level, char *errmsg); 860Sstevel@tonic-gate static hrtime_t dummy_hrtime(void); 870Sstevel@tonic-gate static void dummy_scalehrtime(hrtime_t *); 8810797SEric.Saxe@Sun.COM static uint64_t dummy_unscalehrtime(hrtime_t); 898906SEric.Saxe@Sun.COM void cpu_idle(void); 900Sstevel@tonic-gate static void cpu_wakeup(cpu_t *, int); 915084Sjohnlev #ifndef __xpv 928906SEric.Saxe@Sun.COM void cpu_idle_mwait(void); 934481Sbholler static void cpu_wakeup_mwait(cpu_t *, int); 945084Sjohnlev #endif 959652SMichael.Corcoran@Sun.COM static int mach_cpu_create_devinfo(cpu_t *cp, dev_info_t **dipp); 969652SMichael.Corcoran@Sun.COM 970Sstevel@tonic-gate /* 980Sstevel@tonic-gate * External reference functions 990Sstevel@tonic-gate */ 1000Sstevel@tonic-gate extern void return_instr(); 1010Sstevel@tonic-gate extern uint64_t freq_tsc(uint32_t *); 1020Sstevel@tonic-gate #if defined(__i386) 1030Sstevel@tonic-gate extern uint64_t freq_notsc(uint32_t *); 1040Sstevel@tonic-gate #endif 1050Sstevel@tonic-gate extern void pc_gethrestime(timestruc_t *); 1063434Sesaxe extern int cpuid_get_coreid(cpu_t *); 1073434Sesaxe extern int cpuid_get_chipid(cpu_t *); 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate /* 1100Sstevel@tonic-gate * PSM functions initialization 1110Sstevel@tonic-gate */ 1123446Smrj void (*psm_shutdownf)(int, int) = (void (*)(int, int))return_instr; 1133446Smrj void (*psm_preshutdownf)(int, int) = (void (*)(int, int))return_instr; 1143446Smrj void (*psm_notifyf)(int) = (void (*)(int))return_instr; 1153446Smrj void (*psm_set_idle_cpuf)(int) = (void (*)(int))return_instr; 1163446Smrj void (*psm_unset_idle_cpuf)(int) = (void (*)(int))return_instr; 1170Sstevel@tonic-gate void (*psminitf)() = mach_init; 1180Sstevel@tonic-gate void (*picinitf)() = return_instr; 1190Sstevel@tonic-gate int (*clkinitf)(int, int *) = (int (*)(int, int *))return_instr; 1200Sstevel@tonic-gate int (*ap_mlsetup)() = (int (*)(void))return_instr; 1210Sstevel@tonic-gate void (*send_dirintf)() = return_instr; 1223446Smrj void (*setspl)(int) = (void (*)(int))return_instr; 1230Sstevel@tonic-gate int (*addspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr; 1240Sstevel@tonic-gate int (*delspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr; 1254652Scwb void (*kdisetsoftint)(int, struct av_softinfo *)= 1264652Scwb (void (*)(int, struct av_softinfo *))return_instr; 127999Slq150181 void (*setsoftint)(int, struct av_softinfo *)= 128999Slq150181 (void (*)(int, struct av_softinfo *))return_instr; 1290Sstevel@tonic-gate int (*slvltovect)(int) = (int (*)(int))return_instr; 1300Sstevel@tonic-gate int (*setlvl)(int, int *) = (int (*)(int, int *))return_instr; 1310Sstevel@tonic-gate void (*setlvlx)(int, int) = (void (*)(int, int))return_instr; 1320Sstevel@tonic-gate int (*psm_disable_intr)(int) = mp_disable_intr; 1330Sstevel@tonic-gate void (*psm_enable_intr)(int) = mp_enable_intr; 1340Sstevel@tonic-gate hrtime_t (*gethrtimef)(void) = dummy_hrtime; 1350Sstevel@tonic-gate hrtime_t (*gethrtimeunscaledf)(void) = dummy_hrtime; 1360Sstevel@tonic-gate void (*scalehrtimef)(hrtime_t *) = dummy_scalehrtime; 13710797SEric.Saxe@Sun.COM uint64_t (*unscalehrtimef)(hrtime_t) = dummy_unscalehrtime; 1380Sstevel@tonic-gate int (*psm_translate_irq)(dev_info_t *, int) = mach_translate_irq; 1390Sstevel@tonic-gate void (*gethrestimef)(timestruc_t *) = pc_gethrestime; 1400Sstevel@tonic-gate void (*psm_notify_error)(int, char *) = (void (*)(int, char *))NULL; 1410Sstevel@tonic-gate int (*psm_get_clockirq)(int) = NULL; 1420Sstevel@tonic-gate int (*psm_get_ipivect)(int, int) = NULL; 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate int (*psm_clkinit)(int) = NULL; 1450Sstevel@tonic-gate void (*psm_timer_reprogram)(hrtime_t) = NULL; 1460Sstevel@tonic-gate void (*psm_timer_enable)(void) = NULL; 1470Sstevel@tonic-gate void (*psm_timer_disable)(void) = NULL; 1480Sstevel@tonic-gate void (*psm_post_cyclic_setup)(void *arg) = NULL; 1490Sstevel@tonic-gate int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *, psm_intr_op_t, 1500Sstevel@tonic-gate int *) = mach_intr_ops; 1515295Srandyf int (*psm_state)(psm_state_request_t *) = (int (*)(psm_state_request_t *)) 1525295Srandyf return_instr; 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate void (*notify_error)(int, char *) = (void (*)(int, char *))return_instr; 1550Sstevel@tonic-gate void (*hrtime_tick)(void) = return_instr; 1560Sstevel@tonic-gate 1579652SMichael.Corcoran@Sun.COM int (*psm_cpu_create_devinfo)(cpu_t *, dev_info_t **) = mach_cpu_create_devinfo; 1589652SMichael.Corcoran@Sun.COM 1595084Sjohnlev /* 1605084Sjohnlev * True if the generic TSC code is our source of hrtime, rather than whatever 1615084Sjohnlev * the PSM can provide. 1625084Sjohnlev */ 1635084Sjohnlev #ifdef __xpv 1645084Sjohnlev int tsc_gethrtime_enable = 0; 1655084Sjohnlev #else 1660Sstevel@tonic-gate int tsc_gethrtime_enable = 1; 1675084Sjohnlev #endif 1680Sstevel@tonic-gate int tsc_gethrtime_initted = 0; 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate /* 1715084Sjohnlev * True if the hrtime implementation is "hires"; namely, better than microdata. 1725084Sjohnlev */ 1735084Sjohnlev int gethrtime_hires = 0; 1745084Sjohnlev 1755084Sjohnlev /* 1760Sstevel@tonic-gate * Local Static Data 1770Sstevel@tonic-gate */ 1780Sstevel@tonic-gate static struct psm_ops mach_ops; 1790Sstevel@tonic-gate static struct psm_ops *mach_set[4] = {&mach_ops, NULL, NULL, NULL}; 1800Sstevel@tonic-gate static ushort_t mach_ver[4] = {0, 0, 0, 0}; 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate /* 1838675SVikram.Hegde@Sun.COM * virtualization support for psm 1848675SVikram.Hegde@Sun.COM */ 1858675SVikram.Hegde@Sun.COM void *psm_vt_ops = NULL; 1868675SVikram.Hegde@Sun.COM /* 1873446Smrj * If non-zero, idle cpus will become "halted" when there's 1880Sstevel@tonic-gate * no work to do. 1890Sstevel@tonic-gate */ 1903446Smrj int idle_cpu_use_hlt = 1; 1910Sstevel@tonic-gate 1925084Sjohnlev #ifndef __xpv 1934481Sbholler /* 1944481Sbholler * If non-zero, idle cpus will use mwait if available to halt instead of hlt. 1954481Sbholler */ 1964481Sbholler int idle_cpu_prefer_mwait = 1; 1977716SBill.Holler@Sun.COM /* 1987716SBill.Holler@Sun.COM * Set to 0 to avoid MONITOR+CLFLUSH assertion. 1997716SBill.Holler@Sun.COM */ 2007716SBill.Holler@Sun.COM int idle_cpu_assert_cflush_monitor = 1; 2017716SBill.Holler@Sun.COM 2028906SEric.Saxe@Sun.COM /* 2038906SEric.Saxe@Sun.COM * If non-zero, idle cpus will not use power saving Deep C-States idle loop. 2048906SEric.Saxe@Sun.COM */ 2058906SEric.Saxe@Sun.COM int idle_cpu_no_deep_c = 0; 2068906SEric.Saxe@Sun.COM /* 2078906SEric.Saxe@Sun.COM * Non-power saving idle loop and wakeup pointers. 2088906SEric.Saxe@Sun.COM * Allows user to toggle Deep Idle power saving feature on/off. 2098906SEric.Saxe@Sun.COM */ 2108906SEric.Saxe@Sun.COM void (*non_deep_idle_cpu)() = cpu_idle; 2118906SEric.Saxe@Sun.COM void (*non_deep_idle_disp_enq_thread)(cpu_t *, int); 2128906SEric.Saxe@Sun.COM 2138906SEric.Saxe@Sun.COM /* 2148906SEric.Saxe@Sun.COM * Object for the kernel to access the HPET. 2158906SEric.Saxe@Sun.COM */ 2168906SEric.Saxe@Sun.COM hpet_t hpet; 2178906SEric.Saxe@Sun.COM 2188906SEric.Saxe@Sun.COM #endif /* ifndef __xpv */ 2193434Sesaxe 2203434Sesaxe /*ARGSUSED*/ 2213434Sesaxe int 2223434Sesaxe pg_plat_hw_shared(cpu_t *cp, pghw_type_t hw) 2233434Sesaxe { 2243434Sesaxe switch (hw) { 2253434Sesaxe case PGHW_IPIPE: 2263434Sesaxe if (x86_feature & (X86_HTT)) { 2273434Sesaxe /* 2283434Sesaxe * Hyper-threading is SMT 2293434Sesaxe */ 2303434Sesaxe return (1); 2313434Sesaxe } else { 2323434Sesaxe return (0); 2333434Sesaxe } 234*10947SSrihari.Venkatesan@Sun.COM case PGHW_PROCNODE: 235*10947SSrihari.Venkatesan@Sun.COM if (cpuid_get_procnodes_per_pkg(cp) > 1) 236*10947SSrihari.Venkatesan@Sun.COM return (1); 237*10947SSrihari.Venkatesan@Sun.COM else 238*10947SSrihari.Venkatesan@Sun.COM return (0); 2393434Sesaxe case PGHW_CHIP: 2403434Sesaxe if (x86_feature & (X86_CMP|X86_HTT)) 2413434Sesaxe return (1); 2423434Sesaxe else 2433434Sesaxe return (0); 2444606Sesaxe case PGHW_CACHE: 2454606Sesaxe if (cpuid_get_ncpu_sharing_last_cache(cp) > 1) 2464606Sesaxe return (1); 2474606Sesaxe else 2484606Sesaxe return (0); 2498906SEric.Saxe@Sun.COM case PGHW_POW_ACTIVE: 2508906SEric.Saxe@Sun.COM if (cpupm_domain_id(cp, CPUPM_DTYPE_ACTIVE) != (id_t)-1) 2518906SEric.Saxe@Sun.COM return (1); 2528906SEric.Saxe@Sun.COM else 2538906SEric.Saxe@Sun.COM return (0); 2548906SEric.Saxe@Sun.COM case PGHW_POW_IDLE: 2558906SEric.Saxe@Sun.COM if (cpupm_domain_id(cp, CPUPM_DTYPE_IDLE) != (id_t)-1) 2568906SEric.Saxe@Sun.COM return (1); 2578906SEric.Saxe@Sun.COM else 2588906SEric.Saxe@Sun.COM return (0); 2593434Sesaxe default: 2603434Sesaxe return (0); 2613434Sesaxe } 2623434Sesaxe } 2633434Sesaxe 2643434Sesaxe /* 2653434Sesaxe * Compare two CPUs and see if they have a pghw_type_t sharing relationship 2663434Sesaxe * If pghw_type_t is an unsupported hardware type, then return -1 2673434Sesaxe */ 2683434Sesaxe int 2693434Sesaxe pg_plat_cpus_share(cpu_t *cpu_a, cpu_t *cpu_b, pghw_type_t hw) 2700Sstevel@tonic-gate { 2713434Sesaxe id_t pgp_a, pgp_b; 2723434Sesaxe 2733434Sesaxe pgp_a = pg_plat_hw_instance_id(cpu_a, hw); 2743434Sesaxe pgp_b = pg_plat_hw_instance_id(cpu_b, hw); 2753434Sesaxe 2763434Sesaxe if (pgp_a == -1 || pgp_b == -1) 2773434Sesaxe return (-1); 2783434Sesaxe 2793434Sesaxe return (pgp_a == pgp_b); 2803434Sesaxe } 2813434Sesaxe 2823434Sesaxe /* 2833434Sesaxe * Return a physical instance identifier for known hardware sharing 2843434Sesaxe * relationships 2853434Sesaxe */ 2863434Sesaxe id_t 2873434Sesaxe pg_plat_hw_instance_id(cpu_t *cpu, pghw_type_t hw) 2883434Sesaxe { 2893434Sesaxe switch (hw) { 2903434Sesaxe case PGHW_IPIPE: 2913434Sesaxe return (cpuid_get_coreid(cpu)); 2924606Sesaxe case PGHW_CACHE: 2934606Sesaxe return (cpuid_get_last_lvl_cacheid(cpu)); 294*10947SSrihari.Venkatesan@Sun.COM case PGHW_PROCNODE: 295*10947SSrihari.Venkatesan@Sun.COM return (cpuid_get_procnodeid(cpu)); 2963434Sesaxe case PGHW_CHIP: 2973434Sesaxe return (cpuid_get_chipid(cpu)); 2988906SEric.Saxe@Sun.COM case PGHW_POW_ACTIVE: 2998906SEric.Saxe@Sun.COM return (cpupm_domain_id(cpu, CPUPM_DTYPE_ACTIVE)); 3008906SEric.Saxe@Sun.COM case PGHW_POW_IDLE: 3018906SEric.Saxe@Sun.COM return (cpupm_domain_id(cpu, CPUPM_DTYPE_IDLE)); 3023434Sesaxe default: 3033434Sesaxe return (-1); 3041228Sandrei } 3053434Sesaxe } 3060Sstevel@tonic-gate 3078906SEric.Saxe@Sun.COM /* 3088906SEric.Saxe@Sun.COM * Express preference for optimizing for sharing relationship 3098906SEric.Saxe@Sun.COM * hw1 vs hw2 3108906SEric.Saxe@Sun.COM */ 3118906SEric.Saxe@Sun.COM pghw_type_t 3128906SEric.Saxe@Sun.COM pg_plat_hw_rank(pghw_type_t hw1, pghw_type_t hw2) 3133434Sesaxe { 3148906SEric.Saxe@Sun.COM int i, rank1, rank2; 3158906SEric.Saxe@Sun.COM 3163434Sesaxe static pghw_type_t hw_hier[] = { 3173434Sesaxe PGHW_IPIPE, 3184606Sesaxe PGHW_CACHE, 319*10947SSrihari.Venkatesan@Sun.COM PGHW_PROCNODE, 3203434Sesaxe PGHW_CHIP, 3218906SEric.Saxe@Sun.COM PGHW_POW_IDLE, 3228906SEric.Saxe@Sun.COM PGHW_POW_ACTIVE, 3233434Sesaxe PGHW_NUM_COMPONENTS 3243434Sesaxe }; 3253434Sesaxe 3263434Sesaxe for (i = 0; hw_hier[i] != PGHW_NUM_COMPONENTS; i++) { 3278906SEric.Saxe@Sun.COM if (hw_hier[i] == hw1) 3288906SEric.Saxe@Sun.COM rank1 = i; 3298906SEric.Saxe@Sun.COM if (hw_hier[i] == hw2) 3308906SEric.Saxe@Sun.COM rank2 = i; 3313434Sesaxe } 3328906SEric.Saxe@Sun.COM 3338906SEric.Saxe@Sun.COM if (rank1 > rank2) 3348906SEric.Saxe@Sun.COM return (hw1); 3358906SEric.Saxe@Sun.COM else 3368906SEric.Saxe@Sun.COM return (hw2); 3373434Sesaxe } 3383434Sesaxe 3394606Sesaxe /* 3408906SEric.Saxe@Sun.COM * Override the default CMT dispatcher policy for the specified 3418906SEric.Saxe@Sun.COM * hardware sharing relationship 3424606Sesaxe */ 3438906SEric.Saxe@Sun.COM pg_cmt_policy_t 3448906SEric.Saxe@Sun.COM pg_plat_cmt_policy(pghw_type_t hw) 3454606Sesaxe { 3468906SEric.Saxe@Sun.COM /* 3478906SEric.Saxe@Sun.COM * For shared caches, also load balance across them to 3488906SEric.Saxe@Sun.COM * maximize aggregate cache capacity 3498906SEric.Saxe@Sun.COM */ 3508906SEric.Saxe@Sun.COM switch (hw) { 3518906SEric.Saxe@Sun.COM case PGHW_CACHE: 3528906SEric.Saxe@Sun.COM return (CMT_BALANCE|CMT_AFFINITY); 3538906SEric.Saxe@Sun.COM default: 3548906SEric.Saxe@Sun.COM return (CMT_NO_POLICY); 3558906SEric.Saxe@Sun.COM } 3564606Sesaxe } 3574606Sesaxe 3583434Sesaxe id_t 3593434Sesaxe pg_plat_get_core_id(cpu_t *cpu) 3603434Sesaxe { 3613434Sesaxe return ((id_t)cpuid_get_coreid(cpu)); 3623434Sesaxe } 3633434Sesaxe 3643434Sesaxe void 3653434Sesaxe cmp_set_nosteal_interval(void) 3663434Sesaxe { 3673434Sesaxe /* Set the nosteal interval (used by disp_getbest()) to 100us */ 3683434Sesaxe nosteal_nsec = 100000UL; 3690Sstevel@tonic-gate } 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate /* 3720Sstevel@tonic-gate * Routine to ensure initial callers to hrtime gets 0 as return 3730Sstevel@tonic-gate */ 3740Sstevel@tonic-gate static hrtime_t 3750Sstevel@tonic-gate dummy_hrtime(void) 3760Sstevel@tonic-gate { 3770Sstevel@tonic-gate return (0); 3780Sstevel@tonic-gate } 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate /* ARGSUSED */ 3810Sstevel@tonic-gate static void 3820Sstevel@tonic-gate dummy_scalehrtime(hrtime_t *ticks) 3830Sstevel@tonic-gate {} 3840Sstevel@tonic-gate 38510797SEric.Saxe@Sun.COM static uint64_t 38610797SEric.Saxe@Sun.COM dummy_unscalehrtime(hrtime_t nsecs) 38710797SEric.Saxe@Sun.COM { 38810797SEric.Saxe@Sun.COM return ((uint64_t)nsecs); 38910797SEric.Saxe@Sun.COM } 39010797SEric.Saxe@Sun.COM 3910Sstevel@tonic-gate /* 3928906SEric.Saxe@Sun.COM * Supports Deep C-State power saving idle loop. 3938906SEric.Saxe@Sun.COM */ 3948906SEric.Saxe@Sun.COM void 3958906SEric.Saxe@Sun.COM cpu_idle_adaptive(void) 3968906SEric.Saxe@Sun.COM { 3978906SEric.Saxe@Sun.COM (*CPU->cpu_m.mcpu_idle_cpu)(); 3988906SEric.Saxe@Sun.COM } 3998906SEric.Saxe@Sun.COM 4009637SRandy.Fishel@Sun.COM /* 4019637SRandy.Fishel@Sun.COM * Function called by CPU idle notification framework to check whether CPU 4029637SRandy.Fishel@Sun.COM * has been awakened. It will be called with interrupt disabled. 4039637SRandy.Fishel@Sun.COM * If CPU has been awakened, call cpu_idle_exit() to notify CPU idle 4049637SRandy.Fishel@Sun.COM * notification framework. 4059637SRandy.Fishel@Sun.COM */ 4069637SRandy.Fishel@Sun.COM /*ARGSUSED*/ 4079637SRandy.Fishel@Sun.COM static void 4089637SRandy.Fishel@Sun.COM cpu_idle_check_wakeup(void *arg) 4098906SEric.Saxe@Sun.COM { 4109637SRandy.Fishel@Sun.COM /* 4119637SRandy.Fishel@Sun.COM * Toggle interrupt flag to detect pending interrupts. 4129637SRandy.Fishel@Sun.COM * If interrupt happened, do_interrupt() will notify CPU idle 4139637SRandy.Fishel@Sun.COM * notification framework so no need to call cpu_idle_exit() here. 4149637SRandy.Fishel@Sun.COM */ 4159637SRandy.Fishel@Sun.COM sti(); 4169637SRandy.Fishel@Sun.COM SMT_PAUSE(); 4179637SRandy.Fishel@Sun.COM cli(); 4188906SEric.Saxe@Sun.COM } 4198906SEric.Saxe@Sun.COM 4208906SEric.Saxe@Sun.COM /* 4219637SRandy.Fishel@Sun.COM * Idle the present CPU until wakened via an interrupt 4220Sstevel@tonic-gate */ 4238906SEric.Saxe@Sun.COM void 4243446Smrj cpu_idle(void) 4250Sstevel@tonic-gate { 4260Sstevel@tonic-gate cpu_t *cpup = CPU; 4278408SEric.Saxe@Sun.COM processorid_t cpu_sid = cpup->cpu_seqid; 428711Sesaxe cpupart_t *cp = cpup->cpu_part; 4290Sstevel@tonic-gate int hset_update = 1; 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate /* 4320Sstevel@tonic-gate * If this CPU is online, and there's multiple CPUs 4330Sstevel@tonic-gate * in the system, then we should notate our halting 4340Sstevel@tonic-gate * by adding ourselves to the partition's halted CPU 4350Sstevel@tonic-gate * bitmap. This allows other CPUs to find/awaken us when 4360Sstevel@tonic-gate * work becomes available. 4370Sstevel@tonic-gate */ 4380Sstevel@tonic-gate if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1) 4390Sstevel@tonic-gate hset_update = 0; 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate /* 4428408SEric.Saxe@Sun.COM * Add ourselves to the partition's halted CPUs bitmap 4430Sstevel@tonic-gate * and set our HALTED flag, if necessary. 4440Sstevel@tonic-gate * 445711Sesaxe * When a thread becomes runnable, it is placed on the queue 4468408SEric.Saxe@Sun.COM * and then the halted CPU bitmap is checked to determine who 4479637SRandy.Fishel@Sun.COM * (if anyone) should be awakened. We therefore need to first 4488408SEric.Saxe@Sun.COM * add ourselves to the bitmap, and and then check if there 4498408SEric.Saxe@Sun.COM * is any work available. The order is important to prevent a race 4508408SEric.Saxe@Sun.COM * that can lead to work languishing on a run queue somewhere while 4518408SEric.Saxe@Sun.COM * this CPU remains halted. 4528408SEric.Saxe@Sun.COM * 4538408SEric.Saxe@Sun.COM * Either the producing CPU will see we're halted and will awaken us, 4548408SEric.Saxe@Sun.COM * or this CPU will see the work available in disp_anywork(). 455711Sesaxe * 4560Sstevel@tonic-gate * Note that memory barriers after updating the HALTED flag 4578408SEric.Saxe@Sun.COM * are not necessary since an atomic operation (updating the bitset) 4580Sstevel@tonic-gate * immediately follows. On x86 the atomic operation acts as a 4590Sstevel@tonic-gate * memory barrier for the update of cpu_disp_flags. 4600Sstevel@tonic-gate */ 4610Sstevel@tonic-gate if (hset_update) { 4620Sstevel@tonic-gate cpup->cpu_disp_flags |= CPU_DISP_HALTED; 4638408SEric.Saxe@Sun.COM bitset_atomic_add(&cp->cp_haltset, cpu_sid); 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate /* 4670Sstevel@tonic-gate * Check to make sure there's really nothing to do. 468711Sesaxe * Work destined for this CPU may become available after 469711Sesaxe * this check. We'll be notified through the clearing of our 4708408SEric.Saxe@Sun.COM * bit in the halted CPU bitmap, and a poke. 4710Sstevel@tonic-gate */ 4720Sstevel@tonic-gate if (disp_anywork()) { 4730Sstevel@tonic-gate if (hset_update) { 4740Sstevel@tonic-gate cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 4758408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid); 4760Sstevel@tonic-gate } 477711Sesaxe return; 478711Sesaxe } 479711Sesaxe 480711Sesaxe /* 481711Sesaxe * We're on our way to being halted. 482711Sesaxe * 483711Sesaxe * Disable interrupts now, so that we'll awaken immediately 484711Sesaxe * after halting if someone tries to poke us between now and 485711Sesaxe * the time we actually halt. 486711Sesaxe * 487711Sesaxe * We check for the presence of our bit after disabling interrupts. 488711Sesaxe * If it's cleared, we'll return. If the bit is cleared after 489711Sesaxe * we check then the poke will pop us out of the halted state. 490711Sesaxe * 491711Sesaxe * This means that the ordering of the poke and the clearing 492711Sesaxe * of the bit by cpu_wakeup is important. 493711Sesaxe * cpu_wakeup() must clear, then poke. 4943446Smrj * cpu_idle() must disable interrupts, then check for the bit. 495711Sesaxe */ 496711Sesaxe cli(); 497711Sesaxe 4988408SEric.Saxe@Sun.COM if (hset_update && bitset_in_set(&cp->cp_haltset, cpu_sid) == 0) { 499711Sesaxe cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 500711Sesaxe sti(); 501711Sesaxe return; 502711Sesaxe } 503711Sesaxe 504711Sesaxe /* 505711Sesaxe * The check for anything locally runnable is here for performance 506711Sesaxe * and isn't needed for correctness. disp_nrunnable ought to be 507711Sesaxe * in our cache still, so it's inexpensive to check, and if there 508711Sesaxe * is anything runnable we won't have to wait for the poke. 509711Sesaxe */ 510711Sesaxe if (cpup->cpu_disp->disp_nrunnable != 0) { 511711Sesaxe if (hset_update) { 512711Sesaxe cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 5138408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid); 514711Sesaxe } 5150Sstevel@tonic-gate sti(); 5160Sstevel@tonic-gate return; 5170Sstevel@tonic-gate } 5180Sstevel@tonic-gate 5199637SRandy.Fishel@Sun.COM if (cpu_idle_enter(IDLE_STATE_C1, 0, 5209637SRandy.Fishel@Sun.COM cpu_idle_check_wakeup, NULL) == 0) { 5219637SRandy.Fishel@Sun.COM mach_cpu_idle(); 5229637SRandy.Fishel@Sun.COM cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE); 5239637SRandy.Fishel@Sun.COM } 5247006Srv207048 5250Sstevel@tonic-gate /* 5260Sstevel@tonic-gate * We're no longer halted 5270Sstevel@tonic-gate */ 5280Sstevel@tonic-gate if (hset_update) { 5290Sstevel@tonic-gate cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 5308408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid); 5310Sstevel@tonic-gate } 5320Sstevel@tonic-gate } 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate /* 5360Sstevel@tonic-gate * If "cpu" is halted, then wake it up clearing its halted bit in advance. 5370Sstevel@tonic-gate * Otherwise, see if other CPUs in the cpu partition are halted and need to 5380Sstevel@tonic-gate * be woken up so that they can steal the thread we placed on this CPU. 5390Sstevel@tonic-gate * This function is only used on MP systems. 5400Sstevel@tonic-gate */ 5410Sstevel@tonic-gate static void 5420Sstevel@tonic-gate cpu_wakeup(cpu_t *cpu, int bound) 5430Sstevel@tonic-gate { 5440Sstevel@tonic-gate uint_t cpu_found; 5458408SEric.Saxe@Sun.COM processorid_t cpu_sid; 5460Sstevel@tonic-gate cpupart_t *cp; 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate cp = cpu->cpu_part; 5498408SEric.Saxe@Sun.COM cpu_sid = cpu->cpu_seqid; 5508408SEric.Saxe@Sun.COM if (bitset_in_set(&cp->cp_haltset, cpu_sid)) { 5510Sstevel@tonic-gate /* 5520Sstevel@tonic-gate * Clear the halted bit for that CPU since it will be 5530Sstevel@tonic-gate * poked in a moment. 5540Sstevel@tonic-gate */ 5558408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid); 5560Sstevel@tonic-gate /* 5570Sstevel@tonic-gate * We may find the current CPU present in the halted cpuset 5580Sstevel@tonic-gate * if we're in the context of an interrupt that occurred 5593446Smrj * before we had a chance to clear our bit in cpu_idle(). 5600Sstevel@tonic-gate * Poking ourself is obviously unnecessary, since if 5610Sstevel@tonic-gate * we're here, we're not halted. 5620Sstevel@tonic-gate */ 5630Sstevel@tonic-gate if (cpu != CPU) 5640Sstevel@tonic-gate poke_cpu(cpu->cpu_id); 5650Sstevel@tonic-gate return; 5660Sstevel@tonic-gate } else { 5670Sstevel@tonic-gate /* 5680Sstevel@tonic-gate * This cpu isn't halted, but it's idle or undergoing a 5690Sstevel@tonic-gate * context switch. No need to awaken anyone else. 5700Sstevel@tonic-gate */ 5710Sstevel@tonic-gate if (cpu->cpu_thread == cpu->cpu_idle_thread || 5720Sstevel@tonic-gate cpu->cpu_disp_flags & CPU_DISP_DONTSTEAL) 5730Sstevel@tonic-gate return; 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate /* 5778408SEric.Saxe@Sun.COM * No need to wake up other CPUs if this is for a bound thread. 5780Sstevel@tonic-gate */ 5790Sstevel@tonic-gate if (bound) 5800Sstevel@tonic-gate return; 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate /* 5838408SEric.Saxe@Sun.COM * The CPU specified for wakeup isn't currently halted, so check 5848408SEric.Saxe@Sun.COM * to see if there are any other halted CPUs in the partition, 5858408SEric.Saxe@Sun.COM * and if there are then awaken one. 5860Sstevel@tonic-gate */ 5870Sstevel@tonic-gate do { 5888408SEric.Saxe@Sun.COM cpu_found = bitset_find(&cp->cp_haltset); 5898408SEric.Saxe@Sun.COM if (cpu_found == (uint_t)-1) 5900Sstevel@tonic-gate return; 5918408SEric.Saxe@Sun.COM } while (bitset_atomic_test_and_del(&cp->cp_haltset, cpu_found) < 0); 5920Sstevel@tonic-gate 5938408SEric.Saxe@Sun.COM if (cpu_found != CPU->cpu_seqid) { 5948408SEric.Saxe@Sun.COM poke_cpu(cpu_seq[cpu_found]->cpu_id); 5958408SEric.Saxe@Sun.COM } 5960Sstevel@tonic-gate } 5970Sstevel@tonic-gate 5985084Sjohnlev #ifndef __xpv 5994481Sbholler /* 6009637SRandy.Fishel@Sun.COM * Function called by CPU idle notification framework to check whether CPU 6019637SRandy.Fishel@Sun.COM * has been awakened. It will be called with interrupt disabled. 6029637SRandy.Fishel@Sun.COM * If CPU has been awakened, call cpu_idle_exit() to notify CPU idle 6039637SRandy.Fishel@Sun.COM * notification framework. 6049637SRandy.Fishel@Sun.COM */ 6059637SRandy.Fishel@Sun.COM static void 6069637SRandy.Fishel@Sun.COM cpu_idle_mwait_check_wakeup(void *arg) 6079637SRandy.Fishel@Sun.COM { 6089637SRandy.Fishel@Sun.COM volatile uint32_t *mcpu_mwait = (volatile uint32_t *)arg; 6099637SRandy.Fishel@Sun.COM 6109637SRandy.Fishel@Sun.COM ASSERT(arg != NULL); 6119637SRandy.Fishel@Sun.COM if (*mcpu_mwait != MWAIT_HALTED) { 6129637SRandy.Fishel@Sun.COM /* 6139637SRandy.Fishel@Sun.COM * CPU has been awakened, notify CPU idle notification system. 6149637SRandy.Fishel@Sun.COM */ 6159637SRandy.Fishel@Sun.COM cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE); 6169637SRandy.Fishel@Sun.COM } else { 6179637SRandy.Fishel@Sun.COM /* 6189637SRandy.Fishel@Sun.COM * Toggle interrupt flag to detect pending interrupts. 6199637SRandy.Fishel@Sun.COM * If interrupt happened, do_interrupt() will notify CPU idle 6209637SRandy.Fishel@Sun.COM * notification framework so no need to call cpu_idle_exit() 6219637SRandy.Fishel@Sun.COM * here. 6229637SRandy.Fishel@Sun.COM */ 6239637SRandy.Fishel@Sun.COM sti(); 6249637SRandy.Fishel@Sun.COM SMT_PAUSE(); 6259637SRandy.Fishel@Sun.COM cli(); 6269637SRandy.Fishel@Sun.COM } 6279637SRandy.Fishel@Sun.COM } 6289637SRandy.Fishel@Sun.COM 6299637SRandy.Fishel@Sun.COM /* 6309637SRandy.Fishel@Sun.COM * Idle the present CPU until awakened via touching its monitored line 6314481Sbholler */ 6328906SEric.Saxe@Sun.COM void 6334481Sbholler cpu_idle_mwait(void) 6344481Sbholler { 6354481Sbholler volatile uint32_t *mcpu_mwait = CPU->cpu_m.mcpu_mwait; 6364481Sbholler cpu_t *cpup = CPU; 6378408SEric.Saxe@Sun.COM processorid_t cpu_sid = cpup->cpu_seqid; 6384481Sbholler cpupart_t *cp = cpup->cpu_part; 6394481Sbholler int hset_update = 1; 6404481Sbholler 6414481Sbholler /* 6428906SEric.Saxe@Sun.COM * Set our mcpu_mwait here, so we can tell if anyone tries to 6434481Sbholler * wake us between now and when we call mwait. No other cpu will 6448408SEric.Saxe@Sun.COM * attempt to set our mcpu_mwait until we add ourself to the halted 6458408SEric.Saxe@Sun.COM * CPU bitmap. 6464481Sbholler */ 6474481Sbholler *mcpu_mwait = MWAIT_HALTED; 6484481Sbholler 6494481Sbholler /* 6504481Sbholler * If this CPU is online, and there's multiple CPUs 6518906SEric.Saxe@Sun.COM * in the system, then we should note our halting 6524481Sbholler * by adding ourselves to the partition's halted CPU 6534481Sbholler * bitmap. This allows other CPUs to find/awaken us when 6544481Sbholler * work becomes available. 6554481Sbholler */ 6564481Sbholler if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1) 6574481Sbholler hset_update = 0; 6584481Sbholler 6594481Sbholler /* 6608408SEric.Saxe@Sun.COM * Add ourselves to the partition's halted CPUs bitmap 6614481Sbholler * and set our HALTED flag, if necessary. 6624481Sbholler * 6634481Sbholler * When a thread becomes runnable, it is placed on the queue 6648408SEric.Saxe@Sun.COM * and then the halted CPU bitmap is checked to determine who 6658906SEric.Saxe@Sun.COM * (if anyone) should be awakened. We therefore need to first 6668408SEric.Saxe@Sun.COM * add ourselves to the bitmap, and and then check if there 6674481Sbholler * is any work available. 6684481Sbholler * 6694481Sbholler * Note that memory barriers after updating the HALTED flag 6704481Sbholler * are not necessary since an atomic operation (updating the bitmap) 6714481Sbholler * immediately follows. On x86 the atomic operation acts as a 6724481Sbholler * memory barrier for the update of cpu_disp_flags. 6734481Sbholler */ 6744481Sbholler if (hset_update) { 6754481Sbholler cpup->cpu_disp_flags |= CPU_DISP_HALTED; 6768408SEric.Saxe@Sun.COM bitset_atomic_add(&cp->cp_haltset, cpu_sid); 6774481Sbholler } 6784481Sbholler 6794481Sbholler /* 6804481Sbholler * Check to make sure there's really nothing to do. 6814481Sbholler * Work destined for this CPU may become available after 6824481Sbholler * this check. We'll be notified through the clearing of our 6838408SEric.Saxe@Sun.COM * bit in the halted CPU bitmap, and a write to our mcpu_mwait. 6844481Sbholler * 6854481Sbholler * disp_anywork() checks disp_nrunnable, so we do not have to later. 6864481Sbholler */ 6874481Sbholler if (disp_anywork()) { 6884481Sbholler if (hset_update) { 6894481Sbholler cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 6908408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid); 6914481Sbholler } 6924481Sbholler return; 6934481Sbholler } 6944481Sbholler 6954481Sbholler /* 6964481Sbholler * We're on our way to being halted. 6974481Sbholler * To avoid a lost wakeup, arm the monitor before checking if another 6984481Sbholler * cpu wrote to mcpu_mwait to wake us up. 6994481Sbholler */ 7004481Sbholler i86_monitor(mcpu_mwait, 0, 0); 7014481Sbholler if (*mcpu_mwait == MWAIT_HALTED) { 7029637SRandy.Fishel@Sun.COM if (cpu_idle_enter(IDLE_STATE_C1, 0, 7039637SRandy.Fishel@Sun.COM cpu_idle_mwait_check_wakeup, (void *)mcpu_mwait) == 0) { 7049637SRandy.Fishel@Sun.COM if (*mcpu_mwait == MWAIT_HALTED) { 7059637SRandy.Fishel@Sun.COM i86_mwait(0, 0); 7069637SRandy.Fishel@Sun.COM } 7079637SRandy.Fishel@Sun.COM cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE); 7089637SRandy.Fishel@Sun.COM } 7094481Sbholler } 7104481Sbholler 7114481Sbholler /* 7124481Sbholler * We're no longer halted 7134481Sbholler */ 7144481Sbholler if (hset_update) { 7154481Sbholler cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 7168408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid); 7174481Sbholler } 7184481Sbholler } 7194481Sbholler 7204481Sbholler /* 7214481Sbholler * If "cpu" is halted in mwait, then wake it up clearing its halted bit in 7224481Sbholler * advance. Otherwise, see if other CPUs in the cpu partition are halted and 7234481Sbholler * need to be woken up so that they can steal the thread we placed on this CPU. 7244481Sbholler * This function is only used on MP systems. 7254481Sbholler */ 7264481Sbholler static void 7274481Sbholler cpu_wakeup_mwait(cpu_t *cp, int bound) 7284481Sbholler { 7294481Sbholler cpupart_t *cpu_part; 7304481Sbholler uint_t cpu_found; 7318408SEric.Saxe@Sun.COM processorid_t cpu_sid; 7324481Sbholler 7334481Sbholler cpu_part = cp->cpu_part; 7348408SEric.Saxe@Sun.COM cpu_sid = cp->cpu_seqid; 7354481Sbholler 7364481Sbholler /* 7374481Sbholler * Clear the halted bit for that CPU since it will be woken up 7384481Sbholler * in a moment. 7394481Sbholler */ 7408408SEric.Saxe@Sun.COM if (bitset_in_set(&cpu_part->cp_haltset, cpu_sid)) { 7414481Sbholler /* 7424481Sbholler * Clear the halted bit for that CPU since it will be 7434481Sbholler * poked in a moment. 7444481Sbholler */ 7458408SEric.Saxe@Sun.COM bitset_atomic_del(&cpu_part->cp_haltset, cpu_sid); 7464481Sbholler /* 7474481Sbholler * We may find the current CPU present in the halted cpuset 7484481Sbholler * if we're in the context of an interrupt that occurred 7494481Sbholler * before we had a chance to clear our bit in cpu_idle(). 7504481Sbholler * Waking ourself is obviously unnecessary, since if 7514481Sbholler * we're here, we're not halted. 7524481Sbholler * 7534481Sbholler * monitor/mwait wakeup via writing to our cache line is 7544481Sbholler * harmless and less expensive than always checking if we 7554481Sbholler * are waking ourself which is an uncommon case. 7564481Sbholler */ 7574481Sbholler MWAIT_WAKEUP(cp); /* write to monitored line */ 7584481Sbholler return; 7594481Sbholler } else { 7604481Sbholler /* 7614481Sbholler * This cpu isn't halted, but it's idle or undergoing a 7624481Sbholler * context switch. No need to awaken anyone else. 7634481Sbholler */ 7644481Sbholler if (cp->cpu_thread == cp->cpu_idle_thread || 7654481Sbholler cp->cpu_disp_flags & CPU_DISP_DONTSTEAL) 7664481Sbholler return; 7674481Sbholler } 7684481Sbholler 7694481Sbholler /* 7704481Sbholler * No need to wake up other CPUs if the thread we just enqueued 7714481Sbholler * is bound. 7724481Sbholler */ 7738408SEric.Saxe@Sun.COM if (bound || ncpus == 1) 7744481Sbholler return; 7754481Sbholler 7764481Sbholler /* 7774481Sbholler * See if there's any other halted CPUs. If there are, then 7784481Sbholler * select one, and awaken it. 7794481Sbholler * It's possible that after we find a CPU, somebody else 7804481Sbholler * will awaken it before we get the chance. 7814481Sbholler * In that case, look again. 7824481Sbholler */ 7834481Sbholler do { 7848408SEric.Saxe@Sun.COM cpu_found = bitset_find(&cpu_part->cp_haltset); 7858408SEric.Saxe@Sun.COM if (cpu_found == (uint_t)-1) 7864481Sbholler return; 7878408SEric.Saxe@Sun.COM } while (bitset_atomic_test_and_del(&cpu_part->cp_haltset, 7888408SEric.Saxe@Sun.COM cpu_found) < 0); 7894481Sbholler 7904481Sbholler /* 7918408SEric.Saxe@Sun.COM * Do not check if cpu_found is ourself as monitor/mwait 7928408SEric.Saxe@Sun.COM * wakeup is cheap. 7934481Sbholler */ 7948408SEric.Saxe@Sun.COM MWAIT_WAKEUP(cpu_seq[cpu_found]); /* write to monitored line */ 7954481Sbholler } 7968408SEric.Saxe@Sun.COM 7975084Sjohnlev #endif 7984481Sbholler 7993446Smrj void (*cpu_pause_handler)(volatile char *) = NULL; 8003446Smrj 8010Sstevel@tonic-gate static int 8020Sstevel@tonic-gate mp_disable_intr(int cpun) 8030Sstevel@tonic-gate { 8040Sstevel@tonic-gate /* 8050Sstevel@tonic-gate * switch to the offline cpu 8060Sstevel@tonic-gate */ 8070Sstevel@tonic-gate affinity_set(cpun); 8080Sstevel@tonic-gate /* 8090Sstevel@tonic-gate * raise ipl to just below cross call 8100Sstevel@tonic-gate */ 8119489SJoe.Bonasera@sun.com splx(XC_SYS_PIL - 1); 8120Sstevel@tonic-gate /* 8130Sstevel@tonic-gate * set base spl to prevent the next swtch to idle from 8140Sstevel@tonic-gate * lowering back to ipl 0 8150Sstevel@tonic-gate */ 8169489SJoe.Bonasera@sun.com CPU->cpu_intr_actv |= (1 << (XC_SYS_PIL - 1)); 8170Sstevel@tonic-gate set_base_spl(); 8180Sstevel@tonic-gate affinity_clear(); 8190Sstevel@tonic-gate return (DDI_SUCCESS); 8200Sstevel@tonic-gate } 8210Sstevel@tonic-gate 8220Sstevel@tonic-gate static void 8230Sstevel@tonic-gate mp_enable_intr(int cpun) 8240Sstevel@tonic-gate { 8250Sstevel@tonic-gate /* 8260Sstevel@tonic-gate * switch to the online cpu 8270Sstevel@tonic-gate */ 8280Sstevel@tonic-gate affinity_set(cpun); 8290Sstevel@tonic-gate /* 8300Sstevel@tonic-gate * clear the interrupt active mask 8310Sstevel@tonic-gate */ 8329489SJoe.Bonasera@sun.com CPU->cpu_intr_actv &= ~(1 << (XC_SYS_PIL - 1)); 8330Sstevel@tonic-gate set_base_spl(); 8340Sstevel@tonic-gate (void) spl0(); 8350Sstevel@tonic-gate affinity_clear(); 8360Sstevel@tonic-gate } 8370Sstevel@tonic-gate 8380Sstevel@tonic-gate static void 8390Sstevel@tonic-gate mach_get_platform(int owner) 8400Sstevel@tonic-gate { 8410Sstevel@tonic-gate void **srv_opsp; 8420Sstevel@tonic-gate void **clt_opsp; 8430Sstevel@tonic-gate int i; 8440Sstevel@tonic-gate int total_ops; 8450Sstevel@tonic-gate 8460Sstevel@tonic-gate /* fix up psm ops */ 8470Sstevel@tonic-gate srv_opsp = (void **)mach_set[0]; 8480Sstevel@tonic-gate clt_opsp = (void **)mach_set[owner]; 8490Sstevel@tonic-gate if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01) 8500Sstevel@tonic-gate total_ops = sizeof (struct psm_ops_ver01) / 8514481Sbholler sizeof (void (*)(void)); 8520Sstevel@tonic-gate else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_1) 8530Sstevel@tonic-gate /* no psm_notify_func */ 8540Sstevel@tonic-gate total_ops = OFFSETOF(struct psm_ops, psm_notify_func) / 8550Sstevel@tonic-gate sizeof (void (*)(void)); 8560Sstevel@tonic-gate else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_2) 8570Sstevel@tonic-gate /* no psm_timer funcs */ 8580Sstevel@tonic-gate total_ops = OFFSETOF(struct psm_ops, psm_timer_reprogram) / 8590Sstevel@tonic-gate sizeof (void (*)(void)); 8600Sstevel@tonic-gate else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_3) 8610Sstevel@tonic-gate /* no psm_preshutdown function */ 8620Sstevel@tonic-gate total_ops = OFFSETOF(struct psm_ops, psm_preshutdown) / 8630Sstevel@tonic-gate sizeof (void (*)(void)); 8640Sstevel@tonic-gate else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_4) 8650Sstevel@tonic-gate /* no psm_preshutdown function */ 8660Sstevel@tonic-gate total_ops = OFFSETOF(struct psm_ops, psm_intr_ops) / 8670Sstevel@tonic-gate sizeof (void (*)(void)); 8680Sstevel@tonic-gate else 8690Sstevel@tonic-gate total_ops = sizeof (struct psm_ops) / sizeof (void (*)(void)); 8700Sstevel@tonic-gate 8710Sstevel@tonic-gate /* 8720Sstevel@tonic-gate * Save the version of the PSM module, in case we need to 8739637SRandy.Fishel@Sun.COM * behave differently based on version. 8740Sstevel@tonic-gate */ 8750Sstevel@tonic-gate mach_ver[0] = mach_ver[owner]; 8760Sstevel@tonic-gate 8770Sstevel@tonic-gate for (i = 0; i < total_ops; i++) 8780Sstevel@tonic-gate if (clt_opsp[i] != NULL) 8790Sstevel@tonic-gate srv_opsp[i] = clt_opsp[i]; 8800Sstevel@tonic-gate } 8810Sstevel@tonic-gate 8820Sstevel@tonic-gate static void 8830Sstevel@tonic-gate mach_construct_info() 8840Sstevel@tonic-gate { 8853446Smrj struct psm_sw *swp; 8860Sstevel@tonic-gate int mach_cnt[PSM_OWN_OVERRIDE+1] = {0}; 8870Sstevel@tonic-gate int conflict_owner = 0; 8880Sstevel@tonic-gate 8890Sstevel@tonic-gate if (psmsw->psw_forw == psmsw) 8900Sstevel@tonic-gate panic("No valid PSM modules found"); 8910Sstevel@tonic-gate mutex_enter(&psmsw_lock); 8920Sstevel@tonic-gate for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) { 8930Sstevel@tonic-gate if (!(swp->psw_flag & PSM_MOD_IDENTIFY)) 8940Sstevel@tonic-gate continue; 8950Sstevel@tonic-gate mach_set[swp->psw_infop->p_owner] = swp->psw_infop->p_ops; 8960Sstevel@tonic-gate mach_ver[swp->psw_infop->p_owner] = swp->psw_infop->p_version; 8970Sstevel@tonic-gate mach_cnt[swp->psw_infop->p_owner]++; 8980Sstevel@tonic-gate } 8990Sstevel@tonic-gate mutex_exit(&psmsw_lock); 9000Sstevel@tonic-gate 9010Sstevel@tonic-gate mach_get_platform(PSM_OWN_SYS_DEFAULT); 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate /* check to see are there any conflicts */ 9040Sstevel@tonic-gate if (mach_cnt[PSM_OWN_EXCLUSIVE] > 1) 9050Sstevel@tonic-gate conflict_owner = PSM_OWN_EXCLUSIVE; 9060Sstevel@tonic-gate if (mach_cnt[PSM_OWN_OVERRIDE] > 1) 9070Sstevel@tonic-gate conflict_owner = PSM_OWN_OVERRIDE; 9080Sstevel@tonic-gate if (conflict_owner) { 9090Sstevel@tonic-gate /* remove all psm modules except uppc */ 9100Sstevel@tonic-gate cmn_err(CE_WARN, 9114481Sbholler "Conflicts detected on the following PSM modules:"); 9120Sstevel@tonic-gate mutex_enter(&psmsw_lock); 9130Sstevel@tonic-gate for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) { 9140Sstevel@tonic-gate if (swp->psw_infop->p_owner == conflict_owner) 9150Sstevel@tonic-gate cmn_err(CE_WARN, "%s ", 9164481Sbholler swp->psw_infop->p_mach_idstring); 9170Sstevel@tonic-gate } 9180Sstevel@tonic-gate mutex_exit(&psmsw_lock); 9190Sstevel@tonic-gate cmn_err(CE_WARN, 9204481Sbholler "Setting the system back to SINGLE processor mode!"); 9210Sstevel@tonic-gate cmn_err(CE_WARN, 9220Sstevel@tonic-gate "Please edit /etc/mach to remove the invalid PSM module."); 9230Sstevel@tonic-gate return; 9240Sstevel@tonic-gate } 9250Sstevel@tonic-gate 9260Sstevel@tonic-gate if (mach_set[PSM_OWN_EXCLUSIVE]) 9270Sstevel@tonic-gate mach_get_platform(PSM_OWN_EXCLUSIVE); 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate if (mach_set[PSM_OWN_OVERRIDE]) 9300Sstevel@tonic-gate mach_get_platform(PSM_OWN_OVERRIDE); 9310Sstevel@tonic-gate } 9320Sstevel@tonic-gate 9330Sstevel@tonic-gate static void 9340Sstevel@tonic-gate mach_init() 9350Sstevel@tonic-gate { 9363446Smrj struct psm_ops *pops; 9370Sstevel@tonic-gate 9380Sstevel@tonic-gate mach_construct_info(); 9390Sstevel@tonic-gate 9400Sstevel@tonic-gate pops = mach_set[0]; 9410Sstevel@tonic-gate 9420Sstevel@tonic-gate /* register the interrupt and clock initialization rotuines */ 9430Sstevel@tonic-gate picinitf = mach_picinit; 9440Sstevel@tonic-gate clkinitf = mach_clkinit; 9450Sstevel@tonic-gate psm_get_clockirq = pops->psm_get_clockirq; 9460Sstevel@tonic-gate 9470Sstevel@tonic-gate /* register the interrupt setup code */ 9480Sstevel@tonic-gate slvltovect = mach_softlvl_to_vect; 9490Sstevel@tonic-gate addspl = pops->psm_addspl; 9500Sstevel@tonic-gate delspl = pops->psm_delspl; 9510Sstevel@tonic-gate 9520Sstevel@tonic-gate if (pops->psm_translate_irq) 9530Sstevel@tonic-gate psm_translate_irq = pops->psm_translate_irq; 9540Sstevel@tonic-gate if (pops->psm_intr_ops) 9550Sstevel@tonic-gate psm_intr_ops = pops->psm_intr_ops; 9563446Smrj 9573446Smrj #if defined(PSMI_1_2) || defined(PSMI_1_3) || defined(PSMI_1_4) 9583446Smrj /* 9593446Smrj * Time-of-day functionality now handled in TOD modules. 9603446Smrj * (Warn about PSM modules that think that we're going to use 9613446Smrj * their ops vectors.) 9623446Smrj */ 9633446Smrj if (pops->psm_tod_get) 9643446Smrj cmn_err(CE_WARN, "obsolete psm_tod_get op %p", 9653446Smrj (void *)pops->psm_tod_get); 9663446Smrj 9673446Smrj if (pops->psm_tod_set) 9683446Smrj cmn_err(CE_WARN, "obsolete psm_tod_set op %p", 9693446Smrj (void *)pops->psm_tod_set); 9703446Smrj #endif 9713446Smrj 9720Sstevel@tonic-gate if (pops->psm_notify_error) { 9730Sstevel@tonic-gate psm_notify_error = mach_notify_error; 9740Sstevel@tonic-gate notify_error = pops->psm_notify_error; 9750Sstevel@tonic-gate } 9760Sstevel@tonic-gate 9770Sstevel@tonic-gate (*pops->psm_softinit)(); 9780Sstevel@tonic-gate 9790Sstevel@tonic-gate /* 9808906SEric.Saxe@Sun.COM * Initialize the dispatcher's function hooks to enable CPU halting 9818906SEric.Saxe@Sun.COM * when idle. Set both the deep-idle and non-deep-idle hooks. 9828906SEric.Saxe@Sun.COM * 9838906SEric.Saxe@Sun.COM * Assume we can use power saving deep-idle loop cpu_idle_adaptive. 9848906SEric.Saxe@Sun.COM * Platform deep-idle driver will reset our idle loop to 9858906SEric.Saxe@Sun.COM * non_deep_idle_cpu if power saving deep-idle feature is not available. 9868906SEric.Saxe@Sun.COM * 9875045Sbholler * Do not use monitor/mwait if idle_cpu_use_hlt is not set(spin idle) 9885045Sbholler * or idle_cpu_prefer_mwait is not set. 9894481Sbholler * Allocate monitor/mwait buffer for cpu0. 9900Sstevel@tonic-gate */ 9918906SEric.Saxe@Sun.COM #ifndef __xpv 9928906SEric.Saxe@Sun.COM non_deep_idle_disp_enq_thread = disp_enq_thread; 9938906SEric.Saxe@Sun.COM #endif 9944481Sbholler if (idle_cpu_use_hlt) { 9958906SEric.Saxe@Sun.COM idle_cpu = cpu_idle_adaptive; 9968906SEric.Saxe@Sun.COM CPU->cpu_m.mcpu_idle_cpu = cpu_idle; 9975084Sjohnlev #ifndef __xpv 9984481Sbholler if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait) { 9995045Sbholler CPU->cpu_m.mcpu_mwait = cpuid_mwait_alloc(CPU); 10005045Sbholler /* 10015045Sbholler * Protect ourself from insane mwait size. 10025045Sbholler */ 10035045Sbholler if (CPU->cpu_m.mcpu_mwait == NULL) { 10045045Sbholler #ifdef DEBUG 10055045Sbholler cmn_err(CE_NOTE, "Using hlt idle. Cannot " 10065045Sbholler "handle cpu 0 mwait size."); 10075045Sbholler #endif 10085045Sbholler idle_cpu_prefer_mwait = 0; 10098906SEric.Saxe@Sun.COM CPU->cpu_m.mcpu_idle_cpu = cpu_idle; 10105045Sbholler } else { 10118906SEric.Saxe@Sun.COM CPU->cpu_m.mcpu_idle_cpu = cpu_idle_mwait; 10125045Sbholler } 10134481Sbholler } else { 10148906SEric.Saxe@Sun.COM CPU->cpu_m.mcpu_idle_cpu = cpu_idle; 10158906SEric.Saxe@Sun.COM } 10168906SEric.Saxe@Sun.COM non_deep_idle_cpu = CPU->cpu_m.mcpu_idle_cpu; 10178906SEric.Saxe@Sun.COM 10188906SEric.Saxe@Sun.COM /* 10198906SEric.Saxe@Sun.COM * Disable power saving deep idle loop? 10208906SEric.Saxe@Sun.COM */ 10218906SEric.Saxe@Sun.COM if (idle_cpu_no_deep_c) { 10228906SEric.Saxe@Sun.COM idle_cpu = non_deep_idle_cpu; 10234481Sbholler } 10245084Sjohnlev #endif 10254481Sbholler } 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate mach_smpinit(); 10280Sstevel@tonic-gate } 10290Sstevel@tonic-gate 10300Sstevel@tonic-gate static void 10310Sstevel@tonic-gate mach_smpinit(void) 10320Sstevel@tonic-gate { 10332006Sandrei struct psm_ops *pops; 10342006Sandrei processorid_t cpu_id; 10352006Sandrei int cnt; 10362006Sandrei cpuset_t cpumask; 10370Sstevel@tonic-gate 10380Sstevel@tonic-gate pops = mach_set[0]; 10396336Sbholler CPUSET_ZERO(cpumask); 10400Sstevel@tonic-gate 10410Sstevel@tonic-gate cpu_id = -1; 10420Sstevel@tonic-gate cpu_id = (*pops->psm_get_next_processorid)(cpu_id); 10436336Sbholler for (cnt = 0; cpu_id != -1; cnt++) { 10442006Sandrei CPUSET_ADD(cpumask, cpu_id); 10450Sstevel@tonic-gate cpu_id = (*pops->psm_get_next_processorid)(cpu_id); 10460Sstevel@tonic-gate } 10470Sstevel@tonic-gate 10480Sstevel@tonic-gate mp_cpus = cpumask; 10490Sstevel@tonic-gate 10500Sstevel@tonic-gate /* MP related routines */ 10510Sstevel@tonic-gate ap_mlsetup = pops->psm_post_cpu_start; 10520Sstevel@tonic-gate send_dirintf = pops->psm_send_ipi; 10530Sstevel@tonic-gate 10540Sstevel@tonic-gate /* optional MP related routines */ 10550Sstevel@tonic-gate if (pops->psm_shutdown) 10560Sstevel@tonic-gate psm_shutdownf = pops->psm_shutdown; 10570Sstevel@tonic-gate if (pops->psm_preshutdown) 10580Sstevel@tonic-gate psm_preshutdownf = pops->psm_preshutdown; 10590Sstevel@tonic-gate if (pops->psm_notify_func) 10600Sstevel@tonic-gate psm_notifyf = pops->psm_notify_func; 10610Sstevel@tonic-gate if (pops->psm_set_idlecpu) 10620Sstevel@tonic-gate psm_set_idle_cpuf = pops->psm_set_idlecpu; 10630Sstevel@tonic-gate if (pops->psm_unset_idlecpu) 10640Sstevel@tonic-gate psm_unset_idle_cpuf = pops->psm_unset_idlecpu; 10650Sstevel@tonic-gate 10660Sstevel@tonic-gate psm_clkinit = pops->psm_clkinit; 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate if (pops->psm_timer_reprogram) 10690Sstevel@tonic-gate psm_timer_reprogram = pops->psm_timer_reprogram; 10700Sstevel@tonic-gate 10710Sstevel@tonic-gate if (pops->psm_timer_enable) 10720Sstevel@tonic-gate psm_timer_enable = pops->psm_timer_enable; 10730Sstevel@tonic-gate 10740Sstevel@tonic-gate if (pops->psm_timer_disable) 10750Sstevel@tonic-gate psm_timer_disable = pops->psm_timer_disable; 10760Sstevel@tonic-gate 10770Sstevel@tonic-gate if (pops->psm_post_cyclic_setup) 10780Sstevel@tonic-gate psm_post_cyclic_setup = pops->psm_post_cyclic_setup; 10790Sstevel@tonic-gate 10805295Srandyf if (pops->psm_state) 10815295Srandyf psm_state = pops->psm_state; 10825295Srandyf 10837113Sbholler /* 10847113Sbholler * Set these vectors here so they can be used by Suspend/Resume 10857113Sbholler * on UP machines. 10867113Sbholler */ 10877113Sbholler if (pops->psm_disable_intr) 10887113Sbholler psm_disable_intr = pops->psm_disable_intr; 10897113Sbholler if (pops->psm_enable_intr) 10907113Sbholler psm_enable_intr = pops->psm_enable_intr; 10917113Sbholler 10927113Sbholler /* check for multiple CPUs */ 10930Sstevel@tonic-gate if (cnt < 2) 10940Sstevel@tonic-gate return; 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate /* check for MP platforms */ 10970Sstevel@tonic-gate if (pops->psm_cpu_start == NULL) 10980Sstevel@tonic-gate return; 10990Sstevel@tonic-gate 11000Sstevel@tonic-gate /* 11010Sstevel@tonic-gate * Set the dispatcher hook to enable cpu "wake up" 11020Sstevel@tonic-gate * when a thread becomes runnable. 11030Sstevel@tonic-gate */ 11045084Sjohnlev if (idle_cpu_use_hlt) { 11055084Sjohnlev disp_enq_thread = cpu_wakeup; 11065084Sjohnlev #ifndef __xpv 11074481Sbholler if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait) 11084481Sbholler disp_enq_thread = cpu_wakeup_mwait; 11098906SEric.Saxe@Sun.COM non_deep_idle_disp_enq_thread = disp_enq_thread; 11105084Sjohnlev #endif 11115084Sjohnlev } 11120Sstevel@tonic-gate 11130Sstevel@tonic-gate psm_get_ipivect = pops->psm_get_ipivect; 11140Sstevel@tonic-gate 11159489SJoe.Bonasera@sun.com (void) add_avintr((void *)NULL, XC_HI_PIL, xc_serv, "xc_intr", 11164481Sbholler (*pops->psm_get_ipivect)(XC_HI_PIL, PSM_INTR_IPI_HI), 11179489SJoe.Bonasera@sun.com NULL, NULL, NULL, NULL); 11180Sstevel@tonic-gate 11190Sstevel@tonic-gate (void) (*pops->psm_get_ipivect)(XC_CPUPOKE_PIL, PSM_INTR_POKE); 11200Sstevel@tonic-gate } 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate static void 11230Sstevel@tonic-gate mach_picinit() 11240Sstevel@tonic-gate { 11252006Sandrei struct psm_ops *pops; 11260Sstevel@tonic-gate 11270Sstevel@tonic-gate pops = mach_set[0]; 11280Sstevel@tonic-gate 11290Sstevel@tonic-gate /* register the interrupt handlers */ 11300Sstevel@tonic-gate setlvl = pops->psm_intr_enter; 11310Sstevel@tonic-gate setlvlx = pops->psm_intr_exit; 11320Sstevel@tonic-gate 11330Sstevel@tonic-gate /* initialize the interrupt hardware */ 11340Sstevel@tonic-gate (*pops->psm_picinit)(); 11350Sstevel@tonic-gate 11360Sstevel@tonic-gate /* set interrupt mask for current ipl */ 11370Sstevel@tonic-gate setspl = pops->psm_setspl; 11383446Smrj cli(); 11390Sstevel@tonic-gate setspl(CPU->cpu_pri); 11400Sstevel@tonic-gate } 11410Sstevel@tonic-gate 11420Sstevel@tonic-gate uint_t cpu_freq; /* MHz */ 11430Sstevel@tonic-gate uint64_t cpu_freq_hz; /* measured (in hertz) */ 11440Sstevel@tonic-gate 11450Sstevel@tonic-gate #define MEGA_HZ 1000000 11460Sstevel@tonic-gate 11475084Sjohnlev #ifdef __xpv 11485084Sjohnlev 11495084Sjohnlev int xpv_cpufreq_workaround = 1; 11505084Sjohnlev int xpv_cpufreq_verbose = 0; 11515084Sjohnlev 11525084Sjohnlev #else /* __xpv */ 11535084Sjohnlev 11540Sstevel@tonic-gate static uint64_t 11550Sstevel@tonic-gate mach_calchz(uint32_t pit_counter, uint64_t *processor_clks) 11560Sstevel@tonic-gate { 11570Sstevel@tonic-gate uint64_t cpu_hz; 11580Sstevel@tonic-gate 11590Sstevel@tonic-gate if ((pit_counter == 0) || (*processor_clks == 0) || 11600Sstevel@tonic-gate (*processor_clks > (((uint64_t)-1) / PIT_HZ))) 11610Sstevel@tonic-gate return (0); 11620Sstevel@tonic-gate 11630Sstevel@tonic-gate cpu_hz = ((uint64_t)PIT_HZ * *processor_clks) / pit_counter; 11640Sstevel@tonic-gate 11650Sstevel@tonic-gate return (cpu_hz); 11660Sstevel@tonic-gate } 11670Sstevel@tonic-gate 11685084Sjohnlev #endif /* __xpv */ 11695084Sjohnlev 11700Sstevel@tonic-gate static uint64_t 11710Sstevel@tonic-gate mach_getcpufreq(void) 11720Sstevel@tonic-gate { 11735084Sjohnlev #if defined(__xpv) 11745084Sjohnlev vcpu_time_info_t *vti = &CPU->cpu_m.mcpu_vcpu_info->time; 11755084Sjohnlev uint64_t cpu_hz; 11765084Sjohnlev 11775084Sjohnlev /* 11785084Sjohnlev * During dom0 bringup, it was noted that on at least one older 11795084Sjohnlev * Intel HT machine, the hypervisor initially gives a tsc_to_system_mul 11805084Sjohnlev * value that is quite wrong (the 3.06GHz clock was reported 11815084Sjohnlev * as 4.77GHz) 11825084Sjohnlev * 11835084Sjohnlev * The curious thing is, that if you stop the kernel at entry, 11845084Sjohnlev * breakpoint here and inspect the value with kmdb, the value 11855084Sjohnlev * is correct - but if you don't stop and simply enable the 11865084Sjohnlev * printf statement (below), you can see the bad value printed 11875084Sjohnlev * here. Almost as if something kmdb did caused the hypervisor to 11885084Sjohnlev * figure it out correctly. And, note that the hypervisor 11895084Sjohnlev * eventually -does- figure it out correctly ... if you look at 11905084Sjohnlev * the field later in the life of dom0, it is correct. 11915084Sjohnlev * 11925084Sjohnlev * For now, on dom0, we employ a slightly cheesy workaround of 11935084Sjohnlev * using the DOM0_PHYSINFO hypercall. 11945084Sjohnlev */ 11955084Sjohnlev if (DOMAIN_IS_INITDOMAIN(xen_info) && xpv_cpufreq_workaround) { 119610175SStuart.Maybee@Sun.COM cpu_hz = 1000 * xpv_cpu_khz(); 11975084Sjohnlev } else { 11985084Sjohnlev cpu_hz = (UINT64_C(1000000000) << 32) / vti->tsc_to_system_mul; 11995084Sjohnlev 12005084Sjohnlev if (vti->tsc_shift < 0) 12015084Sjohnlev cpu_hz <<= -vti->tsc_shift; 12025084Sjohnlev else 12035084Sjohnlev cpu_hz >>= vti->tsc_shift; 12045084Sjohnlev } 12055084Sjohnlev 12065084Sjohnlev if (xpv_cpufreq_verbose) 12075084Sjohnlev printf("mach_getcpufreq: system_mul 0x%x, shift %d, " 12085084Sjohnlev "cpu_hz %" PRId64 "Hz\n", 12095084Sjohnlev vti->tsc_to_system_mul, vti->tsc_shift, cpu_hz); 12105084Sjohnlev 12115084Sjohnlev return (cpu_hz); 12125084Sjohnlev #else /* __xpv */ 12130Sstevel@tonic-gate uint32_t pit_counter; 12140Sstevel@tonic-gate uint64_t processor_clks; 12150Sstevel@tonic-gate 12160Sstevel@tonic-gate if (x86_feature & X86_TSC) { 12170Sstevel@tonic-gate /* 12180Sstevel@tonic-gate * We have a TSC. freq_tsc() knows how to measure the number 12190Sstevel@tonic-gate * of clock cycles sampled against the PIT. 12200Sstevel@tonic-gate */ 12213446Smrj ulong_t flags = clear_int_flag(); 12220Sstevel@tonic-gate processor_clks = freq_tsc(&pit_counter); 12233446Smrj restore_int_flag(flags); 12240Sstevel@tonic-gate return (mach_calchz(pit_counter, &processor_clks)); 12250Sstevel@tonic-gate } else if (x86_vendor == X86_VENDOR_Cyrix || x86_type == X86_TYPE_P5) { 12260Sstevel@tonic-gate #if defined(__amd64) 12270Sstevel@tonic-gate panic("mach_getcpufreq: no TSC!"); 12280Sstevel@tonic-gate #elif defined(__i386) 12290Sstevel@tonic-gate /* 12300Sstevel@tonic-gate * We are a Cyrix based on a 6x86 core or an Intel Pentium 12310Sstevel@tonic-gate * for which freq_notsc() knows how to measure the number of 12320Sstevel@tonic-gate * elapsed clock cycles sampled against the PIT 12330Sstevel@tonic-gate */ 12343446Smrj ulong_t flags = clear_int_flag(); 12350Sstevel@tonic-gate processor_clks = freq_notsc(&pit_counter); 12363446Smrj restore_int_flag(flags); 12370Sstevel@tonic-gate return (mach_calchz(pit_counter, &processor_clks)); 12380Sstevel@tonic-gate #endif /* __i386 */ 12390Sstevel@tonic-gate } 12400Sstevel@tonic-gate 12410Sstevel@tonic-gate /* We do not know how to calculate cpu frequency for this cpu. */ 12420Sstevel@tonic-gate return (0); 12435084Sjohnlev #endif /* __xpv */ 12440Sstevel@tonic-gate } 12450Sstevel@tonic-gate 12460Sstevel@tonic-gate /* 12470Sstevel@tonic-gate * If the clock speed of a cpu is found to be reported incorrectly, do not add 12480Sstevel@tonic-gate * to this array, instead improve the accuracy of the algorithm that determines 12490Sstevel@tonic-gate * the clock speed of the processor or extend the implementation to support the 12500Sstevel@tonic-gate * vendor as appropriate. This is here only to support adjusting the speed on 12510Sstevel@tonic-gate * older slower processors that mach_fixcpufreq() would not be able to account 12520Sstevel@tonic-gate * for otherwise. 12530Sstevel@tonic-gate */ 12540Sstevel@tonic-gate static int x86_cpu_freq[] = { 60, 75, 80, 90, 120, 160, 166, 175, 180, 233 }; 12550Sstevel@tonic-gate 12560Sstevel@tonic-gate /* 12570Sstevel@tonic-gate * On fast processors the clock frequency that is measured may be off by 12580Sstevel@tonic-gate * a few MHz from the value printed on the part. This is a combination of 12590Sstevel@tonic-gate * the factors that for such fast parts being off by this much is within 12600Sstevel@tonic-gate * the tolerances for manufacture and because of the difficulties in the 12610Sstevel@tonic-gate * measurement that can lead to small error. This function uses some 12620Sstevel@tonic-gate * heuristics in order to tweak the value that was measured to match what 12630Sstevel@tonic-gate * is most likely printed on the part. 12640Sstevel@tonic-gate * 12650Sstevel@tonic-gate * Some examples: 12660Sstevel@tonic-gate * AMD Athlon 1000 mhz measured as 998 mhz 12670Sstevel@tonic-gate * Intel Pentium III Xeon 733 mhz measured as 731 mhz 12680Sstevel@tonic-gate * Intel Pentium IV 1500 mhz measured as 1495mhz 12690Sstevel@tonic-gate * 12700Sstevel@tonic-gate * If in the future this function is no longer sufficient to correct 12710Sstevel@tonic-gate * for the error in the measurement, then the algorithm used to perform 12720Sstevel@tonic-gate * the measurement will have to be improved in order to increase accuracy 12730Sstevel@tonic-gate * rather than adding horrible and questionable kludges here. 12740Sstevel@tonic-gate * 12750Sstevel@tonic-gate * This is called after the cyclics subsystem because of the potential 12760Sstevel@tonic-gate * that the heuristics within may give a worse estimate of the clock 12770Sstevel@tonic-gate * frequency than the value that was measured. 12780Sstevel@tonic-gate */ 12790Sstevel@tonic-gate static void 12800Sstevel@tonic-gate mach_fixcpufreq(void) 12810Sstevel@tonic-gate { 12820Sstevel@tonic-gate uint32_t freq, mul, near66, delta66, near50, delta50, fixed, delta, i; 12830Sstevel@tonic-gate 12840Sstevel@tonic-gate freq = (uint32_t)cpu_freq; 12850Sstevel@tonic-gate 12860Sstevel@tonic-gate /* 12870Sstevel@tonic-gate * Find the nearest integer multiple of 200/3 (about 66) MHz to the 12880Sstevel@tonic-gate * measured speed taking into account that the 667 MHz parts were 12890Sstevel@tonic-gate * the first to round-up. 12900Sstevel@tonic-gate */ 12910Sstevel@tonic-gate mul = (uint32_t)((3 * (uint64_t)freq + 100) / 200); 12920Sstevel@tonic-gate near66 = (uint32_t)((200 * (uint64_t)mul + ((mul >= 10) ? 1 : 0)) / 3); 12930Sstevel@tonic-gate delta66 = (near66 > freq) ? (near66 - freq) : (freq - near66); 12940Sstevel@tonic-gate 12950Sstevel@tonic-gate /* Find the nearest integer multiple of 50 MHz to the measured speed */ 12960Sstevel@tonic-gate mul = (freq + 25) / 50; 12970Sstevel@tonic-gate near50 = mul * 50; 12980Sstevel@tonic-gate delta50 = (near50 > freq) ? (near50 - freq) : (freq - near50); 12990Sstevel@tonic-gate 13000Sstevel@tonic-gate /* Find the closer of the two */ 13010Sstevel@tonic-gate if (delta66 < delta50) { 13020Sstevel@tonic-gate fixed = near66; 13030Sstevel@tonic-gate delta = delta66; 13040Sstevel@tonic-gate } else { 13050Sstevel@tonic-gate fixed = near50; 13060Sstevel@tonic-gate delta = delta50; 13070Sstevel@tonic-gate } 13080Sstevel@tonic-gate 13090Sstevel@tonic-gate if (fixed > INT_MAX) 13100Sstevel@tonic-gate return; 13110Sstevel@tonic-gate 13120Sstevel@tonic-gate /* 13130Sstevel@tonic-gate * Some older parts have a core clock frequency that is not an 13140Sstevel@tonic-gate * integral multiple of 50 or 66 MHz. Check if one of the old 13150Sstevel@tonic-gate * clock frequencies is closer to the measured value than any 13160Sstevel@tonic-gate * of the integral multiples of 50 an 66, and if so set fixed 13170Sstevel@tonic-gate * and delta appropriately to represent the closest value. 13180Sstevel@tonic-gate */ 13190Sstevel@tonic-gate i = sizeof (x86_cpu_freq) / sizeof (int); 13200Sstevel@tonic-gate while (i > 0) { 13210Sstevel@tonic-gate i--; 13220Sstevel@tonic-gate 13230Sstevel@tonic-gate if (x86_cpu_freq[i] <= freq) { 13240Sstevel@tonic-gate mul = freq - x86_cpu_freq[i]; 13250Sstevel@tonic-gate 13260Sstevel@tonic-gate if (mul < delta) { 13270Sstevel@tonic-gate fixed = x86_cpu_freq[i]; 13280Sstevel@tonic-gate delta = mul; 13290Sstevel@tonic-gate } 13300Sstevel@tonic-gate 13310Sstevel@tonic-gate break; 13320Sstevel@tonic-gate } 13330Sstevel@tonic-gate 13340Sstevel@tonic-gate mul = x86_cpu_freq[i] - freq; 13350Sstevel@tonic-gate 13360Sstevel@tonic-gate if (mul < delta) { 13370Sstevel@tonic-gate fixed = x86_cpu_freq[i]; 13380Sstevel@tonic-gate delta = mul; 13390Sstevel@tonic-gate } 13400Sstevel@tonic-gate } 13410Sstevel@tonic-gate 13420Sstevel@tonic-gate /* 13430Sstevel@tonic-gate * Set a reasonable maximum for how much to correct the measured 13440Sstevel@tonic-gate * result by. This check is here to prevent the adjustment made 13450Sstevel@tonic-gate * by this function from being more harm than good. It is entirely 13460Sstevel@tonic-gate * possible that in the future parts will be made that are not 13470Sstevel@tonic-gate * integral multiples of 66 or 50 in clock frequency or that 13480Sstevel@tonic-gate * someone may overclock a part to some odd frequency. If the 13490Sstevel@tonic-gate * measured value is farther from the corrected value than 13500Sstevel@tonic-gate * allowed, then assume the corrected value is in error and use 13510Sstevel@tonic-gate * the measured value. 13520Sstevel@tonic-gate */ 13530Sstevel@tonic-gate if (6 < delta) 13540Sstevel@tonic-gate return; 13550Sstevel@tonic-gate 13560Sstevel@tonic-gate cpu_freq = (int)fixed; 13570Sstevel@tonic-gate } 13580Sstevel@tonic-gate 13590Sstevel@tonic-gate 13600Sstevel@tonic-gate static int 13610Sstevel@tonic-gate machhztomhz(uint64_t cpu_freq_hz) 13620Sstevel@tonic-gate { 13630Sstevel@tonic-gate uint64_t cpu_mhz; 13640Sstevel@tonic-gate 13650Sstevel@tonic-gate /* Round to nearest MHZ */ 13660Sstevel@tonic-gate cpu_mhz = (cpu_freq_hz + (MEGA_HZ / 2)) / MEGA_HZ; 13670Sstevel@tonic-gate 13680Sstevel@tonic-gate if (cpu_mhz > INT_MAX) 13690Sstevel@tonic-gate return (0); 13700Sstevel@tonic-gate 13710Sstevel@tonic-gate return ((int)cpu_mhz); 13720Sstevel@tonic-gate 13730Sstevel@tonic-gate } 13740Sstevel@tonic-gate 13750Sstevel@tonic-gate 13760Sstevel@tonic-gate static int 13770Sstevel@tonic-gate mach_clkinit(int preferred_mode, int *set_mode) 13780Sstevel@tonic-gate { 13793446Smrj struct psm_ops *pops; 13800Sstevel@tonic-gate int resolution; 13810Sstevel@tonic-gate 13820Sstevel@tonic-gate pops = mach_set[0]; 13830Sstevel@tonic-gate 13840Sstevel@tonic-gate cpu_freq_hz = mach_getcpufreq(); 13850Sstevel@tonic-gate 13860Sstevel@tonic-gate cpu_freq = machhztomhz(cpu_freq_hz); 13870Sstevel@tonic-gate 13880Sstevel@tonic-gate if (!(x86_feature & X86_TSC) || (cpu_freq == 0)) 13890Sstevel@tonic-gate tsc_gethrtime_enable = 0; 13900Sstevel@tonic-gate 13915084Sjohnlev #ifndef __xpv 13920Sstevel@tonic-gate if (tsc_gethrtime_enable) { 13930Sstevel@tonic-gate tsc_hrtimeinit(cpu_freq_hz); 13945084Sjohnlev } else 13955084Sjohnlev #endif 13965084Sjohnlev { 13970Sstevel@tonic-gate if (pops->psm_hrtimeinit) 13980Sstevel@tonic-gate (*pops->psm_hrtimeinit)(); 13990Sstevel@tonic-gate gethrtimef = pops->psm_gethrtime; 14000Sstevel@tonic-gate gethrtimeunscaledf = gethrtimef; 14010Sstevel@tonic-gate /* scalehrtimef will remain dummy */ 14020Sstevel@tonic-gate } 14030Sstevel@tonic-gate 14040Sstevel@tonic-gate mach_fixcpufreq(); 14050Sstevel@tonic-gate 14060Sstevel@tonic-gate if (mach_ver[0] >= PSM_INFO_VER01_3) { 14075084Sjohnlev if (preferred_mode == TIMER_ONESHOT) { 14080Sstevel@tonic-gate 14090Sstevel@tonic-gate resolution = (*pops->psm_clkinit)(0); 14100Sstevel@tonic-gate if (resolution != 0) { 14110Sstevel@tonic-gate *set_mode = TIMER_ONESHOT; 14120Sstevel@tonic-gate return (resolution); 14130Sstevel@tonic-gate } 14140Sstevel@tonic-gate } 14150Sstevel@tonic-gate 14160Sstevel@tonic-gate /* 14170Sstevel@tonic-gate * either periodic mode was requested or could not set to 14180Sstevel@tonic-gate * one-shot mode 14190Sstevel@tonic-gate */ 14200Sstevel@tonic-gate resolution = (*pops->psm_clkinit)(hz); 14210Sstevel@tonic-gate /* 14220Sstevel@tonic-gate * psm should be able to do periodic, so we do not check 14230Sstevel@tonic-gate * for return value of psm_clkinit here. 14240Sstevel@tonic-gate */ 14250Sstevel@tonic-gate *set_mode = TIMER_PERIODIC; 14260Sstevel@tonic-gate return (resolution); 14270Sstevel@tonic-gate } else { 14280Sstevel@tonic-gate /* 14290Sstevel@tonic-gate * PSMI interface prior to PSMI_3 does not define a return 14300Sstevel@tonic-gate * value for psm_clkinit, so the return value is ignored. 14310Sstevel@tonic-gate */ 14320Sstevel@tonic-gate (void) (*pops->psm_clkinit)(hz); 14330Sstevel@tonic-gate *set_mode = TIMER_PERIODIC; 14340Sstevel@tonic-gate return (nsec_per_tick); 14350Sstevel@tonic-gate } 14360Sstevel@tonic-gate } 14370Sstevel@tonic-gate 14384652Scwb 1439999Slq150181 /*ARGSUSED*/ 14400Sstevel@tonic-gate static int 14413446Smrj mach_softlvl_to_vect(int ipl) 14420Sstevel@tonic-gate { 14434652Scwb setsoftint = av_set_softint_pending; 14444652Scwb kdisetsoftint = kdi_av_set_softint_pending; 14450Sstevel@tonic-gate 14460Sstevel@tonic-gate return (PSM_SV_SOFTWARE); 14470Sstevel@tonic-gate } 14480Sstevel@tonic-gate 14493446Smrj #ifdef DEBUG 14503446Smrj /* 14513446Smrj * This is here to allow us to simulate cpus that refuse to start. 14523446Smrj */ 14533446Smrj cpuset_t cpufailset; 14543446Smrj #endif 14553446Smrj 14563446Smrj int 14573446Smrj mach_cpu_start(struct cpu *cp, void *ctx) 14580Sstevel@tonic-gate { 14593446Smrj struct psm_ops *pops = mach_set[0]; 14603446Smrj processorid_t id = cp->cpu_id; 14610Sstevel@tonic-gate 14623446Smrj #ifdef DEBUG 14633446Smrj if (CPU_IN_SET(cpufailset, id)) 14643446Smrj return (0); 14653446Smrj #endif 14663446Smrj return ((*pops->psm_cpu_start)(id, ctx)); 14670Sstevel@tonic-gate } 14680Sstevel@tonic-gate 14695295Srandyf int 14705295Srandyf mach_cpuid_start(processorid_t id, void *ctx) 14715295Srandyf { 14725295Srandyf struct psm_ops *pops = mach_set[0]; 14735295Srandyf 14745295Srandyf #ifdef DEBUG 14755295Srandyf if (CPU_IN_SET(cpufailset, id)) 14765295Srandyf return (0); 14775295Srandyf #endif 14785295Srandyf return ((*pops->psm_cpu_start)(id, ctx)); 14795295Srandyf } 14805295Srandyf 14819652SMichael.Corcoran@Sun.COM /* 14829652SMichael.Corcoran@Sun.COM * Default handler to create device node for CPU. 14839652SMichael.Corcoran@Sun.COM * One reference count will be held on created device node. 14849652SMichael.Corcoran@Sun.COM */ 14859652SMichael.Corcoran@Sun.COM static int 14869652SMichael.Corcoran@Sun.COM mach_cpu_create_devinfo(cpu_t *cp, dev_info_t **dipp) 14879652SMichael.Corcoran@Sun.COM { 14889652SMichael.Corcoran@Sun.COM int rv, circ; 14899652SMichael.Corcoran@Sun.COM dev_info_t *dip; 14909652SMichael.Corcoran@Sun.COM static kmutex_t cpu_node_lock; 14919652SMichael.Corcoran@Sun.COM static dev_info_t *cpu_nex_devi = NULL; 14929652SMichael.Corcoran@Sun.COM 14939652SMichael.Corcoran@Sun.COM ASSERT(cp != NULL); 14949652SMichael.Corcoran@Sun.COM ASSERT(dipp != NULL); 14959652SMichael.Corcoran@Sun.COM *dipp = NULL; 14969652SMichael.Corcoran@Sun.COM 14979652SMichael.Corcoran@Sun.COM if (cpu_nex_devi == NULL) { 14989652SMichael.Corcoran@Sun.COM mutex_enter(&cpu_node_lock); 14999652SMichael.Corcoran@Sun.COM /* First check whether cpus exists. */ 15009652SMichael.Corcoran@Sun.COM cpu_nex_devi = ddi_find_devinfo("cpus", -1, 0); 15019652SMichael.Corcoran@Sun.COM /* Create cpus if it doesn't exist. */ 15029652SMichael.Corcoran@Sun.COM if (cpu_nex_devi == NULL) { 15039652SMichael.Corcoran@Sun.COM ndi_devi_enter(ddi_root_node(), &circ); 15049652SMichael.Corcoran@Sun.COM rv = ndi_devi_alloc(ddi_root_node(), "cpus", 15059652SMichael.Corcoran@Sun.COM (pnode_t)DEVI_SID_NODEID, &dip); 15069652SMichael.Corcoran@Sun.COM if (rv != NDI_SUCCESS) { 15079652SMichael.Corcoran@Sun.COM mutex_exit(&cpu_node_lock); 15089652SMichael.Corcoran@Sun.COM cmn_err(CE_CONT, 15099652SMichael.Corcoran@Sun.COM "?failed to create cpu nexus device.\n"); 15109652SMichael.Corcoran@Sun.COM return (PSM_FAILURE); 15119652SMichael.Corcoran@Sun.COM } 15129652SMichael.Corcoran@Sun.COM ASSERT(dip != NULL); 15139652SMichael.Corcoran@Sun.COM (void) ndi_devi_online(dip, 0); 15149652SMichael.Corcoran@Sun.COM ndi_devi_exit(ddi_root_node(), circ); 15159652SMichael.Corcoran@Sun.COM cpu_nex_devi = dip; 15169652SMichael.Corcoran@Sun.COM } 15179652SMichael.Corcoran@Sun.COM mutex_exit(&cpu_node_lock); 15189652SMichael.Corcoran@Sun.COM } 15199652SMichael.Corcoran@Sun.COM 15209652SMichael.Corcoran@Sun.COM /* 15219652SMichael.Corcoran@Sun.COM * create a child node for cpu identified as 'cpu_id' 15229652SMichael.Corcoran@Sun.COM */ 15239652SMichael.Corcoran@Sun.COM ndi_devi_enter(cpu_nex_devi, &circ); 152410537SMichael.Corcoran@Sun.COM dip = ddi_add_child(cpu_nex_devi, "cpu", DEVI_SID_NODEID, -1); 15259652SMichael.Corcoran@Sun.COM if (dip == NULL) { 15269652SMichael.Corcoran@Sun.COM cmn_err(CE_CONT, 15279652SMichael.Corcoran@Sun.COM "?failed to create device node for cpu%d.\n", cp->cpu_id); 15289652SMichael.Corcoran@Sun.COM rv = PSM_FAILURE; 15299652SMichael.Corcoran@Sun.COM } else { 15309652SMichael.Corcoran@Sun.COM *dipp = dip; 15319652SMichael.Corcoran@Sun.COM (void) ndi_hold_devi(dip); 15329652SMichael.Corcoran@Sun.COM rv = PSM_SUCCESS; 15339652SMichael.Corcoran@Sun.COM } 15349652SMichael.Corcoran@Sun.COM ndi_devi_exit(cpu_nex_devi, circ); 15359652SMichael.Corcoran@Sun.COM 15369652SMichael.Corcoran@Sun.COM return (rv); 15379652SMichael.Corcoran@Sun.COM } 15389652SMichael.Corcoran@Sun.COM 15399652SMichael.Corcoran@Sun.COM /* 15409652SMichael.Corcoran@Sun.COM * Create cpu device node in device tree and online it. 15419652SMichael.Corcoran@Sun.COM * Return created dip with reference count held if requested. 15429652SMichael.Corcoran@Sun.COM */ 15439652SMichael.Corcoran@Sun.COM int 15449652SMichael.Corcoran@Sun.COM mach_cpu_create_device_node(struct cpu *cp, dev_info_t **dipp) 15459652SMichael.Corcoran@Sun.COM { 15469652SMichael.Corcoran@Sun.COM int rv; 15479652SMichael.Corcoran@Sun.COM dev_info_t *dip = NULL; 15489652SMichael.Corcoran@Sun.COM 15499652SMichael.Corcoran@Sun.COM ASSERT(psm_cpu_create_devinfo != NULL); 15509652SMichael.Corcoran@Sun.COM rv = psm_cpu_create_devinfo(cp, &dip); 15519652SMichael.Corcoran@Sun.COM if (rv == PSM_SUCCESS) { 15529652SMichael.Corcoran@Sun.COM cpuid_set_cpu_properties(dip, cp->cpu_id, cp->cpu_m.mcpu_cpi); 15539652SMichael.Corcoran@Sun.COM /* Recursively attach driver for parent nexus device. */ 15549652SMichael.Corcoran@Sun.COM if (i_ddi_attach_node_hierarchy(ddi_get_parent(dip)) == 15559652SMichael.Corcoran@Sun.COM DDI_SUCCESS) { 15569652SMichael.Corcoran@Sun.COM /* Configure cpu itself and descendants. */ 15579652SMichael.Corcoran@Sun.COM (void) ndi_devi_online(dip, 15589652SMichael.Corcoran@Sun.COM NDI_ONLINE_ATTACH | NDI_CONFIG); 15599652SMichael.Corcoran@Sun.COM } 15609652SMichael.Corcoran@Sun.COM if (dipp != NULL) { 15619652SMichael.Corcoran@Sun.COM *dipp = dip; 15629652SMichael.Corcoran@Sun.COM } else { 15639652SMichael.Corcoran@Sun.COM (void) ndi_rele_devi(dip); 15649652SMichael.Corcoran@Sun.COM } 15659652SMichael.Corcoran@Sun.COM } 15669652SMichael.Corcoran@Sun.COM 15679652SMichael.Corcoran@Sun.COM return (rv); 15689652SMichael.Corcoran@Sun.COM } 15699652SMichael.Corcoran@Sun.COM 15700Sstevel@tonic-gate /*ARGSUSED*/ 15710Sstevel@tonic-gate static int 15720Sstevel@tonic-gate mach_translate_irq(dev_info_t *dip, int irqno) 15730Sstevel@tonic-gate { 15740Sstevel@tonic-gate return (irqno); /* default to NO translation */ 15750Sstevel@tonic-gate } 15760Sstevel@tonic-gate 15770Sstevel@tonic-gate static void 15780Sstevel@tonic-gate mach_notify_error(int level, char *errmsg) 15790Sstevel@tonic-gate { 15800Sstevel@tonic-gate /* 15810Sstevel@tonic-gate * SL_FATAL is pass in once panicstr is set, deliver it 15820Sstevel@tonic-gate * as CE_PANIC. Also, translate SL_ codes back to CE_ 15830Sstevel@tonic-gate * codes for the psmi handler 15840Sstevel@tonic-gate */ 15850Sstevel@tonic-gate if (level & SL_FATAL) 15860Sstevel@tonic-gate (*notify_error)(CE_PANIC, errmsg); 15870Sstevel@tonic-gate else if (level & SL_WARN) 15880Sstevel@tonic-gate (*notify_error)(CE_WARN, errmsg); 15890Sstevel@tonic-gate else if (level & SL_NOTE) 15900Sstevel@tonic-gate (*notify_error)(CE_NOTE, errmsg); 15910Sstevel@tonic-gate else if (level & SL_CONSOLE) 15920Sstevel@tonic-gate (*notify_error)(CE_CONT, errmsg); 15930Sstevel@tonic-gate } 15940Sstevel@tonic-gate 15950Sstevel@tonic-gate /* 15960Sstevel@tonic-gate * It provides the default basic intr_ops interface for the new DDI 15970Sstevel@tonic-gate * interrupt framework if the PSM doesn't have one. 15980Sstevel@tonic-gate * 15990Sstevel@tonic-gate * Input: 16000Sstevel@tonic-gate * dip - pointer to the dev_info structure of the requested device 16010Sstevel@tonic-gate * hdlp - pointer to the internal interrupt handle structure for the 16020Sstevel@tonic-gate * requested interrupt 16030Sstevel@tonic-gate * intr_op - opcode for this call 16040Sstevel@tonic-gate * result - pointer to the integer that will hold the result to be 16050Sstevel@tonic-gate * passed back if return value is PSM_SUCCESS 16060Sstevel@tonic-gate * 16070Sstevel@tonic-gate * Output: 16080Sstevel@tonic-gate * return value is either PSM_SUCCESS or PSM_FAILURE 16090Sstevel@tonic-gate */ 16100Sstevel@tonic-gate static int 16110Sstevel@tonic-gate mach_intr_ops(dev_info_t *dip, ddi_intr_handle_impl_t *hdlp, 16120Sstevel@tonic-gate psm_intr_op_t intr_op, int *result) 16130Sstevel@tonic-gate { 16140Sstevel@tonic-gate struct intrspec *ispec; 16150Sstevel@tonic-gate 16160Sstevel@tonic-gate switch (intr_op) { 16170Sstevel@tonic-gate case PSM_INTR_OP_CHECK_MSI: 16180Sstevel@tonic-gate *result = hdlp->ih_type & ~(DDI_INTR_TYPE_MSI | 16194481Sbholler DDI_INTR_TYPE_MSIX); 16200Sstevel@tonic-gate break; 16210Sstevel@tonic-gate case PSM_INTR_OP_ALLOC_VECTORS: 16220Sstevel@tonic-gate if (hdlp->ih_type == DDI_INTR_TYPE_FIXED) 16230Sstevel@tonic-gate *result = 1; 16240Sstevel@tonic-gate else 16250Sstevel@tonic-gate *result = 0; 16260Sstevel@tonic-gate break; 16270Sstevel@tonic-gate case PSM_INTR_OP_FREE_VECTORS: 16280Sstevel@tonic-gate break; 16290Sstevel@tonic-gate case PSM_INTR_OP_NAVAIL_VECTORS: 16300Sstevel@tonic-gate if (hdlp->ih_type == DDI_INTR_TYPE_FIXED) 16310Sstevel@tonic-gate *result = 1; 16320Sstevel@tonic-gate else 16330Sstevel@tonic-gate *result = 0; 16340Sstevel@tonic-gate break; 16350Sstevel@tonic-gate case PSM_INTR_OP_XLATE_VECTOR: 1636916Sschwartz ispec = ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp; 16370Sstevel@tonic-gate *result = psm_translate_irq(dip, ispec->intrspec_vec); 16380Sstevel@tonic-gate break; 16390Sstevel@tonic-gate case PSM_INTR_OP_GET_CAP: 16400Sstevel@tonic-gate *result = 0; 16410Sstevel@tonic-gate break; 16420Sstevel@tonic-gate case PSM_INTR_OP_GET_PENDING: 16430Sstevel@tonic-gate case PSM_INTR_OP_CLEAR_MASK: 16440Sstevel@tonic-gate case PSM_INTR_OP_SET_MASK: 16450Sstevel@tonic-gate case PSM_INTR_OP_GET_SHARED: 16460Sstevel@tonic-gate case PSM_INTR_OP_SET_PRI: 16470Sstevel@tonic-gate case PSM_INTR_OP_SET_CAP: 1648916Sschwartz case PSM_INTR_OP_SET_CPU: 1649916Sschwartz case PSM_INTR_OP_GET_INTR: 16500Sstevel@tonic-gate default: 16510Sstevel@tonic-gate return (PSM_FAILURE); 16520Sstevel@tonic-gate } 16530Sstevel@tonic-gate return (PSM_SUCCESS); 16540Sstevel@tonic-gate } 16554769Sdp78419 /* 16564769Sdp78419 * Return 1 if CMT load balancing policies should be 16574769Sdp78419 * implemented across instances of the specified hardware 16584769Sdp78419 * sharing relationship. 16594769Sdp78419 */ 16604769Sdp78419 int 16614769Sdp78419 pg_cmt_load_bal_hw(pghw_type_t hw) 16624769Sdp78419 { 16634769Sdp78419 if (hw == PGHW_IPIPE || 16644769Sdp78419 hw == PGHW_FPU || 1665*10947SSrihari.Venkatesan@Sun.COM hw == PGHW_PROCNODE || 16664769Sdp78419 hw == PGHW_CHIP) 16674769Sdp78419 return (1); 16684769Sdp78419 else 16694769Sdp78419 return (0); 16704769Sdp78419 } 16714769Sdp78419 /* 16724769Sdp78419 * Return 1 if thread affinity polices should be implemented 16734769Sdp78419 * for instances of the specifed hardware sharing relationship. 16744769Sdp78419 */ 16754769Sdp78419 int 16764769Sdp78419 pg_cmt_affinity_hw(pghw_type_t hw) 16774769Sdp78419 { 16784769Sdp78419 if (hw == PGHW_CACHE) 16794769Sdp78419 return (1); 16804769Sdp78419 else 16814769Sdp78419 return (0); 16824769Sdp78419 } 1683