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 */ 250Sstevel@tonic-gate 265295Srandyf #define PSMI_1_6 270Sstevel@tonic-gate #include <sys/smp_impldefs.h> 280Sstevel@tonic-gate #include <sys/psm.h> 290Sstevel@tonic-gate #include <sys/psm_modctl.h> 300Sstevel@tonic-gate #include <sys/pit.h> 310Sstevel@tonic-gate #include <sys/cmn_err.h> 320Sstevel@tonic-gate #include <sys/strlog.h> 330Sstevel@tonic-gate #include <sys/clock.h> 340Sstevel@tonic-gate #include <sys/debug.h> 350Sstevel@tonic-gate #include <sys/rtc.h> 360Sstevel@tonic-gate #include <sys/x86_archext.h> 370Sstevel@tonic-gate #include <sys/cpupart.h> 380Sstevel@tonic-gate #include <sys/cpuvar.h> 394606Sesaxe #include <sys/cmt.h> 404481Sbholler #include <sys/cpu.h> 410Sstevel@tonic-gate #include <sys/disp.h> 420Sstevel@tonic-gate #include <sys/archsystm.h> 433446Smrj #include <sys/machsystm.h> 444481Sbholler #include <sys/sysmacros.h> 455084Sjohnlev #include <sys/memlist.h> 463446Smrj #include <sys/param.h> 473446Smrj #include <sys/promif.h> 48*8906SEric.Saxe@Sun.COM #include <sys/cpu_pm.h> 495084Sjohnlev #if defined(__xpv) 505084Sjohnlev #include <sys/hypervisor.h> 515084Sjohnlev #endif 52916Sschwartz #include <sys/mach_intr.h> 534481Sbholler #include <vm/hat_i86.h> 544652Scwb #include <sys/kdi_machimpl.h> 555864Sesaxe #include <sys/sdt.h> 56*8906SEric.Saxe@Sun.COM #include <sys/hpet.h> 570Sstevel@tonic-gate 580Sstevel@tonic-gate #define OFFSETOF(s, m) (size_t)(&(((s *)0)->m)) 590Sstevel@tonic-gate 600Sstevel@tonic-gate /* 610Sstevel@tonic-gate * Local function prototypes 620Sstevel@tonic-gate */ 630Sstevel@tonic-gate static int mp_disable_intr(processorid_t cpun); 640Sstevel@tonic-gate static void mp_enable_intr(processorid_t cpun); 650Sstevel@tonic-gate static void mach_init(); 660Sstevel@tonic-gate static void mach_picinit(); 670Sstevel@tonic-gate static int machhztomhz(uint64_t cpu_freq_hz); 680Sstevel@tonic-gate static uint64_t mach_getcpufreq(void); 690Sstevel@tonic-gate static void mach_fixcpufreq(void); 700Sstevel@tonic-gate static int mach_clkinit(int, int *); 710Sstevel@tonic-gate static void mach_smpinit(void); 720Sstevel@tonic-gate static int mach_softlvl_to_vect(int ipl); 730Sstevel@tonic-gate static void mach_get_platform(int owner); 740Sstevel@tonic-gate static void mach_construct_info(); 750Sstevel@tonic-gate static int mach_translate_irq(dev_info_t *dip, int irqno); 760Sstevel@tonic-gate static int mach_intr_ops(dev_info_t *, ddi_intr_handle_impl_t *, 770Sstevel@tonic-gate psm_intr_op_t, int *); 780Sstevel@tonic-gate static void mach_notify_error(int level, char *errmsg); 790Sstevel@tonic-gate static hrtime_t dummy_hrtime(void); 800Sstevel@tonic-gate static void dummy_scalehrtime(hrtime_t *); 81*8906SEric.Saxe@Sun.COM void cpu_idle(void); 820Sstevel@tonic-gate static void cpu_wakeup(cpu_t *, int); 835084Sjohnlev #ifndef __xpv 84*8906SEric.Saxe@Sun.COM void cpu_idle_mwait(void); 854481Sbholler static void cpu_wakeup_mwait(cpu_t *, int); 865084Sjohnlev #endif 870Sstevel@tonic-gate /* 880Sstevel@tonic-gate * External reference functions 890Sstevel@tonic-gate */ 900Sstevel@tonic-gate extern void return_instr(); 910Sstevel@tonic-gate extern uint64_t freq_tsc(uint32_t *); 920Sstevel@tonic-gate #if defined(__i386) 930Sstevel@tonic-gate extern uint64_t freq_notsc(uint32_t *); 940Sstevel@tonic-gate #endif 950Sstevel@tonic-gate extern void pc_gethrestime(timestruc_t *); 963434Sesaxe extern int cpuid_get_coreid(cpu_t *); 973434Sesaxe extern int cpuid_get_chipid(cpu_t *); 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* 1000Sstevel@tonic-gate * PSM functions initialization 1010Sstevel@tonic-gate */ 1023446Smrj void (*psm_shutdownf)(int, int) = (void (*)(int, int))return_instr; 1033446Smrj void (*psm_preshutdownf)(int, int) = (void (*)(int, int))return_instr; 1043446Smrj void (*psm_notifyf)(int) = (void (*)(int))return_instr; 1053446Smrj void (*psm_set_idle_cpuf)(int) = (void (*)(int))return_instr; 1063446Smrj void (*psm_unset_idle_cpuf)(int) = (void (*)(int))return_instr; 1070Sstevel@tonic-gate void (*psminitf)() = mach_init; 1080Sstevel@tonic-gate void (*picinitf)() = return_instr; 1090Sstevel@tonic-gate int (*clkinitf)(int, int *) = (int (*)(int, int *))return_instr; 1100Sstevel@tonic-gate int (*ap_mlsetup)() = (int (*)(void))return_instr; 1110Sstevel@tonic-gate void (*send_dirintf)() = return_instr; 1123446Smrj void (*setspl)(int) = (void (*)(int))return_instr; 1130Sstevel@tonic-gate int (*addspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr; 1140Sstevel@tonic-gate int (*delspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr; 1154652Scwb void (*kdisetsoftint)(int, struct av_softinfo *)= 1164652Scwb (void (*)(int, struct av_softinfo *))return_instr; 117999Slq150181 void (*setsoftint)(int, struct av_softinfo *)= 118999Slq150181 (void (*)(int, struct av_softinfo *))return_instr; 1190Sstevel@tonic-gate int (*slvltovect)(int) = (int (*)(int))return_instr; 1200Sstevel@tonic-gate int (*setlvl)(int, int *) = (int (*)(int, int *))return_instr; 1210Sstevel@tonic-gate void (*setlvlx)(int, int) = (void (*)(int, int))return_instr; 1220Sstevel@tonic-gate int (*psm_disable_intr)(int) = mp_disable_intr; 1230Sstevel@tonic-gate void (*psm_enable_intr)(int) = mp_enable_intr; 1240Sstevel@tonic-gate hrtime_t (*gethrtimef)(void) = dummy_hrtime; 1250Sstevel@tonic-gate hrtime_t (*gethrtimeunscaledf)(void) = dummy_hrtime; 1260Sstevel@tonic-gate void (*scalehrtimef)(hrtime_t *) = dummy_scalehrtime; 1270Sstevel@tonic-gate int (*psm_translate_irq)(dev_info_t *, int) = mach_translate_irq; 1280Sstevel@tonic-gate void (*gethrestimef)(timestruc_t *) = pc_gethrestime; 1290Sstevel@tonic-gate void (*psm_notify_error)(int, char *) = (void (*)(int, char *))NULL; 1300Sstevel@tonic-gate int (*psm_get_clockirq)(int) = NULL; 1310Sstevel@tonic-gate int (*psm_get_ipivect)(int, int) = NULL; 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate int (*psm_clkinit)(int) = NULL; 1340Sstevel@tonic-gate void (*psm_timer_reprogram)(hrtime_t) = NULL; 1350Sstevel@tonic-gate void (*psm_timer_enable)(void) = NULL; 1360Sstevel@tonic-gate void (*psm_timer_disable)(void) = NULL; 1370Sstevel@tonic-gate void (*psm_post_cyclic_setup)(void *arg) = NULL; 1380Sstevel@tonic-gate int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *, psm_intr_op_t, 1390Sstevel@tonic-gate int *) = mach_intr_ops; 1405295Srandyf int (*psm_state)(psm_state_request_t *) = (int (*)(psm_state_request_t *)) 1415295Srandyf return_instr; 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate void (*notify_error)(int, char *) = (void (*)(int, char *))return_instr; 1440Sstevel@tonic-gate void (*hrtime_tick)(void) = return_instr; 1450Sstevel@tonic-gate 1465084Sjohnlev /* 1475084Sjohnlev * True if the generic TSC code is our source of hrtime, rather than whatever 1485084Sjohnlev * the PSM can provide. 1495084Sjohnlev */ 1505084Sjohnlev #ifdef __xpv 1515084Sjohnlev int tsc_gethrtime_enable = 0; 1525084Sjohnlev #else 1530Sstevel@tonic-gate int tsc_gethrtime_enable = 1; 1545084Sjohnlev #endif 1550Sstevel@tonic-gate int tsc_gethrtime_initted = 0; 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate /* 1585084Sjohnlev * True if the hrtime implementation is "hires"; namely, better than microdata. 1595084Sjohnlev */ 1605084Sjohnlev int gethrtime_hires = 0; 1615084Sjohnlev 1625084Sjohnlev /* 1630Sstevel@tonic-gate * Local Static Data 1640Sstevel@tonic-gate */ 1650Sstevel@tonic-gate static struct psm_ops mach_ops; 1660Sstevel@tonic-gate static struct psm_ops *mach_set[4] = {&mach_ops, NULL, NULL, NULL}; 1670Sstevel@tonic-gate static ushort_t mach_ver[4] = {0, 0, 0, 0}; 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate /* 1708675SVikram.Hegde@Sun.COM * virtualization support for psm 1718675SVikram.Hegde@Sun.COM */ 1728675SVikram.Hegde@Sun.COM void *psm_vt_ops = NULL; 1738675SVikram.Hegde@Sun.COM /* 1743446Smrj * If non-zero, idle cpus will become "halted" when there's 1750Sstevel@tonic-gate * no work to do. 1760Sstevel@tonic-gate */ 1773446Smrj int idle_cpu_use_hlt = 1; 1780Sstevel@tonic-gate 1795084Sjohnlev #ifndef __xpv 1804481Sbholler /* 1814481Sbholler * If non-zero, idle cpus will use mwait if available to halt instead of hlt. 1824481Sbholler */ 1834481Sbholler int idle_cpu_prefer_mwait = 1; 1847716SBill.Holler@Sun.COM /* 1857716SBill.Holler@Sun.COM * Set to 0 to avoid MONITOR+CLFLUSH assertion. 1867716SBill.Holler@Sun.COM */ 1877716SBill.Holler@Sun.COM int idle_cpu_assert_cflush_monitor = 1; 1887716SBill.Holler@Sun.COM 189*8906SEric.Saxe@Sun.COM /* 190*8906SEric.Saxe@Sun.COM * If non-zero, idle cpus will not use power saving Deep C-States idle loop. 191*8906SEric.Saxe@Sun.COM */ 192*8906SEric.Saxe@Sun.COM int idle_cpu_no_deep_c = 0; 193*8906SEric.Saxe@Sun.COM /* 194*8906SEric.Saxe@Sun.COM * Non-power saving idle loop and wakeup pointers. 195*8906SEric.Saxe@Sun.COM * Allows user to toggle Deep Idle power saving feature on/off. 196*8906SEric.Saxe@Sun.COM */ 197*8906SEric.Saxe@Sun.COM void (*non_deep_idle_cpu)() = cpu_idle; 198*8906SEric.Saxe@Sun.COM void (*non_deep_idle_disp_enq_thread)(cpu_t *, int); 199*8906SEric.Saxe@Sun.COM 200*8906SEric.Saxe@Sun.COM /* 201*8906SEric.Saxe@Sun.COM * Object for the kernel to access the HPET. 202*8906SEric.Saxe@Sun.COM */ 203*8906SEric.Saxe@Sun.COM hpet_t hpet; 204*8906SEric.Saxe@Sun.COM 205*8906SEric.Saxe@Sun.COM #endif /* ifndef __xpv */ 2063434Sesaxe 2073434Sesaxe /*ARGSUSED*/ 2083434Sesaxe int 2093434Sesaxe pg_plat_hw_shared(cpu_t *cp, pghw_type_t hw) 2103434Sesaxe { 2113434Sesaxe switch (hw) { 2123434Sesaxe case PGHW_IPIPE: 2133434Sesaxe if (x86_feature & (X86_HTT)) { 2143434Sesaxe /* 2153434Sesaxe * Hyper-threading is SMT 2163434Sesaxe */ 2173434Sesaxe return (1); 2183434Sesaxe } else { 2193434Sesaxe return (0); 2203434Sesaxe } 2213434Sesaxe case PGHW_CHIP: 2223434Sesaxe if (x86_feature & (X86_CMP|X86_HTT)) 2233434Sesaxe return (1); 2243434Sesaxe else 2253434Sesaxe return (0); 2264606Sesaxe case PGHW_CACHE: 2274606Sesaxe if (cpuid_get_ncpu_sharing_last_cache(cp) > 1) 2284606Sesaxe return (1); 2294606Sesaxe else 2304606Sesaxe return (0); 231*8906SEric.Saxe@Sun.COM case PGHW_POW_ACTIVE: 232*8906SEric.Saxe@Sun.COM if (cpupm_domain_id(cp, CPUPM_DTYPE_ACTIVE) != (id_t)-1) 233*8906SEric.Saxe@Sun.COM return (1); 234*8906SEric.Saxe@Sun.COM else 235*8906SEric.Saxe@Sun.COM return (0); 236*8906SEric.Saxe@Sun.COM case PGHW_POW_IDLE: 237*8906SEric.Saxe@Sun.COM if (cpupm_domain_id(cp, CPUPM_DTYPE_IDLE) != (id_t)-1) 238*8906SEric.Saxe@Sun.COM return (1); 239*8906SEric.Saxe@Sun.COM else 240*8906SEric.Saxe@Sun.COM return (0); 2413434Sesaxe default: 2423434Sesaxe return (0); 2433434Sesaxe } 2443434Sesaxe } 2453434Sesaxe 2463434Sesaxe /* 2473434Sesaxe * Compare two CPUs and see if they have a pghw_type_t sharing relationship 2483434Sesaxe * If pghw_type_t is an unsupported hardware type, then return -1 2493434Sesaxe */ 2503434Sesaxe int 2513434Sesaxe pg_plat_cpus_share(cpu_t *cpu_a, cpu_t *cpu_b, pghw_type_t hw) 2520Sstevel@tonic-gate { 2533434Sesaxe id_t pgp_a, pgp_b; 2543434Sesaxe 2553434Sesaxe pgp_a = pg_plat_hw_instance_id(cpu_a, hw); 2563434Sesaxe pgp_b = pg_plat_hw_instance_id(cpu_b, hw); 2573434Sesaxe 2583434Sesaxe if (pgp_a == -1 || pgp_b == -1) 2593434Sesaxe return (-1); 2603434Sesaxe 2613434Sesaxe return (pgp_a == pgp_b); 2623434Sesaxe } 2633434Sesaxe 2643434Sesaxe /* 2653434Sesaxe * Return a physical instance identifier for known hardware sharing 2663434Sesaxe * relationships 2673434Sesaxe */ 2683434Sesaxe id_t 2693434Sesaxe pg_plat_hw_instance_id(cpu_t *cpu, pghw_type_t hw) 2703434Sesaxe { 2713434Sesaxe switch (hw) { 2723434Sesaxe case PGHW_IPIPE: 2733434Sesaxe return (cpuid_get_coreid(cpu)); 2744606Sesaxe case PGHW_CACHE: 2754606Sesaxe return (cpuid_get_last_lvl_cacheid(cpu)); 2763434Sesaxe case PGHW_CHIP: 2773434Sesaxe return (cpuid_get_chipid(cpu)); 278*8906SEric.Saxe@Sun.COM case PGHW_POW_ACTIVE: 279*8906SEric.Saxe@Sun.COM return (cpupm_domain_id(cpu, CPUPM_DTYPE_ACTIVE)); 280*8906SEric.Saxe@Sun.COM case PGHW_POW_IDLE: 281*8906SEric.Saxe@Sun.COM return (cpupm_domain_id(cpu, CPUPM_DTYPE_IDLE)); 2823434Sesaxe default: 2833434Sesaxe return (-1); 2841228Sandrei } 2853434Sesaxe } 2860Sstevel@tonic-gate 287*8906SEric.Saxe@Sun.COM /* 288*8906SEric.Saxe@Sun.COM * Express preference for optimizing for sharing relationship 289*8906SEric.Saxe@Sun.COM * hw1 vs hw2 290*8906SEric.Saxe@Sun.COM */ 291*8906SEric.Saxe@Sun.COM pghw_type_t 292*8906SEric.Saxe@Sun.COM pg_plat_hw_rank(pghw_type_t hw1, pghw_type_t hw2) 2933434Sesaxe { 294*8906SEric.Saxe@Sun.COM int i, rank1, rank2; 295*8906SEric.Saxe@Sun.COM 2963434Sesaxe static pghw_type_t hw_hier[] = { 2973434Sesaxe PGHW_IPIPE, 2984606Sesaxe PGHW_CACHE, 2993434Sesaxe PGHW_CHIP, 300*8906SEric.Saxe@Sun.COM PGHW_POW_IDLE, 301*8906SEric.Saxe@Sun.COM PGHW_POW_ACTIVE, 3023434Sesaxe PGHW_NUM_COMPONENTS 3033434Sesaxe }; 3043434Sesaxe 3053434Sesaxe for (i = 0; hw_hier[i] != PGHW_NUM_COMPONENTS; i++) { 306*8906SEric.Saxe@Sun.COM if (hw_hier[i] == hw1) 307*8906SEric.Saxe@Sun.COM rank1 = i; 308*8906SEric.Saxe@Sun.COM if (hw_hier[i] == hw2) 309*8906SEric.Saxe@Sun.COM rank2 = i; 3103434Sesaxe } 311*8906SEric.Saxe@Sun.COM 312*8906SEric.Saxe@Sun.COM if (rank1 > rank2) 313*8906SEric.Saxe@Sun.COM return (hw1); 314*8906SEric.Saxe@Sun.COM else 315*8906SEric.Saxe@Sun.COM return (hw2); 3163434Sesaxe } 3173434Sesaxe 3184606Sesaxe /* 319*8906SEric.Saxe@Sun.COM * Override the default CMT dispatcher policy for the specified 320*8906SEric.Saxe@Sun.COM * hardware sharing relationship 3214606Sesaxe */ 322*8906SEric.Saxe@Sun.COM pg_cmt_policy_t 323*8906SEric.Saxe@Sun.COM pg_plat_cmt_policy(pghw_type_t hw) 3244606Sesaxe { 325*8906SEric.Saxe@Sun.COM /* 326*8906SEric.Saxe@Sun.COM * For shared caches, also load balance across them to 327*8906SEric.Saxe@Sun.COM * maximize aggregate cache capacity 328*8906SEric.Saxe@Sun.COM */ 329*8906SEric.Saxe@Sun.COM switch (hw) { 330*8906SEric.Saxe@Sun.COM case PGHW_CACHE: 331*8906SEric.Saxe@Sun.COM return (CMT_BALANCE|CMT_AFFINITY); 332*8906SEric.Saxe@Sun.COM default: 333*8906SEric.Saxe@Sun.COM return (CMT_NO_POLICY); 334*8906SEric.Saxe@Sun.COM } 3354606Sesaxe } 3364606Sesaxe 3373434Sesaxe id_t 3383434Sesaxe pg_plat_get_core_id(cpu_t *cpu) 3393434Sesaxe { 3403434Sesaxe return ((id_t)cpuid_get_coreid(cpu)); 3413434Sesaxe } 3423434Sesaxe 3433434Sesaxe void 3443434Sesaxe cmp_set_nosteal_interval(void) 3453434Sesaxe { 3463434Sesaxe /* Set the nosteal interval (used by disp_getbest()) to 100us */ 3473434Sesaxe nosteal_nsec = 100000UL; 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate /* 3510Sstevel@tonic-gate * Routine to ensure initial callers to hrtime gets 0 as return 3520Sstevel@tonic-gate */ 3530Sstevel@tonic-gate static hrtime_t 3540Sstevel@tonic-gate dummy_hrtime(void) 3550Sstevel@tonic-gate { 3560Sstevel@tonic-gate return (0); 3570Sstevel@tonic-gate } 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate /* ARGSUSED */ 3600Sstevel@tonic-gate static void 3610Sstevel@tonic-gate dummy_scalehrtime(hrtime_t *ticks) 3620Sstevel@tonic-gate {} 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate /* 365*8906SEric.Saxe@Sun.COM * Supports Deep C-State power saving idle loop. 366*8906SEric.Saxe@Sun.COM */ 367*8906SEric.Saxe@Sun.COM void 368*8906SEric.Saxe@Sun.COM cpu_idle_adaptive(void) 369*8906SEric.Saxe@Sun.COM { 370*8906SEric.Saxe@Sun.COM (*CPU->cpu_m.mcpu_idle_cpu)(); 371*8906SEric.Saxe@Sun.COM } 372*8906SEric.Saxe@Sun.COM 373*8906SEric.Saxe@Sun.COM void 374*8906SEric.Saxe@Sun.COM cpu_dtrace_idle_probe(uint_t cstate) 375*8906SEric.Saxe@Sun.COM { 376*8906SEric.Saxe@Sun.COM cpu_t *cpup = CPU; 377*8906SEric.Saxe@Sun.COM struct machcpu *mcpu = &(cpup->cpu_m); 378*8906SEric.Saxe@Sun.COM 379*8906SEric.Saxe@Sun.COM mcpu->curr_cstate = cstate; 380*8906SEric.Saxe@Sun.COM DTRACE_PROBE1(idle__state__transition, uint_t, cstate); 381*8906SEric.Saxe@Sun.COM } 382*8906SEric.Saxe@Sun.COM 383*8906SEric.Saxe@Sun.COM /* 3843446Smrj * Idle the present CPU until awoken via an interrupt 3850Sstevel@tonic-gate */ 386*8906SEric.Saxe@Sun.COM void 3873446Smrj cpu_idle(void) 3880Sstevel@tonic-gate { 3890Sstevel@tonic-gate cpu_t *cpup = CPU; 3908408SEric.Saxe@Sun.COM processorid_t cpu_sid = cpup->cpu_seqid; 391711Sesaxe cpupart_t *cp = cpup->cpu_part; 3920Sstevel@tonic-gate int hset_update = 1; 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate /* 3950Sstevel@tonic-gate * If this CPU is online, and there's multiple CPUs 3960Sstevel@tonic-gate * in the system, then we should notate our halting 3970Sstevel@tonic-gate * by adding ourselves to the partition's halted CPU 3980Sstevel@tonic-gate * bitmap. This allows other CPUs to find/awaken us when 3990Sstevel@tonic-gate * work becomes available. 4000Sstevel@tonic-gate */ 4010Sstevel@tonic-gate if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1) 4020Sstevel@tonic-gate hset_update = 0; 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate /* 4058408SEric.Saxe@Sun.COM * Add ourselves to the partition's halted CPUs bitmap 4060Sstevel@tonic-gate * and set our HALTED flag, if necessary. 4070Sstevel@tonic-gate * 408711Sesaxe * When a thread becomes runnable, it is placed on the queue 4098408SEric.Saxe@Sun.COM * and then the halted CPU bitmap is checked to determine who 410711Sesaxe * (if anyone) should be awoken. We therefore need to first 4118408SEric.Saxe@Sun.COM * add ourselves to the bitmap, and and then check if there 4128408SEric.Saxe@Sun.COM * is any work available. The order is important to prevent a race 4138408SEric.Saxe@Sun.COM * that can lead to work languishing on a run queue somewhere while 4148408SEric.Saxe@Sun.COM * this CPU remains halted. 4158408SEric.Saxe@Sun.COM * 4168408SEric.Saxe@Sun.COM * Either the producing CPU will see we're halted and will awaken us, 4178408SEric.Saxe@Sun.COM * or this CPU will see the work available in disp_anywork(). 418711Sesaxe * 4190Sstevel@tonic-gate * Note that memory barriers after updating the HALTED flag 4208408SEric.Saxe@Sun.COM * are not necessary since an atomic operation (updating the bitset) 4210Sstevel@tonic-gate * immediately follows. On x86 the atomic operation acts as a 4220Sstevel@tonic-gate * memory barrier for the update of cpu_disp_flags. 4230Sstevel@tonic-gate */ 4240Sstevel@tonic-gate if (hset_update) { 4250Sstevel@tonic-gate cpup->cpu_disp_flags |= CPU_DISP_HALTED; 4268408SEric.Saxe@Sun.COM bitset_atomic_add(&cp->cp_haltset, cpu_sid); 4270Sstevel@tonic-gate } 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate /* 4300Sstevel@tonic-gate * Check to make sure there's really nothing to do. 431711Sesaxe * Work destined for this CPU may become available after 432711Sesaxe * this check. We'll be notified through the clearing of our 4338408SEric.Saxe@Sun.COM * bit in the halted CPU bitmap, and a poke. 4340Sstevel@tonic-gate */ 4350Sstevel@tonic-gate if (disp_anywork()) { 4360Sstevel@tonic-gate if (hset_update) { 4370Sstevel@tonic-gate cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 4388408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid); 4390Sstevel@tonic-gate } 440711Sesaxe return; 441711Sesaxe } 442711Sesaxe 443711Sesaxe /* 444711Sesaxe * We're on our way to being halted. 445711Sesaxe * 446711Sesaxe * Disable interrupts now, so that we'll awaken immediately 447711Sesaxe * after halting if someone tries to poke us between now and 448711Sesaxe * the time we actually halt. 449711Sesaxe * 450711Sesaxe * We check for the presence of our bit after disabling interrupts. 451711Sesaxe * If it's cleared, we'll return. If the bit is cleared after 452711Sesaxe * we check then the poke will pop us out of the halted state. 453711Sesaxe * 454711Sesaxe * This means that the ordering of the poke and the clearing 455711Sesaxe * of the bit by cpu_wakeup is important. 456711Sesaxe * cpu_wakeup() must clear, then poke. 4573446Smrj * cpu_idle() must disable interrupts, then check for the bit. 458711Sesaxe */ 459711Sesaxe cli(); 460711Sesaxe 4618408SEric.Saxe@Sun.COM if (hset_update && bitset_in_set(&cp->cp_haltset, cpu_sid) == 0) { 462711Sesaxe cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 463711Sesaxe sti(); 464711Sesaxe return; 465711Sesaxe } 466711Sesaxe 467711Sesaxe /* 468711Sesaxe * The check for anything locally runnable is here for performance 469711Sesaxe * and isn't needed for correctness. disp_nrunnable ought to be 470711Sesaxe * in our cache still, so it's inexpensive to check, and if there 471711Sesaxe * is anything runnable we won't have to wait for the poke. 472711Sesaxe */ 473711Sesaxe if (cpup->cpu_disp->disp_nrunnable != 0) { 474711Sesaxe if (hset_update) { 475711Sesaxe cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 4768408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid); 477711Sesaxe } 4780Sstevel@tonic-gate sti(); 4790Sstevel@tonic-gate return; 4800Sstevel@tonic-gate } 4810Sstevel@tonic-gate 482*8906SEric.Saxe@Sun.COM cpu_dtrace_idle_probe(IDLE_STATE_C1); 4837006Srv207048 4843446Smrj mach_cpu_idle(); 4850Sstevel@tonic-gate 486*8906SEric.Saxe@Sun.COM cpu_dtrace_idle_probe(IDLE_STATE_C0); 4877006Srv207048 4880Sstevel@tonic-gate /* 4890Sstevel@tonic-gate * We're no longer halted 4900Sstevel@tonic-gate */ 4910Sstevel@tonic-gate if (hset_update) { 4920Sstevel@tonic-gate cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 4938408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid); 4940Sstevel@tonic-gate } 4950Sstevel@tonic-gate } 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate /* 4990Sstevel@tonic-gate * If "cpu" is halted, then wake it up clearing its halted bit in advance. 5000Sstevel@tonic-gate * Otherwise, see if other CPUs in the cpu partition are halted and need to 5010Sstevel@tonic-gate * be woken up so that they can steal the thread we placed on this CPU. 5020Sstevel@tonic-gate * This function is only used on MP systems. 5030Sstevel@tonic-gate */ 5040Sstevel@tonic-gate static void 5050Sstevel@tonic-gate cpu_wakeup(cpu_t *cpu, int bound) 5060Sstevel@tonic-gate { 5070Sstevel@tonic-gate uint_t cpu_found; 5088408SEric.Saxe@Sun.COM processorid_t cpu_sid; 5090Sstevel@tonic-gate cpupart_t *cp; 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate cp = cpu->cpu_part; 5128408SEric.Saxe@Sun.COM cpu_sid = cpu->cpu_seqid; 5138408SEric.Saxe@Sun.COM if (bitset_in_set(&cp->cp_haltset, cpu_sid)) { 5140Sstevel@tonic-gate /* 5150Sstevel@tonic-gate * Clear the halted bit for that CPU since it will be 5160Sstevel@tonic-gate * poked in a moment. 5170Sstevel@tonic-gate */ 5188408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid); 5190Sstevel@tonic-gate /* 5200Sstevel@tonic-gate * We may find the current CPU present in the halted cpuset 5210Sstevel@tonic-gate * if we're in the context of an interrupt that occurred 5223446Smrj * before we had a chance to clear our bit in cpu_idle(). 5230Sstevel@tonic-gate * Poking ourself is obviously unnecessary, since if 5240Sstevel@tonic-gate * we're here, we're not halted. 5250Sstevel@tonic-gate */ 5260Sstevel@tonic-gate if (cpu != CPU) 5270Sstevel@tonic-gate poke_cpu(cpu->cpu_id); 5280Sstevel@tonic-gate return; 5290Sstevel@tonic-gate } else { 5300Sstevel@tonic-gate /* 5310Sstevel@tonic-gate * This cpu isn't halted, but it's idle or undergoing a 5320Sstevel@tonic-gate * context switch. No need to awaken anyone else. 5330Sstevel@tonic-gate */ 5340Sstevel@tonic-gate if (cpu->cpu_thread == cpu->cpu_idle_thread || 5350Sstevel@tonic-gate cpu->cpu_disp_flags & CPU_DISP_DONTSTEAL) 5360Sstevel@tonic-gate return; 5370Sstevel@tonic-gate } 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate /* 5408408SEric.Saxe@Sun.COM * No need to wake up other CPUs if this is for a bound thread. 5410Sstevel@tonic-gate */ 5420Sstevel@tonic-gate if (bound) 5430Sstevel@tonic-gate return; 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate /* 5468408SEric.Saxe@Sun.COM * The CPU specified for wakeup isn't currently halted, so check 5478408SEric.Saxe@Sun.COM * to see if there are any other halted CPUs in the partition, 5488408SEric.Saxe@Sun.COM * and if there are then awaken one. 5490Sstevel@tonic-gate */ 5500Sstevel@tonic-gate do { 5518408SEric.Saxe@Sun.COM cpu_found = bitset_find(&cp->cp_haltset); 5528408SEric.Saxe@Sun.COM if (cpu_found == (uint_t)-1) 5530Sstevel@tonic-gate return; 5548408SEric.Saxe@Sun.COM } while (bitset_atomic_test_and_del(&cp->cp_haltset, cpu_found) < 0); 5550Sstevel@tonic-gate 5568408SEric.Saxe@Sun.COM if (cpu_found != CPU->cpu_seqid) { 5578408SEric.Saxe@Sun.COM poke_cpu(cpu_seq[cpu_found]->cpu_id); 5588408SEric.Saxe@Sun.COM } 5590Sstevel@tonic-gate } 5600Sstevel@tonic-gate 5615084Sjohnlev #ifndef __xpv 5624481Sbholler /* 5634481Sbholler * Idle the present CPU until awoken via touching its monitored line 5644481Sbholler */ 565*8906SEric.Saxe@Sun.COM void 5664481Sbholler cpu_idle_mwait(void) 5674481Sbholler { 5684481Sbholler volatile uint32_t *mcpu_mwait = CPU->cpu_m.mcpu_mwait; 5694481Sbholler cpu_t *cpup = CPU; 5708408SEric.Saxe@Sun.COM processorid_t cpu_sid = cpup->cpu_seqid; 5714481Sbholler cpupart_t *cp = cpup->cpu_part; 5724481Sbholler int hset_update = 1; 5734481Sbholler 5744481Sbholler /* 575*8906SEric.Saxe@Sun.COM * Set our mcpu_mwait here, so we can tell if anyone tries to 5764481Sbholler * wake us between now and when we call mwait. No other cpu will 5778408SEric.Saxe@Sun.COM * attempt to set our mcpu_mwait until we add ourself to the halted 5788408SEric.Saxe@Sun.COM * CPU bitmap. 5794481Sbholler */ 5804481Sbholler *mcpu_mwait = MWAIT_HALTED; 5814481Sbholler 5824481Sbholler /* 5834481Sbholler * If this CPU is online, and there's multiple CPUs 584*8906SEric.Saxe@Sun.COM * in the system, then we should note our halting 5854481Sbholler * by adding ourselves to the partition's halted CPU 5864481Sbholler * bitmap. This allows other CPUs to find/awaken us when 5874481Sbholler * work becomes available. 5884481Sbholler */ 5894481Sbholler if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1) 5904481Sbholler hset_update = 0; 5914481Sbholler 5924481Sbholler /* 5938408SEric.Saxe@Sun.COM * Add ourselves to the partition's halted CPUs bitmap 5944481Sbholler * and set our HALTED flag, if necessary. 5954481Sbholler * 5964481Sbholler * When a thread becomes runnable, it is placed on the queue 5978408SEric.Saxe@Sun.COM * and then the halted CPU bitmap is checked to determine who 598*8906SEric.Saxe@Sun.COM * (if anyone) should be awakened. We therefore need to first 5998408SEric.Saxe@Sun.COM * add ourselves to the bitmap, and and then check if there 6004481Sbholler * is any work available. 6014481Sbholler * 6024481Sbholler * Note that memory barriers after updating the HALTED flag 6034481Sbholler * are not necessary since an atomic operation (updating the bitmap) 6044481Sbholler * immediately follows. On x86 the atomic operation acts as a 6054481Sbholler * memory barrier for the update of cpu_disp_flags. 6064481Sbholler */ 6074481Sbholler if (hset_update) { 6084481Sbholler cpup->cpu_disp_flags |= CPU_DISP_HALTED; 6098408SEric.Saxe@Sun.COM bitset_atomic_add(&cp->cp_haltset, cpu_sid); 6104481Sbholler } 6114481Sbholler 6124481Sbholler /* 6134481Sbholler * Check to make sure there's really nothing to do. 6144481Sbholler * Work destined for this CPU may become available after 6154481Sbholler * this check. We'll be notified through the clearing of our 6168408SEric.Saxe@Sun.COM * bit in the halted CPU bitmap, and a write to our mcpu_mwait. 6174481Sbholler * 6184481Sbholler * disp_anywork() checks disp_nrunnable, so we do not have to later. 6194481Sbholler */ 6204481Sbholler if (disp_anywork()) { 6214481Sbholler if (hset_update) { 6224481Sbholler cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 6238408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid); 6244481Sbholler } 6254481Sbholler return; 6264481Sbholler } 6274481Sbholler 6284481Sbholler /* 6294481Sbholler * We're on our way to being halted. 6304481Sbholler * To avoid a lost wakeup, arm the monitor before checking if another 6314481Sbholler * cpu wrote to mcpu_mwait to wake us up. 6324481Sbholler */ 6334481Sbholler i86_monitor(mcpu_mwait, 0, 0); 6344481Sbholler if (*mcpu_mwait == MWAIT_HALTED) { 635*8906SEric.Saxe@Sun.COM cpu_dtrace_idle_probe(IDLE_STATE_C1); 6365864Sesaxe 6374481Sbholler tlb_going_idle(); 6384481Sbholler i86_mwait(0, 0); 6394481Sbholler tlb_service(); 6405864Sesaxe 641*8906SEric.Saxe@Sun.COM cpu_dtrace_idle_probe(IDLE_STATE_C0); 6424481Sbholler } 6434481Sbholler 6444481Sbholler /* 6454481Sbholler * We're no longer halted 6464481Sbholler */ 6474481Sbholler if (hset_update) { 6484481Sbholler cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 6498408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid); 6504481Sbholler } 6514481Sbholler } 6524481Sbholler 6534481Sbholler /* 6544481Sbholler * If "cpu" is halted in mwait, then wake it up clearing its halted bit in 6554481Sbholler * advance. Otherwise, see if other CPUs in the cpu partition are halted and 6564481Sbholler * need to be woken up so that they can steal the thread we placed on this CPU. 6574481Sbholler * This function is only used on MP systems. 6584481Sbholler */ 6594481Sbholler static void 6604481Sbholler cpu_wakeup_mwait(cpu_t *cp, int bound) 6614481Sbholler { 6624481Sbholler cpupart_t *cpu_part; 6634481Sbholler uint_t cpu_found; 6648408SEric.Saxe@Sun.COM processorid_t cpu_sid; 6654481Sbholler 6664481Sbholler cpu_part = cp->cpu_part; 6678408SEric.Saxe@Sun.COM cpu_sid = cp->cpu_seqid; 6684481Sbholler 6694481Sbholler /* 6704481Sbholler * Clear the halted bit for that CPU since it will be woken up 6714481Sbholler * in a moment. 6724481Sbholler */ 6738408SEric.Saxe@Sun.COM if (bitset_in_set(&cpu_part->cp_haltset, cpu_sid)) { 6744481Sbholler /* 6754481Sbholler * Clear the halted bit for that CPU since it will be 6764481Sbholler * poked in a moment. 6774481Sbholler */ 6788408SEric.Saxe@Sun.COM bitset_atomic_del(&cpu_part->cp_haltset, cpu_sid); 6794481Sbholler /* 6804481Sbholler * We may find the current CPU present in the halted cpuset 6814481Sbholler * if we're in the context of an interrupt that occurred 6824481Sbholler * before we had a chance to clear our bit in cpu_idle(). 6834481Sbholler * Waking ourself is obviously unnecessary, since if 6844481Sbholler * we're here, we're not halted. 6854481Sbholler * 6864481Sbholler * monitor/mwait wakeup via writing to our cache line is 6874481Sbholler * harmless and less expensive than always checking if we 6884481Sbholler * are waking ourself which is an uncommon case. 6894481Sbholler */ 6904481Sbholler MWAIT_WAKEUP(cp); /* write to monitored line */ 6914481Sbholler return; 6924481Sbholler } else { 6934481Sbholler /* 6944481Sbholler * This cpu isn't halted, but it's idle or undergoing a 6954481Sbholler * context switch. No need to awaken anyone else. 6964481Sbholler */ 6974481Sbholler if (cp->cpu_thread == cp->cpu_idle_thread || 6984481Sbholler cp->cpu_disp_flags & CPU_DISP_DONTSTEAL) 6994481Sbholler return; 7004481Sbholler } 7014481Sbholler 7024481Sbholler /* 7034481Sbholler * No need to wake up other CPUs if the thread we just enqueued 7044481Sbholler * is bound. 7054481Sbholler */ 7068408SEric.Saxe@Sun.COM if (bound || ncpus == 1) 7074481Sbholler return; 7084481Sbholler 7094481Sbholler /* 7104481Sbholler * See if there's any other halted CPUs. If there are, then 7114481Sbholler * select one, and awaken it. 7124481Sbholler * It's possible that after we find a CPU, somebody else 7134481Sbholler * will awaken it before we get the chance. 7144481Sbholler * In that case, look again. 7154481Sbholler */ 7164481Sbholler do { 7178408SEric.Saxe@Sun.COM cpu_found = bitset_find(&cpu_part->cp_haltset); 7188408SEric.Saxe@Sun.COM if (cpu_found == (uint_t)-1) 7194481Sbholler return; 7208408SEric.Saxe@Sun.COM } while (bitset_atomic_test_and_del(&cpu_part->cp_haltset, 7218408SEric.Saxe@Sun.COM cpu_found) < 0); 7224481Sbholler 7234481Sbholler /* 7248408SEric.Saxe@Sun.COM * Do not check if cpu_found is ourself as monitor/mwait 7258408SEric.Saxe@Sun.COM * wakeup is cheap. 7264481Sbholler */ 7278408SEric.Saxe@Sun.COM MWAIT_WAKEUP(cpu_seq[cpu_found]); /* write to monitored line */ 7284481Sbholler } 7298408SEric.Saxe@Sun.COM 7305084Sjohnlev #endif 7314481Sbholler 7323446Smrj void (*cpu_pause_handler)(volatile char *) = NULL; 7333446Smrj 7340Sstevel@tonic-gate static int 7350Sstevel@tonic-gate mp_disable_intr(int cpun) 7360Sstevel@tonic-gate { 7370Sstevel@tonic-gate /* 7380Sstevel@tonic-gate * switch to the offline cpu 7390Sstevel@tonic-gate */ 7400Sstevel@tonic-gate affinity_set(cpun); 7410Sstevel@tonic-gate /* 7420Sstevel@tonic-gate * raise ipl to just below cross call 7430Sstevel@tonic-gate */ 7440Sstevel@tonic-gate splx(XC_MED_PIL-1); 7450Sstevel@tonic-gate /* 7460Sstevel@tonic-gate * set base spl to prevent the next swtch to idle from 7470Sstevel@tonic-gate * lowering back to ipl 0 7480Sstevel@tonic-gate */ 7490Sstevel@tonic-gate CPU->cpu_intr_actv |= (1 << (XC_MED_PIL-1)); 7500Sstevel@tonic-gate set_base_spl(); 7510Sstevel@tonic-gate affinity_clear(); 7520Sstevel@tonic-gate return (DDI_SUCCESS); 7530Sstevel@tonic-gate } 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate static void 7560Sstevel@tonic-gate mp_enable_intr(int cpun) 7570Sstevel@tonic-gate { 7580Sstevel@tonic-gate /* 7590Sstevel@tonic-gate * switch to the online cpu 7600Sstevel@tonic-gate */ 7610Sstevel@tonic-gate affinity_set(cpun); 7620Sstevel@tonic-gate /* 7630Sstevel@tonic-gate * clear the interrupt active mask 7640Sstevel@tonic-gate */ 7650Sstevel@tonic-gate CPU->cpu_intr_actv &= ~(1 << (XC_MED_PIL-1)); 7660Sstevel@tonic-gate set_base_spl(); 7670Sstevel@tonic-gate (void) spl0(); 7680Sstevel@tonic-gate affinity_clear(); 7690Sstevel@tonic-gate } 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate static void 7720Sstevel@tonic-gate mach_get_platform(int owner) 7730Sstevel@tonic-gate { 7740Sstevel@tonic-gate void **srv_opsp; 7750Sstevel@tonic-gate void **clt_opsp; 7760Sstevel@tonic-gate int i; 7770Sstevel@tonic-gate int total_ops; 7780Sstevel@tonic-gate 7790Sstevel@tonic-gate /* fix up psm ops */ 7800Sstevel@tonic-gate srv_opsp = (void **)mach_set[0]; 7810Sstevel@tonic-gate clt_opsp = (void **)mach_set[owner]; 7820Sstevel@tonic-gate if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01) 7830Sstevel@tonic-gate total_ops = sizeof (struct psm_ops_ver01) / 7844481Sbholler sizeof (void (*)(void)); 7850Sstevel@tonic-gate else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_1) 7860Sstevel@tonic-gate /* no psm_notify_func */ 7870Sstevel@tonic-gate total_ops = OFFSETOF(struct psm_ops, psm_notify_func) / 7880Sstevel@tonic-gate sizeof (void (*)(void)); 7890Sstevel@tonic-gate else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_2) 7900Sstevel@tonic-gate /* no psm_timer funcs */ 7910Sstevel@tonic-gate total_ops = OFFSETOF(struct psm_ops, psm_timer_reprogram) / 7920Sstevel@tonic-gate sizeof (void (*)(void)); 7930Sstevel@tonic-gate else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_3) 7940Sstevel@tonic-gate /* no psm_preshutdown function */ 7950Sstevel@tonic-gate total_ops = OFFSETOF(struct psm_ops, psm_preshutdown) / 7960Sstevel@tonic-gate sizeof (void (*)(void)); 7970Sstevel@tonic-gate else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_4) 7980Sstevel@tonic-gate /* no psm_preshutdown function */ 7990Sstevel@tonic-gate total_ops = OFFSETOF(struct psm_ops, psm_intr_ops) / 8000Sstevel@tonic-gate sizeof (void (*)(void)); 8010Sstevel@tonic-gate else 8020Sstevel@tonic-gate total_ops = sizeof (struct psm_ops) / sizeof (void (*)(void)); 8030Sstevel@tonic-gate 8040Sstevel@tonic-gate /* 8050Sstevel@tonic-gate * Save the version of the PSM module, in case we need to 8060Sstevel@tonic-gate * bahave differently based on version. 8070Sstevel@tonic-gate */ 8080Sstevel@tonic-gate mach_ver[0] = mach_ver[owner]; 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate for (i = 0; i < total_ops; i++) 8110Sstevel@tonic-gate if (clt_opsp[i] != NULL) 8120Sstevel@tonic-gate srv_opsp[i] = clt_opsp[i]; 8130Sstevel@tonic-gate } 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate static void 8160Sstevel@tonic-gate mach_construct_info() 8170Sstevel@tonic-gate { 8183446Smrj struct psm_sw *swp; 8190Sstevel@tonic-gate int mach_cnt[PSM_OWN_OVERRIDE+1] = {0}; 8200Sstevel@tonic-gate int conflict_owner = 0; 8210Sstevel@tonic-gate 8220Sstevel@tonic-gate if (psmsw->psw_forw == psmsw) 8230Sstevel@tonic-gate panic("No valid PSM modules found"); 8240Sstevel@tonic-gate mutex_enter(&psmsw_lock); 8250Sstevel@tonic-gate for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) { 8260Sstevel@tonic-gate if (!(swp->psw_flag & PSM_MOD_IDENTIFY)) 8270Sstevel@tonic-gate continue; 8280Sstevel@tonic-gate mach_set[swp->psw_infop->p_owner] = swp->psw_infop->p_ops; 8290Sstevel@tonic-gate mach_ver[swp->psw_infop->p_owner] = swp->psw_infop->p_version; 8300Sstevel@tonic-gate mach_cnt[swp->psw_infop->p_owner]++; 8310Sstevel@tonic-gate } 8320Sstevel@tonic-gate mutex_exit(&psmsw_lock); 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate mach_get_platform(PSM_OWN_SYS_DEFAULT); 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate /* check to see are there any conflicts */ 8370Sstevel@tonic-gate if (mach_cnt[PSM_OWN_EXCLUSIVE] > 1) 8380Sstevel@tonic-gate conflict_owner = PSM_OWN_EXCLUSIVE; 8390Sstevel@tonic-gate if (mach_cnt[PSM_OWN_OVERRIDE] > 1) 8400Sstevel@tonic-gate conflict_owner = PSM_OWN_OVERRIDE; 8410Sstevel@tonic-gate if (conflict_owner) { 8420Sstevel@tonic-gate /* remove all psm modules except uppc */ 8430Sstevel@tonic-gate cmn_err(CE_WARN, 8444481Sbholler "Conflicts detected on the following PSM modules:"); 8450Sstevel@tonic-gate mutex_enter(&psmsw_lock); 8460Sstevel@tonic-gate for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) { 8470Sstevel@tonic-gate if (swp->psw_infop->p_owner == conflict_owner) 8480Sstevel@tonic-gate cmn_err(CE_WARN, "%s ", 8494481Sbholler swp->psw_infop->p_mach_idstring); 8500Sstevel@tonic-gate } 8510Sstevel@tonic-gate mutex_exit(&psmsw_lock); 8520Sstevel@tonic-gate cmn_err(CE_WARN, 8534481Sbholler "Setting the system back to SINGLE processor mode!"); 8540Sstevel@tonic-gate cmn_err(CE_WARN, 8550Sstevel@tonic-gate "Please edit /etc/mach to remove the invalid PSM module."); 8560Sstevel@tonic-gate return; 8570Sstevel@tonic-gate } 8580Sstevel@tonic-gate 8590Sstevel@tonic-gate if (mach_set[PSM_OWN_EXCLUSIVE]) 8600Sstevel@tonic-gate mach_get_platform(PSM_OWN_EXCLUSIVE); 8610Sstevel@tonic-gate 8620Sstevel@tonic-gate if (mach_set[PSM_OWN_OVERRIDE]) 8630Sstevel@tonic-gate mach_get_platform(PSM_OWN_OVERRIDE); 8640Sstevel@tonic-gate } 8650Sstevel@tonic-gate 8660Sstevel@tonic-gate static void 8670Sstevel@tonic-gate mach_init() 8680Sstevel@tonic-gate { 8693446Smrj struct psm_ops *pops; 8700Sstevel@tonic-gate 8710Sstevel@tonic-gate mach_construct_info(); 8720Sstevel@tonic-gate 8730Sstevel@tonic-gate pops = mach_set[0]; 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate /* register the interrupt and clock initialization rotuines */ 8760Sstevel@tonic-gate picinitf = mach_picinit; 8770Sstevel@tonic-gate clkinitf = mach_clkinit; 8780Sstevel@tonic-gate psm_get_clockirq = pops->psm_get_clockirq; 8790Sstevel@tonic-gate 8800Sstevel@tonic-gate /* register the interrupt setup code */ 8810Sstevel@tonic-gate slvltovect = mach_softlvl_to_vect; 8820Sstevel@tonic-gate addspl = pops->psm_addspl; 8830Sstevel@tonic-gate delspl = pops->psm_delspl; 8840Sstevel@tonic-gate 8850Sstevel@tonic-gate if (pops->psm_translate_irq) 8860Sstevel@tonic-gate psm_translate_irq = pops->psm_translate_irq; 8870Sstevel@tonic-gate if (pops->psm_intr_ops) 8880Sstevel@tonic-gate psm_intr_ops = pops->psm_intr_ops; 8893446Smrj 8903446Smrj #if defined(PSMI_1_2) || defined(PSMI_1_3) || defined(PSMI_1_4) 8913446Smrj /* 8923446Smrj * Time-of-day functionality now handled in TOD modules. 8933446Smrj * (Warn about PSM modules that think that we're going to use 8943446Smrj * their ops vectors.) 8953446Smrj */ 8963446Smrj if (pops->psm_tod_get) 8973446Smrj cmn_err(CE_WARN, "obsolete psm_tod_get op %p", 8983446Smrj (void *)pops->psm_tod_get); 8993446Smrj 9003446Smrj if (pops->psm_tod_set) 9013446Smrj cmn_err(CE_WARN, "obsolete psm_tod_set op %p", 9023446Smrj (void *)pops->psm_tod_set); 9033446Smrj #endif 9043446Smrj 9050Sstevel@tonic-gate if (pops->psm_notify_error) { 9060Sstevel@tonic-gate psm_notify_error = mach_notify_error; 9070Sstevel@tonic-gate notify_error = pops->psm_notify_error; 9080Sstevel@tonic-gate } 9090Sstevel@tonic-gate 9100Sstevel@tonic-gate (*pops->psm_softinit)(); 9110Sstevel@tonic-gate 9120Sstevel@tonic-gate /* 913*8906SEric.Saxe@Sun.COM * Initialize the dispatcher's function hooks to enable CPU halting 914*8906SEric.Saxe@Sun.COM * when idle. Set both the deep-idle and non-deep-idle hooks. 915*8906SEric.Saxe@Sun.COM * 916*8906SEric.Saxe@Sun.COM * Assume we can use power saving deep-idle loop cpu_idle_adaptive. 917*8906SEric.Saxe@Sun.COM * Platform deep-idle driver will reset our idle loop to 918*8906SEric.Saxe@Sun.COM * non_deep_idle_cpu if power saving deep-idle feature is not available. 919*8906SEric.Saxe@Sun.COM * 9205045Sbholler * Do not use monitor/mwait if idle_cpu_use_hlt is not set(spin idle) 9215045Sbholler * or idle_cpu_prefer_mwait is not set. 9224481Sbholler * Allocate monitor/mwait buffer for cpu0. 9230Sstevel@tonic-gate */ 924*8906SEric.Saxe@Sun.COM #ifndef __xpv 925*8906SEric.Saxe@Sun.COM non_deep_idle_disp_enq_thread = disp_enq_thread; 926*8906SEric.Saxe@Sun.COM #endif 9274481Sbholler if (idle_cpu_use_hlt) { 928*8906SEric.Saxe@Sun.COM idle_cpu = cpu_idle_adaptive; 929*8906SEric.Saxe@Sun.COM CPU->cpu_m.mcpu_idle_cpu = cpu_idle; 9305084Sjohnlev #ifndef __xpv 9314481Sbholler if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait) { 9325045Sbholler CPU->cpu_m.mcpu_mwait = cpuid_mwait_alloc(CPU); 9335045Sbholler /* 9345045Sbholler * Protect ourself from insane mwait size. 9355045Sbholler */ 9365045Sbholler if (CPU->cpu_m.mcpu_mwait == NULL) { 9375045Sbholler #ifdef DEBUG 9385045Sbholler cmn_err(CE_NOTE, "Using hlt idle. Cannot " 9395045Sbholler "handle cpu 0 mwait size."); 9405045Sbholler #endif 9415045Sbholler idle_cpu_prefer_mwait = 0; 942*8906SEric.Saxe@Sun.COM CPU->cpu_m.mcpu_idle_cpu = cpu_idle; 9435045Sbholler } else { 944*8906SEric.Saxe@Sun.COM CPU->cpu_m.mcpu_idle_cpu = cpu_idle_mwait; 9455045Sbholler } 9464481Sbholler } else { 947*8906SEric.Saxe@Sun.COM CPU->cpu_m.mcpu_idle_cpu = cpu_idle; 948*8906SEric.Saxe@Sun.COM } 949*8906SEric.Saxe@Sun.COM non_deep_idle_cpu = CPU->cpu_m.mcpu_idle_cpu; 950*8906SEric.Saxe@Sun.COM 951*8906SEric.Saxe@Sun.COM /* 952*8906SEric.Saxe@Sun.COM * Disable power saving deep idle loop? 953*8906SEric.Saxe@Sun.COM */ 954*8906SEric.Saxe@Sun.COM if (idle_cpu_no_deep_c) { 955*8906SEric.Saxe@Sun.COM idle_cpu = non_deep_idle_cpu; 9564481Sbholler } 9575084Sjohnlev #endif 9584481Sbholler } 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate mach_smpinit(); 9610Sstevel@tonic-gate } 9620Sstevel@tonic-gate 9630Sstevel@tonic-gate static void 9640Sstevel@tonic-gate mach_smpinit(void) 9650Sstevel@tonic-gate { 9662006Sandrei struct psm_ops *pops; 9672006Sandrei processorid_t cpu_id; 9682006Sandrei int cnt; 9692006Sandrei cpuset_t cpumask; 9700Sstevel@tonic-gate 9710Sstevel@tonic-gate pops = mach_set[0]; 9726336Sbholler CPUSET_ZERO(cpumask); 9730Sstevel@tonic-gate 9740Sstevel@tonic-gate cpu_id = -1; 9750Sstevel@tonic-gate cpu_id = (*pops->psm_get_next_processorid)(cpu_id); 9766336Sbholler for (cnt = 0; cpu_id != -1; cnt++) { 9772006Sandrei CPUSET_ADD(cpumask, cpu_id); 9780Sstevel@tonic-gate cpu_id = (*pops->psm_get_next_processorid)(cpu_id); 9790Sstevel@tonic-gate } 9800Sstevel@tonic-gate 9810Sstevel@tonic-gate mp_cpus = cpumask; 9820Sstevel@tonic-gate 9830Sstevel@tonic-gate /* MP related routines */ 9840Sstevel@tonic-gate ap_mlsetup = pops->psm_post_cpu_start; 9850Sstevel@tonic-gate send_dirintf = pops->psm_send_ipi; 9860Sstevel@tonic-gate 9870Sstevel@tonic-gate /* optional MP related routines */ 9880Sstevel@tonic-gate if (pops->psm_shutdown) 9890Sstevel@tonic-gate psm_shutdownf = pops->psm_shutdown; 9900Sstevel@tonic-gate if (pops->psm_preshutdown) 9910Sstevel@tonic-gate psm_preshutdownf = pops->psm_preshutdown; 9920Sstevel@tonic-gate if (pops->psm_notify_func) 9930Sstevel@tonic-gate psm_notifyf = pops->psm_notify_func; 9940Sstevel@tonic-gate if (pops->psm_set_idlecpu) 9950Sstevel@tonic-gate psm_set_idle_cpuf = pops->psm_set_idlecpu; 9960Sstevel@tonic-gate if (pops->psm_unset_idlecpu) 9970Sstevel@tonic-gate psm_unset_idle_cpuf = pops->psm_unset_idlecpu; 9980Sstevel@tonic-gate 9990Sstevel@tonic-gate psm_clkinit = pops->psm_clkinit; 10000Sstevel@tonic-gate 10010Sstevel@tonic-gate if (pops->psm_timer_reprogram) 10020Sstevel@tonic-gate psm_timer_reprogram = pops->psm_timer_reprogram; 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate if (pops->psm_timer_enable) 10050Sstevel@tonic-gate psm_timer_enable = pops->psm_timer_enable; 10060Sstevel@tonic-gate 10070Sstevel@tonic-gate if (pops->psm_timer_disable) 10080Sstevel@tonic-gate psm_timer_disable = pops->psm_timer_disable; 10090Sstevel@tonic-gate 10100Sstevel@tonic-gate if (pops->psm_post_cyclic_setup) 10110Sstevel@tonic-gate psm_post_cyclic_setup = pops->psm_post_cyclic_setup; 10120Sstevel@tonic-gate 10135295Srandyf if (pops->psm_state) 10145295Srandyf psm_state = pops->psm_state; 10155295Srandyf 10167113Sbholler /* 10177113Sbholler * Set these vectors here so they can be used by Suspend/Resume 10187113Sbholler * on UP machines. 10197113Sbholler */ 10207113Sbholler if (pops->psm_disable_intr) 10217113Sbholler psm_disable_intr = pops->psm_disable_intr; 10227113Sbholler if (pops->psm_enable_intr) 10237113Sbholler psm_enable_intr = pops->psm_enable_intr; 10247113Sbholler 10257113Sbholler /* check for multiple CPUs */ 10260Sstevel@tonic-gate if (cnt < 2) 10270Sstevel@tonic-gate return; 10280Sstevel@tonic-gate 10290Sstevel@tonic-gate /* check for MP platforms */ 10300Sstevel@tonic-gate if (pops->psm_cpu_start == NULL) 10310Sstevel@tonic-gate return; 10320Sstevel@tonic-gate 10330Sstevel@tonic-gate /* 10340Sstevel@tonic-gate * Set the dispatcher hook to enable cpu "wake up" 10350Sstevel@tonic-gate * when a thread becomes runnable. 10360Sstevel@tonic-gate */ 10375084Sjohnlev if (idle_cpu_use_hlt) { 10385084Sjohnlev disp_enq_thread = cpu_wakeup; 10395084Sjohnlev #ifndef __xpv 10404481Sbholler if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait) 10414481Sbholler disp_enq_thread = cpu_wakeup_mwait; 1042*8906SEric.Saxe@Sun.COM non_deep_idle_disp_enq_thread = disp_enq_thread; 10435084Sjohnlev #endif 10445084Sjohnlev } 10450Sstevel@tonic-gate 10460Sstevel@tonic-gate psm_get_ipivect = pops->psm_get_ipivect; 10470Sstevel@tonic-gate 10480Sstevel@tonic-gate (void) add_avintr((void *)NULL, XC_HI_PIL, xc_serv, "xc_hi_intr", 10494481Sbholler (*pops->psm_get_ipivect)(XC_HI_PIL, PSM_INTR_IPI_HI), 10504481Sbholler (caddr_t)X_CALL_HIPRI, NULL, NULL, NULL); 10510Sstevel@tonic-gate (void) add_avintr((void *)NULL, XC_MED_PIL, xc_serv, "xc_med_intr", 10524481Sbholler (*pops->psm_get_ipivect)(XC_MED_PIL, PSM_INTR_IPI_LO), 10534481Sbholler (caddr_t)X_CALL_MEDPRI, NULL, NULL, NULL); 10540Sstevel@tonic-gate 10550Sstevel@tonic-gate (void) (*pops->psm_get_ipivect)(XC_CPUPOKE_PIL, PSM_INTR_POKE); 10560Sstevel@tonic-gate } 10570Sstevel@tonic-gate 10580Sstevel@tonic-gate static void 10590Sstevel@tonic-gate mach_picinit() 10600Sstevel@tonic-gate { 10612006Sandrei struct psm_ops *pops; 10620Sstevel@tonic-gate 10630Sstevel@tonic-gate pops = mach_set[0]; 10640Sstevel@tonic-gate 10650Sstevel@tonic-gate /* register the interrupt handlers */ 10660Sstevel@tonic-gate setlvl = pops->psm_intr_enter; 10670Sstevel@tonic-gate setlvlx = pops->psm_intr_exit; 10680Sstevel@tonic-gate 10690Sstevel@tonic-gate /* initialize the interrupt hardware */ 10700Sstevel@tonic-gate (*pops->psm_picinit)(); 10710Sstevel@tonic-gate 10720Sstevel@tonic-gate /* set interrupt mask for current ipl */ 10730Sstevel@tonic-gate setspl = pops->psm_setspl; 10743446Smrj cli(); 10750Sstevel@tonic-gate setspl(CPU->cpu_pri); 10760Sstevel@tonic-gate } 10770Sstevel@tonic-gate 10780Sstevel@tonic-gate uint_t cpu_freq; /* MHz */ 10790Sstevel@tonic-gate uint64_t cpu_freq_hz; /* measured (in hertz) */ 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate #define MEGA_HZ 1000000 10820Sstevel@tonic-gate 10835084Sjohnlev #ifdef __xpv 10845084Sjohnlev 10855084Sjohnlev int xpv_cpufreq_workaround = 1; 10865084Sjohnlev int xpv_cpufreq_verbose = 0; 10875084Sjohnlev 10885084Sjohnlev #else /* __xpv */ 10895084Sjohnlev 10900Sstevel@tonic-gate static uint64_t 10910Sstevel@tonic-gate mach_calchz(uint32_t pit_counter, uint64_t *processor_clks) 10920Sstevel@tonic-gate { 10930Sstevel@tonic-gate uint64_t cpu_hz; 10940Sstevel@tonic-gate 10950Sstevel@tonic-gate if ((pit_counter == 0) || (*processor_clks == 0) || 10960Sstevel@tonic-gate (*processor_clks > (((uint64_t)-1) / PIT_HZ))) 10970Sstevel@tonic-gate return (0); 10980Sstevel@tonic-gate 10990Sstevel@tonic-gate cpu_hz = ((uint64_t)PIT_HZ * *processor_clks) / pit_counter; 11000Sstevel@tonic-gate 11010Sstevel@tonic-gate return (cpu_hz); 11020Sstevel@tonic-gate } 11030Sstevel@tonic-gate 11045084Sjohnlev #endif /* __xpv */ 11055084Sjohnlev 11060Sstevel@tonic-gate static uint64_t 11070Sstevel@tonic-gate mach_getcpufreq(void) 11080Sstevel@tonic-gate { 11095084Sjohnlev #if defined(__xpv) 11105084Sjohnlev vcpu_time_info_t *vti = &CPU->cpu_m.mcpu_vcpu_info->time; 11115084Sjohnlev uint64_t cpu_hz; 11125084Sjohnlev 11135084Sjohnlev /* 11145084Sjohnlev * During dom0 bringup, it was noted that on at least one older 11155084Sjohnlev * Intel HT machine, the hypervisor initially gives a tsc_to_system_mul 11165084Sjohnlev * value that is quite wrong (the 3.06GHz clock was reported 11175084Sjohnlev * as 4.77GHz) 11185084Sjohnlev * 11195084Sjohnlev * The curious thing is, that if you stop the kernel at entry, 11205084Sjohnlev * breakpoint here and inspect the value with kmdb, the value 11215084Sjohnlev * is correct - but if you don't stop and simply enable the 11225084Sjohnlev * printf statement (below), you can see the bad value printed 11235084Sjohnlev * here. Almost as if something kmdb did caused the hypervisor to 11245084Sjohnlev * figure it out correctly. And, note that the hypervisor 11255084Sjohnlev * eventually -does- figure it out correctly ... if you look at 11265084Sjohnlev * the field later in the life of dom0, it is correct. 11275084Sjohnlev * 11285084Sjohnlev * For now, on dom0, we employ a slightly cheesy workaround of 11295084Sjohnlev * using the DOM0_PHYSINFO hypercall. 11305084Sjohnlev */ 11315084Sjohnlev if (DOMAIN_IS_INITDOMAIN(xen_info) && xpv_cpufreq_workaround) { 11325084Sjohnlev xen_sysctl_t op0, *op = &op0; 11335084Sjohnlev 11345084Sjohnlev op->cmd = XEN_SYSCTL_physinfo; 11355084Sjohnlev op->interface_version = XEN_SYSCTL_INTERFACE_VERSION; 11365084Sjohnlev if (HYPERVISOR_sysctl(op) != 0) 11375084Sjohnlev panic("physinfo op refused"); 11385084Sjohnlev 11395084Sjohnlev cpu_hz = 1000 * (uint64_t)op->u.physinfo.cpu_khz; 11405084Sjohnlev } else { 11415084Sjohnlev cpu_hz = (UINT64_C(1000000000) << 32) / vti->tsc_to_system_mul; 11425084Sjohnlev 11435084Sjohnlev if (vti->tsc_shift < 0) 11445084Sjohnlev cpu_hz <<= -vti->tsc_shift; 11455084Sjohnlev else 11465084Sjohnlev cpu_hz >>= vti->tsc_shift; 11475084Sjohnlev } 11485084Sjohnlev 11495084Sjohnlev if (xpv_cpufreq_verbose) 11505084Sjohnlev printf("mach_getcpufreq: system_mul 0x%x, shift %d, " 11515084Sjohnlev "cpu_hz %" PRId64 "Hz\n", 11525084Sjohnlev vti->tsc_to_system_mul, vti->tsc_shift, cpu_hz); 11535084Sjohnlev 11545084Sjohnlev return (cpu_hz); 11555084Sjohnlev #else /* __xpv */ 11560Sstevel@tonic-gate uint32_t pit_counter; 11570Sstevel@tonic-gate uint64_t processor_clks; 11580Sstevel@tonic-gate 11590Sstevel@tonic-gate if (x86_feature & X86_TSC) { 11600Sstevel@tonic-gate /* 11610Sstevel@tonic-gate * We have a TSC. freq_tsc() knows how to measure the number 11620Sstevel@tonic-gate * of clock cycles sampled against the PIT. 11630Sstevel@tonic-gate */ 11643446Smrj ulong_t flags = clear_int_flag(); 11650Sstevel@tonic-gate processor_clks = freq_tsc(&pit_counter); 11663446Smrj restore_int_flag(flags); 11670Sstevel@tonic-gate return (mach_calchz(pit_counter, &processor_clks)); 11680Sstevel@tonic-gate } else if (x86_vendor == X86_VENDOR_Cyrix || x86_type == X86_TYPE_P5) { 11690Sstevel@tonic-gate #if defined(__amd64) 11700Sstevel@tonic-gate panic("mach_getcpufreq: no TSC!"); 11710Sstevel@tonic-gate #elif defined(__i386) 11720Sstevel@tonic-gate /* 11730Sstevel@tonic-gate * We are a Cyrix based on a 6x86 core or an Intel Pentium 11740Sstevel@tonic-gate * for which freq_notsc() knows how to measure the number of 11750Sstevel@tonic-gate * elapsed clock cycles sampled against the PIT 11760Sstevel@tonic-gate */ 11773446Smrj ulong_t flags = clear_int_flag(); 11780Sstevel@tonic-gate processor_clks = freq_notsc(&pit_counter); 11793446Smrj restore_int_flag(flags); 11800Sstevel@tonic-gate return (mach_calchz(pit_counter, &processor_clks)); 11810Sstevel@tonic-gate #endif /* __i386 */ 11820Sstevel@tonic-gate } 11830Sstevel@tonic-gate 11840Sstevel@tonic-gate /* We do not know how to calculate cpu frequency for this cpu. */ 11850Sstevel@tonic-gate return (0); 11865084Sjohnlev #endif /* __xpv */ 11870Sstevel@tonic-gate } 11880Sstevel@tonic-gate 11890Sstevel@tonic-gate /* 11900Sstevel@tonic-gate * If the clock speed of a cpu is found to be reported incorrectly, do not add 11910Sstevel@tonic-gate * to this array, instead improve the accuracy of the algorithm that determines 11920Sstevel@tonic-gate * the clock speed of the processor or extend the implementation to support the 11930Sstevel@tonic-gate * vendor as appropriate. This is here only to support adjusting the speed on 11940Sstevel@tonic-gate * older slower processors that mach_fixcpufreq() would not be able to account 11950Sstevel@tonic-gate * for otherwise. 11960Sstevel@tonic-gate */ 11970Sstevel@tonic-gate static int x86_cpu_freq[] = { 60, 75, 80, 90, 120, 160, 166, 175, 180, 233 }; 11980Sstevel@tonic-gate 11990Sstevel@tonic-gate /* 12000Sstevel@tonic-gate * On fast processors the clock frequency that is measured may be off by 12010Sstevel@tonic-gate * a few MHz from the value printed on the part. This is a combination of 12020Sstevel@tonic-gate * the factors that for such fast parts being off by this much is within 12030Sstevel@tonic-gate * the tolerances for manufacture and because of the difficulties in the 12040Sstevel@tonic-gate * measurement that can lead to small error. This function uses some 12050Sstevel@tonic-gate * heuristics in order to tweak the value that was measured to match what 12060Sstevel@tonic-gate * is most likely printed on the part. 12070Sstevel@tonic-gate * 12080Sstevel@tonic-gate * Some examples: 12090Sstevel@tonic-gate * AMD Athlon 1000 mhz measured as 998 mhz 12100Sstevel@tonic-gate * Intel Pentium III Xeon 733 mhz measured as 731 mhz 12110Sstevel@tonic-gate * Intel Pentium IV 1500 mhz measured as 1495mhz 12120Sstevel@tonic-gate * 12130Sstevel@tonic-gate * If in the future this function is no longer sufficient to correct 12140Sstevel@tonic-gate * for the error in the measurement, then the algorithm used to perform 12150Sstevel@tonic-gate * the measurement will have to be improved in order to increase accuracy 12160Sstevel@tonic-gate * rather than adding horrible and questionable kludges here. 12170Sstevel@tonic-gate * 12180Sstevel@tonic-gate * This is called after the cyclics subsystem because of the potential 12190Sstevel@tonic-gate * that the heuristics within may give a worse estimate of the clock 12200Sstevel@tonic-gate * frequency than the value that was measured. 12210Sstevel@tonic-gate */ 12220Sstevel@tonic-gate static void 12230Sstevel@tonic-gate mach_fixcpufreq(void) 12240Sstevel@tonic-gate { 12250Sstevel@tonic-gate uint32_t freq, mul, near66, delta66, near50, delta50, fixed, delta, i; 12260Sstevel@tonic-gate 12270Sstevel@tonic-gate freq = (uint32_t)cpu_freq; 12280Sstevel@tonic-gate 12290Sstevel@tonic-gate /* 12300Sstevel@tonic-gate * Find the nearest integer multiple of 200/3 (about 66) MHz to the 12310Sstevel@tonic-gate * measured speed taking into account that the 667 MHz parts were 12320Sstevel@tonic-gate * the first to round-up. 12330Sstevel@tonic-gate */ 12340Sstevel@tonic-gate mul = (uint32_t)((3 * (uint64_t)freq + 100) / 200); 12350Sstevel@tonic-gate near66 = (uint32_t)((200 * (uint64_t)mul + ((mul >= 10) ? 1 : 0)) / 3); 12360Sstevel@tonic-gate delta66 = (near66 > freq) ? (near66 - freq) : (freq - near66); 12370Sstevel@tonic-gate 12380Sstevel@tonic-gate /* Find the nearest integer multiple of 50 MHz to the measured speed */ 12390Sstevel@tonic-gate mul = (freq + 25) / 50; 12400Sstevel@tonic-gate near50 = mul * 50; 12410Sstevel@tonic-gate delta50 = (near50 > freq) ? (near50 - freq) : (freq - near50); 12420Sstevel@tonic-gate 12430Sstevel@tonic-gate /* Find the closer of the two */ 12440Sstevel@tonic-gate if (delta66 < delta50) { 12450Sstevel@tonic-gate fixed = near66; 12460Sstevel@tonic-gate delta = delta66; 12470Sstevel@tonic-gate } else { 12480Sstevel@tonic-gate fixed = near50; 12490Sstevel@tonic-gate delta = delta50; 12500Sstevel@tonic-gate } 12510Sstevel@tonic-gate 12520Sstevel@tonic-gate if (fixed > INT_MAX) 12530Sstevel@tonic-gate return; 12540Sstevel@tonic-gate 12550Sstevel@tonic-gate /* 12560Sstevel@tonic-gate * Some older parts have a core clock frequency that is not an 12570Sstevel@tonic-gate * integral multiple of 50 or 66 MHz. Check if one of the old 12580Sstevel@tonic-gate * clock frequencies is closer to the measured value than any 12590Sstevel@tonic-gate * of the integral multiples of 50 an 66, and if so set fixed 12600Sstevel@tonic-gate * and delta appropriately to represent the closest value. 12610Sstevel@tonic-gate */ 12620Sstevel@tonic-gate i = sizeof (x86_cpu_freq) / sizeof (int); 12630Sstevel@tonic-gate while (i > 0) { 12640Sstevel@tonic-gate i--; 12650Sstevel@tonic-gate 12660Sstevel@tonic-gate if (x86_cpu_freq[i] <= freq) { 12670Sstevel@tonic-gate mul = freq - x86_cpu_freq[i]; 12680Sstevel@tonic-gate 12690Sstevel@tonic-gate if (mul < delta) { 12700Sstevel@tonic-gate fixed = x86_cpu_freq[i]; 12710Sstevel@tonic-gate delta = mul; 12720Sstevel@tonic-gate } 12730Sstevel@tonic-gate 12740Sstevel@tonic-gate break; 12750Sstevel@tonic-gate } 12760Sstevel@tonic-gate 12770Sstevel@tonic-gate mul = x86_cpu_freq[i] - freq; 12780Sstevel@tonic-gate 12790Sstevel@tonic-gate if (mul < delta) { 12800Sstevel@tonic-gate fixed = x86_cpu_freq[i]; 12810Sstevel@tonic-gate delta = mul; 12820Sstevel@tonic-gate } 12830Sstevel@tonic-gate } 12840Sstevel@tonic-gate 12850Sstevel@tonic-gate /* 12860Sstevel@tonic-gate * Set a reasonable maximum for how much to correct the measured 12870Sstevel@tonic-gate * result by. This check is here to prevent the adjustment made 12880Sstevel@tonic-gate * by this function from being more harm than good. It is entirely 12890Sstevel@tonic-gate * possible that in the future parts will be made that are not 12900Sstevel@tonic-gate * integral multiples of 66 or 50 in clock frequency or that 12910Sstevel@tonic-gate * someone may overclock a part to some odd frequency. If the 12920Sstevel@tonic-gate * measured value is farther from the corrected value than 12930Sstevel@tonic-gate * allowed, then assume the corrected value is in error and use 12940Sstevel@tonic-gate * the measured value. 12950Sstevel@tonic-gate */ 12960Sstevel@tonic-gate if (6 < delta) 12970Sstevel@tonic-gate return; 12980Sstevel@tonic-gate 12990Sstevel@tonic-gate cpu_freq = (int)fixed; 13000Sstevel@tonic-gate } 13010Sstevel@tonic-gate 13020Sstevel@tonic-gate 13030Sstevel@tonic-gate static int 13040Sstevel@tonic-gate machhztomhz(uint64_t cpu_freq_hz) 13050Sstevel@tonic-gate { 13060Sstevel@tonic-gate uint64_t cpu_mhz; 13070Sstevel@tonic-gate 13080Sstevel@tonic-gate /* Round to nearest MHZ */ 13090Sstevel@tonic-gate cpu_mhz = (cpu_freq_hz + (MEGA_HZ / 2)) / MEGA_HZ; 13100Sstevel@tonic-gate 13110Sstevel@tonic-gate if (cpu_mhz > INT_MAX) 13120Sstevel@tonic-gate return (0); 13130Sstevel@tonic-gate 13140Sstevel@tonic-gate return ((int)cpu_mhz); 13150Sstevel@tonic-gate 13160Sstevel@tonic-gate } 13170Sstevel@tonic-gate 13180Sstevel@tonic-gate 13190Sstevel@tonic-gate static int 13200Sstevel@tonic-gate mach_clkinit(int preferred_mode, int *set_mode) 13210Sstevel@tonic-gate { 13223446Smrj struct psm_ops *pops; 13230Sstevel@tonic-gate int resolution; 13240Sstevel@tonic-gate 13250Sstevel@tonic-gate pops = mach_set[0]; 13260Sstevel@tonic-gate 13270Sstevel@tonic-gate cpu_freq_hz = mach_getcpufreq(); 13280Sstevel@tonic-gate 13290Sstevel@tonic-gate cpu_freq = machhztomhz(cpu_freq_hz); 13300Sstevel@tonic-gate 13310Sstevel@tonic-gate if (!(x86_feature & X86_TSC) || (cpu_freq == 0)) 13320Sstevel@tonic-gate tsc_gethrtime_enable = 0; 13330Sstevel@tonic-gate 13345084Sjohnlev #ifndef __xpv 13350Sstevel@tonic-gate if (tsc_gethrtime_enable) { 13360Sstevel@tonic-gate tsc_hrtimeinit(cpu_freq_hz); 13375084Sjohnlev } else 13385084Sjohnlev #endif 13395084Sjohnlev { 13400Sstevel@tonic-gate if (pops->psm_hrtimeinit) 13410Sstevel@tonic-gate (*pops->psm_hrtimeinit)(); 13420Sstevel@tonic-gate gethrtimef = pops->psm_gethrtime; 13430Sstevel@tonic-gate gethrtimeunscaledf = gethrtimef; 13440Sstevel@tonic-gate /* scalehrtimef will remain dummy */ 13450Sstevel@tonic-gate } 13460Sstevel@tonic-gate 13470Sstevel@tonic-gate mach_fixcpufreq(); 13480Sstevel@tonic-gate 13490Sstevel@tonic-gate if (mach_ver[0] >= PSM_INFO_VER01_3) { 13505084Sjohnlev if (preferred_mode == TIMER_ONESHOT) { 13510Sstevel@tonic-gate 13520Sstevel@tonic-gate resolution = (*pops->psm_clkinit)(0); 13530Sstevel@tonic-gate if (resolution != 0) { 13540Sstevel@tonic-gate *set_mode = TIMER_ONESHOT; 13550Sstevel@tonic-gate return (resolution); 13560Sstevel@tonic-gate } 13570Sstevel@tonic-gate } 13580Sstevel@tonic-gate 13590Sstevel@tonic-gate /* 13600Sstevel@tonic-gate * either periodic mode was requested or could not set to 13610Sstevel@tonic-gate * one-shot mode 13620Sstevel@tonic-gate */ 13630Sstevel@tonic-gate resolution = (*pops->psm_clkinit)(hz); 13640Sstevel@tonic-gate /* 13650Sstevel@tonic-gate * psm should be able to do periodic, so we do not check 13660Sstevel@tonic-gate * for return value of psm_clkinit here. 13670Sstevel@tonic-gate */ 13680Sstevel@tonic-gate *set_mode = TIMER_PERIODIC; 13690Sstevel@tonic-gate return (resolution); 13700Sstevel@tonic-gate } else { 13710Sstevel@tonic-gate /* 13720Sstevel@tonic-gate * PSMI interface prior to PSMI_3 does not define a return 13730Sstevel@tonic-gate * value for psm_clkinit, so the return value is ignored. 13740Sstevel@tonic-gate */ 13750Sstevel@tonic-gate (void) (*pops->psm_clkinit)(hz); 13760Sstevel@tonic-gate *set_mode = TIMER_PERIODIC; 13770Sstevel@tonic-gate return (nsec_per_tick); 13780Sstevel@tonic-gate } 13790Sstevel@tonic-gate } 13800Sstevel@tonic-gate 13814652Scwb 1382999Slq150181 /*ARGSUSED*/ 13830Sstevel@tonic-gate static int 13843446Smrj mach_softlvl_to_vect(int ipl) 13850Sstevel@tonic-gate { 13864652Scwb setsoftint = av_set_softint_pending; 13874652Scwb kdisetsoftint = kdi_av_set_softint_pending; 13880Sstevel@tonic-gate 13890Sstevel@tonic-gate return (PSM_SV_SOFTWARE); 13900Sstevel@tonic-gate } 13910Sstevel@tonic-gate 13923446Smrj #ifdef DEBUG 13933446Smrj /* 13943446Smrj * This is here to allow us to simulate cpus that refuse to start. 13953446Smrj */ 13963446Smrj cpuset_t cpufailset; 13973446Smrj #endif 13983446Smrj 13993446Smrj int 14003446Smrj mach_cpu_start(struct cpu *cp, void *ctx) 14010Sstevel@tonic-gate { 14023446Smrj struct psm_ops *pops = mach_set[0]; 14033446Smrj processorid_t id = cp->cpu_id; 14040Sstevel@tonic-gate 14053446Smrj #ifdef DEBUG 14063446Smrj if (CPU_IN_SET(cpufailset, id)) 14073446Smrj return (0); 14083446Smrj #endif 14093446Smrj return ((*pops->psm_cpu_start)(id, ctx)); 14100Sstevel@tonic-gate } 14110Sstevel@tonic-gate 14125295Srandyf int 14135295Srandyf mach_cpuid_start(processorid_t id, void *ctx) 14145295Srandyf { 14155295Srandyf struct psm_ops *pops = mach_set[0]; 14165295Srandyf 14175295Srandyf #ifdef DEBUG 14185295Srandyf if (CPU_IN_SET(cpufailset, id)) 14195295Srandyf return (0); 14205295Srandyf #endif 14215295Srandyf return ((*pops->psm_cpu_start)(id, ctx)); 14225295Srandyf } 14235295Srandyf 14240Sstevel@tonic-gate /*ARGSUSED*/ 14250Sstevel@tonic-gate static int 14260Sstevel@tonic-gate mach_translate_irq(dev_info_t *dip, int irqno) 14270Sstevel@tonic-gate { 14280Sstevel@tonic-gate return (irqno); /* default to NO translation */ 14290Sstevel@tonic-gate } 14300Sstevel@tonic-gate 14310Sstevel@tonic-gate static void 14320Sstevel@tonic-gate mach_notify_error(int level, char *errmsg) 14330Sstevel@tonic-gate { 14340Sstevel@tonic-gate /* 14350Sstevel@tonic-gate * SL_FATAL is pass in once panicstr is set, deliver it 14360Sstevel@tonic-gate * as CE_PANIC. Also, translate SL_ codes back to CE_ 14370Sstevel@tonic-gate * codes for the psmi handler 14380Sstevel@tonic-gate */ 14390Sstevel@tonic-gate if (level & SL_FATAL) 14400Sstevel@tonic-gate (*notify_error)(CE_PANIC, errmsg); 14410Sstevel@tonic-gate else if (level & SL_WARN) 14420Sstevel@tonic-gate (*notify_error)(CE_WARN, errmsg); 14430Sstevel@tonic-gate else if (level & SL_NOTE) 14440Sstevel@tonic-gate (*notify_error)(CE_NOTE, errmsg); 14450Sstevel@tonic-gate else if (level & SL_CONSOLE) 14460Sstevel@tonic-gate (*notify_error)(CE_CONT, errmsg); 14470Sstevel@tonic-gate } 14480Sstevel@tonic-gate 14490Sstevel@tonic-gate /* 14500Sstevel@tonic-gate * It provides the default basic intr_ops interface for the new DDI 14510Sstevel@tonic-gate * interrupt framework if the PSM doesn't have one. 14520Sstevel@tonic-gate * 14530Sstevel@tonic-gate * Input: 14540Sstevel@tonic-gate * dip - pointer to the dev_info structure of the requested device 14550Sstevel@tonic-gate * hdlp - pointer to the internal interrupt handle structure for the 14560Sstevel@tonic-gate * requested interrupt 14570Sstevel@tonic-gate * intr_op - opcode for this call 14580Sstevel@tonic-gate * result - pointer to the integer that will hold the result to be 14590Sstevel@tonic-gate * passed back if return value is PSM_SUCCESS 14600Sstevel@tonic-gate * 14610Sstevel@tonic-gate * Output: 14620Sstevel@tonic-gate * return value is either PSM_SUCCESS or PSM_FAILURE 14630Sstevel@tonic-gate */ 14640Sstevel@tonic-gate static int 14650Sstevel@tonic-gate mach_intr_ops(dev_info_t *dip, ddi_intr_handle_impl_t *hdlp, 14660Sstevel@tonic-gate psm_intr_op_t intr_op, int *result) 14670Sstevel@tonic-gate { 14680Sstevel@tonic-gate struct intrspec *ispec; 14690Sstevel@tonic-gate 14700Sstevel@tonic-gate switch (intr_op) { 14710Sstevel@tonic-gate case PSM_INTR_OP_CHECK_MSI: 14720Sstevel@tonic-gate *result = hdlp->ih_type & ~(DDI_INTR_TYPE_MSI | 14734481Sbholler DDI_INTR_TYPE_MSIX); 14740Sstevel@tonic-gate break; 14750Sstevel@tonic-gate case PSM_INTR_OP_ALLOC_VECTORS: 14760Sstevel@tonic-gate if (hdlp->ih_type == DDI_INTR_TYPE_FIXED) 14770Sstevel@tonic-gate *result = 1; 14780Sstevel@tonic-gate else 14790Sstevel@tonic-gate *result = 0; 14800Sstevel@tonic-gate break; 14810Sstevel@tonic-gate case PSM_INTR_OP_FREE_VECTORS: 14820Sstevel@tonic-gate break; 14830Sstevel@tonic-gate case PSM_INTR_OP_NAVAIL_VECTORS: 14840Sstevel@tonic-gate if (hdlp->ih_type == DDI_INTR_TYPE_FIXED) 14850Sstevel@tonic-gate *result = 1; 14860Sstevel@tonic-gate else 14870Sstevel@tonic-gate *result = 0; 14880Sstevel@tonic-gate break; 14890Sstevel@tonic-gate case PSM_INTR_OP_XLATE_VECTOR: 1490916Sschwartz ispec = ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp; 14910Sstevel@tonic-gate *result = psm_translate_irq(dip, ispec->intrspec_vec); 14920Sstevel@tonic-gate break; 14930Sstevel@tonic-gate case PSM_INTR_OP_GET_CAP: 14940Sstevel@tonic-gate *result = 0; 14950Sstevel@tonic-gate break; 14960Sstevel@tonic-gate case PSM_INTR_OP_GET_PENDING: 14970Sstevel@tonic-gate case PSM_INTR_OP_CLEAR_MASK: 14980Sstevel@tonic-gate case PSM_INTR_OP_SET_MASK: 14990Sstevel@tonic-gate case PSM_INTR_OP_GET_SHARED: 15000Sstevel@tonic-gate case PSM_INTR_OP_SET_PRI: 15010Sstevel@tonic-gate case PSM_INTR_OP_SET_CAP: 1502916Sschwartz case PSM_INTR_OP_SET_CPU: 1503916Sschwartz case PSM_INTR_OP_GET_INTR: 15040Sstevel@tonic-gate default: 15050Sstevel@tonic-gate return (PSM_FAILURE); 15060Sstevel@tonic-gate } 15070Sstevel@tonic-gate return (PSM_SUCCESS); 15080Sstevel@tonic-gate } 15094769Sdp78419 /* 15104769Sdp78419 * Return 1 if CMT load balancing policies should be 15114769Sdp78419 * implemented across instances of the specified hardware 15124769Sdp78419 * sharing relationship. 15134769Sdp78419 */ 15144769Sdp78419 int 15154769Sdp78419 pg_cmt_load_bal_hw(pghw_type_t hw) 15164769Sdp78419 { 15174769Sdp78419 if (hw == PGHW_IPIPE || 15184769Sdp78419 hw == PGHW_FPU || 15194769Sdp78419 hw == PGHW_CHIP) 15204769Sdp78419 return (1); 15214769Sdp78419 else 15224769Sdp78419 return (0); 15234769Sdp78419 } 15244769Sdp78419 /* 15254769Sdp78419 * Return 1 if thread affinity polices should be implemented 15264769Sdp78419 * for instances of the specifed hardware sharing relationship. 15274769Sdp78419 */ 15284769Sdp78419 int 15294769Sdp78419 pg_cmt_affinity_hw(pghw_type_t hw) 15304769Sdp78419 { 15314769Sdp78419 if (hw == PGHW_CACHE) 15324769Sdp78419 return (1); 15334769Sdp78419 else 15344769Sdp78419 return (0); 15354769Sdp78419 } 1536