111389SAlexander.Kolbasov@Sun.COM
20Sstevel@tonic-gate /*
30Sstevel@tonic-gate * CDDL HEADER START
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * The contents of this file are subject to the terms of the
62006Sandrei * Common Development and Distribution License (the "License").
72006Sandrei * You may not use this file except in compliance with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
2312825SJimmy.Vetayases@oracle.com * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
259652SMichael.Corcoran@Sun.COM /*
2612004Sjiang.liu@intel.com * Copyright (c) 2009-2010, Intel Corporation.
279652SMichael.Corcoran@Sun.COM * All rights reserved.
289652SMichael.Corcoran@Sun.COM */
290Sstevel@tonic-gate
3012004Sjiang.liu@intel.com #define PSMI_1_7
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>
6411389SAlexander.Kolbasov@Sun.COM #include <sys/cpc_pcbe.h>
650Sstevel@tonic-gate
660Sstevel@tonic-gate #define OFFSETOF(s, m) (size_t)(&(((s *)0)->m))
670Sstevel@tonic-gate
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate * Local function prototypes
700Sstevel@tonic-gate */
710Sstevel@tonic-gate static int mp_disable_intr(processorid_t cpun);
720Sstevel@tonic-gate static void mp_enable_intr(processorid_t cpun);
730Sstevel@tonic-gate static void mach_init();
740Sstevel@tonic-gate static void mach_picinit();
750Sstevel@tonic-gate static int machhztomhz(uint64_t cpu_freq_hz);
760Sstevel@tonic-gate static uint64_t mach_getcpufreq(void);
770Sstevel@tonic-gate static void mach_fixcpufreq(void);
780Sstevel@tonic-gate static int mach_clkinit(int, int *);
790Sstevel@tonic-gate static void mach_smpinit(void);
800Sstevel@tonic-gate static int mach_softlvl_to_vect(int ipl);
810Sstevel@tonic-gate static void mach_get_platform(int owner);
820Sstevel@tonic-gate static void mach_construct_info();
830Sstevel@tonic-gate static int mach_translate_irq(dev_info_t *dip, int irqno);
840Sstevel@tonic-gate static int mach_intr_ops(dev_info_t *, ddi_intr_handle_impl_t *,
850Sstevel@tonic-gate psm_intr_op_t, int *);
860Sstevel@tonic-gate static void mach_notify_error(int level, char *errmsg);
870Sstevel@tonic-gate static hrtime_t dummy_hrtime(void);
880Sstevel@tonic-gate static void dummy_scalehrtime(hrtime_t *);
8910797SEric.Saxe@Sun.COM static uint64_t dummy_unscalehrtime(hrtime_t);
908906SEric.Saxe@Sun.COM void cpu_idle(void);
910Sstevel@tonic-gate static void cpu_wakeup(cpu_t *, int);
925084Sjohnlev #ifndef __xpv
938906SEric.Saxe@Sun.COM void cpu_idle_mwait(void);
944481Sbholler static void cpu_wakeup_mwait(cpu_t *, int);
955084Sjohnlev #endif
969652SMichael.Corcoran@Sun.COM static int mach_cpu_create_devinfo(cpu_t *cp, dev_info_t **dipp);
979652SMichael.Corcoran@Sun.COM
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate * External reference functions
1000Sstevel@tonic-gate */
1010Sstevel@tonic-gate extern void return_instr();
1020Sstevel@tonic-gate extern uint64_t freq_tsc(uint32_t *);
1030Sstevel@tonic-gate #if defined(__i386)
1040Sstevel@tonic-gate extern uint64_t freq_notsc(uint32_t *);
1050Sstevel@tonic-gate #endif
1060Sstevel@tonic-gate extern void pc_gethrestime(timestruc_t *);
1073434Sesaxe extern int cpuid_get_coreid(cpu_t *);
1083434Sesaxe extern int cpuid_get_chipid(cpu_t *);
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate /*
1110Sstevel@tonic-gate * PSM functions initialization
1120Sstevel@tonic-gate */
1133446Smrj void (*psm_shutdownf)(int, int) = (void (*)(int, int))return_instr;
1143446Smrj void (*psm_preshutdownf)(int, int) = (void (*)(int, int))return_instr;
1153446Smrj void (*psm_notifyf)(int) = (void (*)(int))return_instr;
1163446Smrj void (*psm_set_idle_cpuf)(int) = (void (*)(int))return_instr;
1173446Smrj void (*psm_unset_idle_cpuf)(int) = (void (*)(int))return_instr;
1180Sstevel@tonic-gate void (*psminitf)() = mach_init;
1190Sstevel@tonic-gate void (*picinitf)() = return_instr;
1200Sstevel@tonic-gate int (*clkinitf)(int, int *) = (int (*)(int, int *))return_instr;
1210Sstevel@tonic-gate int (*ap_mlsetup)() = (int (*)(void))return_instr;
1220Sstevel@tonic-gate void (*send_dirintf)() = return_instr;
1233446Smrj void (*setspl)(int) = (void (*)(int))return_instr;
1240Sstevel@tonic-gate int (*addspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr;
1250Sstevel@tonic-gate int (*delspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr;
12612683SJimmy.Vetayases@oracle.com int (*get_pending_spl)(void) = (int (*)(void))return_instr;
12712683SJimmy.Vetayases@oracle.com int (*addintr)(void *, int, avfunc, char *, int, caddr_t, caddr_t,
12812683SJimmy.Vetayases@oracle.com uint64_t *, dev_info_t *) = NULL;
12912683SJimmy.Vetayases@oracle.com void (*remintr)(void *, int, avfunc, int) = NULL;
1304652Scwb void (*kdisetsoftint)(int, struct av_softinfo *)=
1314652Scwb (void (*)(int, struct av_softinfo *))return_instr;
132999Slq150181 void (*setsoftint)(int, struct av_softinfo *)=
133999Slq150181 (void (*)(int, struct av_softinfo *))return_instr;
1340Sstevel@tonic-gate int (*slvltovect)(int) = (int (*)(int))return_instr;
1350Sstevel@tonic-gate int (*setlvl)(int, int *) = (int (*)(int, int *))return_instr;
1360Sstevel@tonic-gate void (*setlvlx)(int, int) = (void (*)(int, int))return_instr;
1370Sstevel@tonic-gate int (*psm_disable_intr)(int) = mp_disable_intr;
1380Sstevel@tonic-gate void (*psm_enable_intr)(int) = mp_enable_intr;
1390Sstevel@tonic-gate hrtime_t (*gethrtimef)(void) = dummy_hrtime;
1400Sstevel@tonic-gate hrtime_t (*gethrtimeunscaledf)(void) = dummy_hrtime;
1410Sstevel@tonic-gate void (*scalehrtimef)(hrtime_t *) = dummy_scalehrtime;
14210797SEric.Saxe@Sun.COM uint64_t (*unscalehrtimef)(hrtime_t) = dummy_unscalehrtime;
1430Sstevel@tonic-gate int (*psm_translate_irq)(dev_info_t *, int) = mach_translate_irq;
1440Sstevel@tonic-gate void (*gethrestimef)(timestruc_t *) = pc_gethrestime;
1450Sstevel@tonic-gate void (*psm_notify_error)(int, char *) = (void (*)(int, char *))NULL;
1460Sstevel@tonic-gate int (*psm_get_clockirq)(int) = NULL;
1470Sstevel@tonic-gate int (*psm_get_ipivect)(int, int) = NULL;
14812683SJimmy.Vetayases@oracle.com uchar_t (*psm_get_ioapicid)(uchar_t) = NULL;
14912683SJimmy.Vetayases@oracle.com uint32_t (*psm_get_localapicid)(uint32_t) = NULL;
15012683SJimmy.Vetayases@oracle.com uchar_t (*psm_xlate_vector_by_irq)(uchar_t) = NULL;
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate int (*psm_clkinit)(int) = NULL;
1530Sstevel@tonic-gate void (*psm_timer_reprogram)(hrtime_t) = NULL;
1540Sstevel@tonic-gate void (*psm_timer_enable)(void) = NULL;
1550Sstevel@tonic-gate void (*psm_timer_disable)(void) = NULL;
1560Sstevel@tonic-gate void (*psm_post_cyclic_setup)(void *arg) = NULL;
1570Sstevel@tonic-gate int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *, psm_intr_op_t,
1580Sstevel@tonic-gate int *) = mach_intr_ops;
1595295Srandyf int (*psm_state)(psm_state_request_t *) = (int (*)(psm_state_request_t *))
1605295Srandyf return_instr;
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate void (*notify_error)(int, char *) = (void (*)(int, char *))return_instr;
1630Sstevel@tonic-gate void (*hrtime_tick)(void) = return_instr;
1640Sstevel@tonic-gate
1659652SMichael.Corcoran@Sun.COM int (*psm_cpu_create_devinfo)(cpu_t *, dev_info_t **) = mach_cpu_create_devinfo;
16612004Sjiang.liu@intel.com int (*psm_cpu_get_devinfo)(cpu_t *, dev_info_t **) = NULL;
1679652SMichael.Corcoran@Sun.COM
16812683SJimmy.Vetayases@oracle.com /* global IRM pool for APIX (PSM) module */
16912683SJimmy.Vetayases@oracle.com ddi_irm_pool_t *apix_irm_pool_p = NULL;
17012683SJimmy.Vetayases@oracle.com
1715084Sjohnlev /*
1725084Sjohnlev * True if the generic TSC code is our source of hrtime, rather than whatever
1735084Sjohnlev * the PSM can provide.
1745084Sjohnlev */
1755084Sjohnlev #ifdef __xpv
1765084Sjohnlev int tsc_gethrtime_enable = 0;
1775084Sjohnlev #else
1780Sstevel@tonic-gate int tsc_gethrtime_enable = 1;
1795084Sjohnlev #endif
1800Sstevel@tonic-gate int tsc_gethrtime_initted = 0;
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate /*
1835084Sjohnlev * True if the hrtime implementation is "hires"; namely, better than microdata.
1845084Sjohnlev */
1855084Sjohnlev int gethrtime_hires = 0;
1865084Sjohnlev
1875084Sjohnlev /*
1880Sstevel@tonic-gate * Local Static Data
1890Sstevel@tonic-gate */
1900Sstevel@tonic-gate static struct psm_ops mach_ops;
1910Sstevel@tonic-gate static struct psm_ops *mach_set[4] = {&mach_ops, NULL, NULL, NULL};
1920Sstevel@tonic-gate static ushort_t mach_ver[4] = {0, 0, 0, 0};
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate /*
1958675SVikram.Hegde@Sun.COM * virtualization support for psm
1968675SVikram.Hegde@Sun.COM */
1978675SVikram.Hegde@Sun.COM void *psm_vt_ops = NULL;
1988675SVikram.Hegde@Sun.COM /*
1993446Smrj * If non-zero, idle cpus will become "halted" when there's
2000Sstevel@tonic-gate * no work to do.
2010Sstevel@tonic-gate */
2023446Smrj int idle_cpu_use_hlt = 1;
2030Sstevel@tonic-gate
2045084Sjohnlev #ifndef __xpv
2054481Sbholler /*
2064481Sbholler * If non-zero, idle cpus will use mwait if available to halt instead of hlt.
2074481Sbholler */
2084481Sbholler int idle_cpu_prefer_mwait = 1;
2097716SBill.Holler@Sun.COM /*
2107716SBill.Holler@Sun.COM * Set to 0 to avoid MONITOR+CLFLUSH assertion.
2117716SBill.Holler@Sun.COM */
2127716SBill.Holler@Sun.COM int idle_cpu_assert_cflush_monitor = 1;
2137716SBill.Holler@Sun.COM
2148906SEric.Saxe@Sun.COM /*
2158906SEric.Saxe@Sun.COM * If non-zero, idle cpus will not use power saving Deep C-States idle loop.
2168906SEric.Saxe@Sun.COM */
2178906SEric.Saxe@Sun.COM int idle_cpu_no_deep_c = 0;
2188906SEric.Saxe@Sun.COM /*
2198906SEric.Saxe@Sun.COM * Non-power saving idle loop and wakeup pointers.
2208906SEric.Saxe@Sun.COM * Allows user to toggle Deep Idle power saving feature on/off.
2218906SEric.Saxe@Sun.COM */
2228906SEric.Saxe@Sun.COM void (*non_deep_idle_cpu)() = cpu_idle;
2238906SEric.Saxe@Sun.COM void (*non_deep_idle_disp_enq_thread)(cpu_t *, int);
2248906SEric.Saxe@Sun.COM
2258906SEric.Saxe@Sun.COM /*
2268906SEric.Saxe@Sun.COM * Object for the kernel to access the HPET.
2278906SEric.Saxe@Sun.COM */
2288906SEric.Saxe@Sun.COM hpet_t hpet;
2298906SEric.Saxe@Sun.COM
2308906SEric.Saxe@Sun.COM #endif /* ifndef __xpv */
2313434Sesaxe
23212149Srafael.vanoni@sun.com uint_t cp_haltset_fanout = 0;
23312149Srafael.vanoni@sun.com
2343434Sesaxe /*ARGSUSED*/
2353434Sesaxe int
pg_plat_hw_shared(cpu_t * cp,pghw_type_t hw)2363434Sesaxe pg_plat_hw_shared(cpu_t *cp, pghw_type_t hw)
2373434Sesaxe {
2383434Sesaxe switch (hw) {
2393434Sesaxe case PGHW_IPIPE:
240*12826Skuriakose.kuruvilla@oracle.com if (is_x86_feature(x86_featureset, X86FSET_HTT)) {
2413434Sesaxe /*
2423434Sesaxe * Hyper-threading is SMT
2433434Sesaxe */
2443434Sesaxe return (1);
2453434Sesaxe } else {
2463434Sesaxe return (0);
2473434Sesaxe }
24810947SSrihari.Venkatesan@Sun.COM case PGHW_PROCNODE:
24910947SSrihari.Venkatesan@Sun.COM if (cpuid_get_procnodes_per_pkg(cp) > 1)
25010947SSrihari.Venkatesan@Sun.COM return (1);
25110947SSrihari.Venkatesan@Sun.COM else
25210947SSrihari.Venkatesan@Sun.COM return (0);
2533434Sesaxe case PGHW_CHIP:
254*12826Skuriakose.kuruvilla@oracle.com if (is_x86_feature(x86_featureset, X86FSET_CMP) ||
255*12826Skuriakose.kuruvilla@oracle.com is_x86_feature(x86_featureset, X86FSET_HTT))
2563434Sesaxe return (1);
2573434Sesaxe else
2583434Sesaxe return (0);
2594606Sesaxe case PGHW_CACHE:
2604606Sesaxe if (cpuid_get_ncpu_sharing_last_cache(cp) > 1)
2614606Sesaxe return (1);
2624606Sesaxe else
2634606Sesaxe return (0);
2648906SEric.Saxe@Sun.COM case PGHW_POW_ACTIVE:
2658906SEric.Saxe@Sun.COM if (cpupm_domain_id(cp, CPUPM_DTYPE_ACTIVE) != (id_t)-1)
2668906SEric.Saxe@Sun.COM return (1);
2678906SEric.Saxe@Sun.COM else
2688906SEric.Saxe@Sun.COM return (0);
2698906SEric.Saxe@Sun.COM case PGHW_POW_IDLE:
2708906SEric.Saxe@Sun.COM if (cpupm_domain_id(cp, CPUPM_DTYPE_IDLE) != (id_t)-1)
2718906SEric.Saxe@Sun.COM return (1);
2728906SEric.Saxe@Sun.COM else
2738906SEric.Saxe@Sun.COM return (0);
2743434Sesaxe default:
2753434Sesaxe return (0);
2763434Sesaxe }
2773434Sesaxe }
2783434Sesaxe
2793434Sesaxe /*
2803434Sesaxe * Compare two CPUs and see if they have a pghw_type_t sharing relationship
2813434Sesaxe * If pghw_type_t is an unsupported hardware type, then return -1
2823434Sesaxe */
2833434Sesaxe int
pg_plat_cpus_share(cpu_t * cpu_a,cpu_t * cpu_b,pghw_type_t hw)2843434Sesaxe pg_plat_cpus_share(cpu_t *cpu_a, cpu_t *cpu_b, pghw_type_t hw)
2850Sstevel@tonic-gate {
2863434Sesaxe id_t pgp_a, pgp_b;
2873434Sesaxe
2883434Sesaxe pgp_a = pg_plat_hw_instance_id(cpu_a, hw);
2893434Sesaxe pgp_b = pg_plat_hw_instance_id(cpu_b, hw);
2903434Sesaxe
2913434Sesaxe if (pgp_a == -1 || pgp_b == -1)
2923434Sesaxe return (-1);
2933434Sesaxe
2943434Sesaxe return (pgp_a == pgp_b);
2953434Sesaxe }
2963434Sesaxe
2973434Sesaxe /*
2983434Sesaxe * Return a physical instance identifier for known hardware sharing
2993434Sesaxe * relationships
3003434Sesaxe */
3013434Sesaxe id_t
pg_plat_hw_instance_id(cpu_t * cpu,pghw_type_t hw)3023434Sesaxe pg_plat_hw_instance_id(cpu_t *cpu, pghw_type_t hw)
3033434Sesaxe {
3043434Sesaxe switch (hw) {
3053434Sesaxe case PGHW_IPIPE:
3063434Sesaxe return (cpuid_get_coreid(cpu));
3074606Sesaxe case PGHW_CACHE:
3084606Sesaxe return (cpuid_get_last_lvl_cacheid(cpu));
30910947SSrihari.Venkatesan@Sun.COM case PGHW_PROCNODE:
31010947SSrihari.Venkatesan@Sun.COM return (cpuid_get_procnodeid(cpu));
3113434Sesaxe case PGHW_CHIP:
3123434Sesaxe return (cpuid_get_chipid(cpu));
3138906SEric.Saxe@Sun.COM case PGHW_POW_ACTIVE:
3148906SEric.Saxe@Sun.COM return (cpupm_domain_id(cpu, CPUPM_DTYPE_ACTIVE));
3158906SEric.Saxe@Sun.COM case PGHW_POW_IDLE:
3168906SEric.Saxe@Sun.COM return (cpupm_domain_id(cpu, CPUPM_DTYPE_IDLE));
3173434Sesaxe default:
3183434Sesaxe return (-1);
3191228Sandrei }
3203434Sesaxe }
3210Sstevel@tonic-gate
3228906SEric.Saxe@Sun.COM /*
3238906SEric.Saxe@Sun.COM * Express preference for optimizing for sharing relationship
3248906SEric.Saxe@Sun.COM * hw1 vs hw2
3258906SEric.Saxe@Sun.COM */
3268906SEric.Saxe@Sun.COM pghw_type_t
pg_plat_hw_rank(pghw_type_t hw1,pghw_type_t hw2)3278906SEric.Saxe@Sun.COM pg_plat_hw_rank(pghw_type_t hw1, pghw_type_t hw2)
3283434Sesaxe {
3298906SEric.Saxe@Sun.COM int i, rank1, rank2;
3308906SEric.Saxe@Sun.COM
3313434Sesaxe static pghw_type_t hw_hier[] = {
3323434Sesaxe PGHW_IPIPE,
3334606Sesaxe PGHW_CACHE,
33410947SSrihari.Venkatesan@Sun.COM PGHW_PROCNODE,
3353434Sesaxe PGHW_CHIP,
3368906SEric.Saxe@Sun.COM PGHW_POW_IDLE,
3378906SEric.Saxe@Sun.COM PGHW_POW_ACTIVE,
3383434Sesaxe PGHW_NUM_COMPONENTS
3393434Sesaxe };
3403434Sesaxe
3413434Sesaxe for (i = 0; hw_hier[i] != PGHW_NUM_COMPONENTS; i++) {
3428906SEric.Saxe@Sun.COM if (hw_hier[i] == hw1)
3438906SEric.Saxe@Sun.COM rank1 = i;
3448906SEric.Saxe@Sun.COM if (hw_hier[i] == hw2)
3458906SEric.Saxe@Sun.COM rank2 = i;
3463434Sesaxe }
3478906SEric.Saxe@Sun.COM
3488906SEric.Saxe@Sun.COM if (rank1 > rank2)
3498906SEric.Saxe@Sun.COM return (hw1);
3508906SEric.Saxe@Sun.COM else
3518906SEric.Saxe@Sun.COM return (hw2);
3523434Sesaxe }
3533434Sesaxe
3544606Sesaxe /*
3558906SEric.Saxe@Sun.COM * Override the default CMT dispatcher policy for the specified
3568906SEric.Saxe@Sun.COM * hardware sharing relationship
3574606Sesaxe */
3588906SEric.Saxe@Sun.COM pg_cmt_policy_t
pg_plat_cmt_policy(pghw_type_t hw)3598906SEric.Saxe@Sun.COM pg_plat_cmt_policy(pghw_type_t hw)
3604606Sesaxe {
3618906SEric.Saxe@Sun.COM /*
3628906SEric.Saxe@Sun.COM * For shared caches, also load balance across them to
3638906SEric.Saxe@Sun.COM * maximize aggregate cache capacity
3648906SEric.Saxe@Sun.COM */
3658906SEric.Saxe@Sun.COM switch (hw) {
3668906SEric.Saxe@Sun.COM case PGHW_CACHE:
3678906SEric.Saxe@Sun.COM return (CMT_BALANCE|CMT_AFFINITY);
3688906SEric.Saxe@Sun.COM default:
3698906SEric.Saxe@Sun.COM return (CMT_NO_POLICY);
3708906SEric.Saxe@Sun.COM }
3714606Sesaxe }
3724606Sesaxe
3733434Sesaxe id_t
pg_plat_get_core_id(cpu_t * cpu)3743434Sesaxe pg_plat_get_core_id(cpu_t *cpu)
3753434Sesaxe {
3763434Sesaxe return ((id_t)cpuid_get_coreid(cpu));
3773434Sesaxe }
3783434Sesaxe
3793434Sesaxe void
cmp_set_nosteal_interval(void)3803434Sesaxe cmp_set_nosteal_interval(void)
3813434Sesaxe {
3823434Sesaxe /* Set the nosteal interval (used by disp_getbest()) to 100us */
3833434Sesaxe nosteal_nsec = 100000UL;
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate /*
3870Sstevel@tonic-gate * Routine to ensure initial callers to hrtime gets 0 as return
3880Sstevel@tonic-gate */
3890Sstevel@tonic-gate static hrtime_t
dummy_hrtime(void)3900Sstevel@tonic-gate dummy_hrtime(void)
3910Sstevel@tonic-gate {
3920Sstevel@tonic-gate return (0);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate /* ARGSUSED */
3960Sstevel@tonic-gate static void
dummy_scalehrtime(hrtime_t * ticks)3970Sstevel@tonic-gate dummy_scalehrtime(hrtime_t *ticks)
3980Sstevel@tonic-gate {}
3990Sstevel@tonic-gate
40010797SEric.Saxe@Sun.COM static uint64_t
dummy_unscalehrtime(hrtime_t nsecs)40110797SEric.Saxe@Sun.COM dummy_unscalehrtime(hrtime_t nsecs)
40210797SEric.Saxe@Sun.COM {
40310797SEric.Saxe@Sun.COM return ((uint64_t)nsecs);
40410797SEric.Saxe@Sun.COM }
40510797SEric.Saxe@Sun.COM
4060Sstevel@tonic-gate /*
4078906SEric.Saxe@Sun.COM * Supports Deep C-State power saving idle loop.
4088906SEric.Saxe@Sun.COM */
4098906SEric.Saxe@Sun.COM void
cpu_idle_adaptive(void)4108906SEric.Saxe@Sun.COM cpu_idle_adaptive(void)
4118906SEric.Saxe@Sun.COM {
4128906SEric.Saxe@Sun.COM (*CPU->cpu_m.mcpu_idle_cpu)();
4138906SEric.Saxe@Sun.COM }
4148906SEric.Saxe@Sun.COM
4159637SRandy.Fishel@Sun.COM /*
4169637SRandy.Fishel@Sun.COM * Function called by CPU idle notification framework to check whether CPU
4179637SRandy.Fishel@Sun.COM * has been awakened. It will be called with interrupt disabled.
4189637SRandy.Fishel@Sun.COM * If CPU has been awakened, call cpu_idle_exit() to notify CPU idle
4199637SRandy.Fishel@Sun.COM * notification framework.
4209637SRandy.Fishel@Sun.COM */
4219637SRandy.Fishel@Sun.COM /*ARGSUSED*/
4229637SRandy.Fishel@Sun.COM static void
cpu_idle_check_wakeup(void * arg)4239637SRandy.Fishel@Sun.COM cpu_idle_check_wakeup(void *arg)
4248906SEric.Saxe@Sun.COM {
4259637SRandy.Fishel@Sun.COM /*
4269637SRandy.Fishel@Sun.COM * Toggle interrupt flag to detect pending interrupts.
4279637SRandy.Fishel@Sun.COM * If interrupt happened, do_interrupt() will notify CPU idle
4289637SRandy.Fishel@Sun.COM * notification framework so no need to call cpu_idle_exit() here.
4299637SRandy.Fishel@Sun.COM */
4309637SRandy.Fishel@Sun.COM sti();
4319637SRandy.Fishel@Sun.COM SMT_PAUSE();
4329637SRandy.Fishel@Sun.COM cli();
4338906SEric.Saxe@Sun.COM }
4348906SEric.Saxe@Sun.COM
4358906SEric.Saxe@Sun.COM /*
4369637SRandy.Fishel@Sun.COM * Idle the present CPU until wakened via an interrupt
4370Sstevel@tonic-gate */
4388906SEric.Saxe@Sun.COM void
cpu_idle(void)4393446Smrj cpu_idle(void)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate cpu_t *cpup = CPU;
4428408SEric.Saxe@Sun.COM processorid_t cpu_sid = cpup->cpu_seqid;
443711Sesaxe cpupart_t *cp = cpup->cpu_part;
4440Sstevel@tonic-gate int hset_update = 1;
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate /*
4470Sstevel@tonic-gate * If this CPU is online, and there's multiple CPUs
4480Sstevel@tonic-gate * in the system, then we should notate our halting
4490Sstevel@tonic-gate * by adding ourselves to the partition's halted CPU
4500Sstevel@tonic-gate * bitmap. This allows other CPUs to find/awaken us when
4510Sstevel@tonic-gate * work becomes available.
4520Sstevel@tonic-gate */
4530Sstevel@tonic-gate if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1)
4540Sstevel@tonic-gate hset_update = 0;
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate /*
4578408SEric.Saxe@Sun.COM * Add ourselves to the partition's halted CPUs bitmap
4580Sstevel@tonic-gate * and set our HALTED flag, if necessary.
4590Sstevel@tonic-gate *
460711Sesaxe * When a thread becomes runnable, it is placed on the queue
4618408SEric.Saxe@Sun.COM * and then the halted CPU bitmap is checked to determine who
4629637SRandy.Fishel@Sun.COM * (if anyone) should be awakened. We therefore need to first
4638408SEric.Saxe@Sun.COM * add ourselves to the bitmap, and and then check if there
4648408SEric.Saxe@Sun.COM * is any work available. The order is important to prevent a race
4658408SEric.Saxe@Sun.COM * that can lead to work languishing on a run queue somewhere while
4668408SEric.Saxe@Sun.COM * this CPU remains halted.
4678408SEric.Saxe@Sun.COM *
4688408SEric.Saxe@Sun.COM * Either the producing CPU will see we're halted and will awaken us,
4698408SEric.Saxe@Sun.COM * or this CPU will see the work available in disp_anywork().
470711Sesaxe *
4710Sstevel@tonic-gate * Note that memory barriers after updating the HALTED flag
4728408SEric.Saxe@Sun.COM * are not necessary since an atomic operation (updating the bitset)
4730Sstevel@tonic-gate * immediately follows. On x86 the atomic operation acts as a
4740Sstevel@tonic-gate * memory barrier for the update of cpu_disp_flags.
4750Sstevel@tonic-gate */
4760Sstevel@tonic-gate if (hset_update) {
4770Sstevel@tonic-gate cpup->cpu_disp_flags |= CPU_DISP_HALTED;
4788408SEric.Saxe@Sun.COM bitset_atomic_add(&cp->cp_haltset, cpu_sid);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate
4810Sstevel@tonic-gate /*
4820Sstevel@tonic-gate * Check to make sure there's really nothing to do.
483711Sesaxe * Work destined for this CPU may become available after
484711Sesaxe * this check. We'll be notified through the clearing of our
4858408SEric.Saxe@Sun.COM * bit in the halted CPU bitmap, and a poke.
4860Sstevel@tonic-gate */
4870Sstevel@tonic-gate if (disp_anywork()) {
4880Sstevel@tonic-gate if (hset_update) {
4890Sstevel@tonic-gate cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
4908408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid);
4910Sstevel@tonic-gate }
492711Sesaxe return;
493711Sesaxe }
494711Sesaxe
495711Sesaxe /*
496711Sesaxe * We're on our way to being halted.
497711Sesaxe *
498711Sesaxe * Disable interrupts now, so that we'll awaken immediately
499711Sesaxe * after halting if someone tries to poke us between now and
500711Sesaxe * the time we actually halt.
501711Sesaxe *
502711Sesaxe * We check for the presence of our bit after disabling interrupts.
503711Sesaxe * If it's cleared, we'll return. If the bit is cleared after
504711Sesaxe * we check then the poke will pop us out of the halted state.
505711Sesaxe *
506711Sesaxe * This means that the ordering of the poke and the clearing
507711Sesaxe * of the bit by cpu_wakeup is important.
508711Sesaxe * cpu_wakeup() must clear, then poke.
5093446Smrj * cpu_idle() must disable interrupts, then check for the bit.
510711Sesaxe */
511711Sesaxe cli();
512711Sesaxe
5138408SEric.Saxe@Sun.COM if (hset_update && bitset_in_set(&cp->cp_haltset, cpu_sid) == 0) {
514711Sesaxe cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
515711Sesaxe sti();
516711Sesaxe return;
517711Sesaxe }
518711Sesaxe
519711Sesaxe /*
520711Sesaxe * The check for anything locally runnable is here for performance
521711Sesaxe * and isn't needed for correctness. disp_nrunnable ought to be
522711Sesaxe * in our cache still, so it's inexpensive to check, and if there
523711Sesaxe * is anything runnable we won't have to wait for the poke.
524711Sesaxe */
525711Sesaxe if (cpup->cpu_disp->disp_nrunnable != 0) {
526711Sesaxe if (hset_update) {
527711Sesaxe cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
5288408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid);
529711Sesaxe }
5300Sstevel@tonic-gate sti();
5310Sstevel@tonic-gate return;
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate
5349637SRandy.Fishel@Sun.COM if (cpu_idle_enter(IDLE_STATE_C1, 0,
5359637SRandy.Fishel@Sun.COM cpu_idle_check_wakeup, NULL) == 0) {
5369637SRandy.Fishel@Sun.COM mach_cpu_idle();
5379637SRandy.Fishel@Sun.COM cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE);
5389637SRandy.Fishel@Sun.COM }
5397006Srv207048
5400Sstevel@tonic-gate /*
5410Sstevel@tonic-gate * We're no longer halted
5420Sstevel@tonic-gate */
5430Sstevel@tonic-gate if (hset_update) {
5440Sstevel@tonic-gate cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
5458408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid);
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate
5490Sstevel@tonic-gate
5500Sstevel@tonic-gate /*
5510Sstevel@tonic-gate * If "cpu" is halted, then wake it up clearing its halted bit in advance.
5520Sstevel@tonic-gate * Otherwise, see if other CPUs in the cpu partition are halted and need to
5530Sstevel@tonic-gate * be woken up so that they can steal the thread we placed on this CPU.
5540Sstevel@tonic-gate * This function is only used on MP systems.
5550Sstevel@tonic-gate */
5560Sstevel@tonic-gate static void
cpu_wakeup(cpu_t * cpu,int bound)5570Sstevel@tonic-gate cpu_wakeup(cpu_t *cpu, int bound)
5580Sstevel@tonic-gate {
5590Sstevel@tonic-gate uint_t cpu_found;
5608408SEric.Saxe@Sun.COM processorid_t cpu_sid;
5610Sstevel@tonic-gate cpupart_t *cp;
5620Sstevel@tonic-gate
5630Sstevel@tonic-gate cp = cpu->cpu_part;
5648408SEric.Saxe@Sun.COM cpu_sid = cpu->cpu_seqid;
5658408SEric.Saxe@Sun.COM if (bitset_in_set(&cp->cp_haltset, cpu_sid)) {
5660Sstevel@tonic-gate /*
5670Sstevel@tonic-gate * Clear the halted bit for that CPU since it will be
5680Sstevel@tonic-gate * poked in a moment.
5690Sstevel@tonic-gate */
5708408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid);
5710Sstevel@tonic-gate /*
5720Sstevel@tonic-gate * We may find the current CPU present in the halted cpuset
5730Sstevel@tonic-gate * if we're in the context of an interrupt that occurred
5743446Smrj * before we had a chance to clear our bit in cpu_idle().
5750Sstevel@tonic-gate * Poking ourself is obviously unnecessary, since if
5760Sstevel@tonic-gate * we're here, we're not halted.
5770Sstevel@tonic-gate */
5780Sstevel@tonic-gate if (cpu != CPU)
5790Sstevel@tonic-gate poke_cpu(cpu->cpu_id);
5800Sstevel@tonic-gate return;
5810Sstevel@tonic-gate } else {
5820Sstevel@tonic-gate /*
5830Sstevel@tonic-gate * This cpu isn't halted, but it's idle or undergoing a
5840Sstevel@tonic-gate * context switch. No need to awaken anyone else.
5850Sstevel@tonic-gate */
5860Sstevel@tonic-gate if (cpu->cpu_thread == cpu->cpu_idle_thread ||
5870Sstevel@tonic-gate cpu->cpu_disp_flags & CPU_DISP_DONTSTEAL)
5880Sstevel@tonic-gate return;
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate /*
5928408SEric.Saxe@Sun.COM * No need to wake up other CPUs if this is for a bound thread.
5930Sstevel@tonic-gate */
5940Sstevel@tonic-gate if (bound)
5950Sstevel@tonic-gate return;
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate /*
5988408SEric.Saxe@Sun.COM * The CPU specified for wakeup isn't currently halted, so check
5998408SEric.Saxe@Sun.COM * to see if there are any other halted CPUs in the partition,
6008408SEric.Saxe@Sun.COM * and if there are then awaken one.
6010Sstevel@tonic-gate */
6020Sstevel@tonic-gate do {
6038408SEric.Saxe@Sun.COM cpu_found = bitset_find(&cp->cp_haltset);
6048408SEric.Saxe@Sun.COM if (cpu_found == (uint_t)-1)
6050Sstevel@tonic-gate return;
6068408SEric.Saxe@Sun.COM } while (bitset_atomic_test_and_del(&cp->cp_haltset, cpu_found) < 0);
6070Sstevel@tonic-gate
6088408SEric.Saxe@Sun.COM if (cpu_found != CPU->cpu_seqid) {
6098408SEric.Saxe@Sun.COM poke_cpu(cpu_seq[cpu_found]->cpu_id);
6108408SEric.Saxe@Sun.COM }
6110Sstevel@tonic-gate }
6120Sstevel@tonic-gate
6135084Sjohnlev #ifndef __xpv
6144481Sbholler /*
6159637SRandy.Fishel@Sun.COM * Function called by CPU idle notification framework to check whether CPU
6169637SRandy.Fishel@Sun.COM * has been awakened. It will be called with interrupt disabled.
6179637SRandy.Fishel@Sun.COM * If CPU has been awakened, call cpu_idle_exit() to notify CPU idle
6189637SRandy.Fishel@Sun.COM * notification framework.
6199637SRandy.Fishel@Sun.COM */
6209637SRandy.Fishel@Sun.COM static void
cpu_idle_mwait_check_wakeup(void * arg)6219637SRandy.Fishel@Sun.COM cpu_idle_mwait_check_wakeup(void *arg)
6229637SRandy.Fishel@Sun.COM {
6239637SRandy.Fishel@Sun.COM volatile uint32_t *mcpu_mwait = (volatile uint32_t *)arg;
6249637SRandy.Fishel@Sun.COM
6259637SRandy.Fishel@Sun.COM ASSERT(arg != NULL);
6269637SRandy.Fishel@Sun.COM if (*mcpu_mwait != MWAIT_HALTED) {
6279637SRandy.Fishel@Sun.COM /*
6289637SRandy.Fishel@Sun.COM * CPU has been awakened, notify CPU idle notification system.
6299637SRandy.Fishel@Sun.COM */
6309637SRandy.Fishel@Sun.COM cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE);
6319637SRandy.Fishel@Sun.COM } else {
6329637SRandy.Fishel@Sun.COM /*
6339637SRandy.Fishel@Sun.COM * Toggle interrupt flag to detect pending interrupts.
6349637SRandy.Fishel@Sun.COM * If interrupt happened, do_interrupt() will notify CPU idle
6359637SRandy.Fishel@Sun.COM * notification framework so no need to call cpu_idle_exit()
6369637SRandy.Fishel@Sun.COM * here.
6379637SRandy.Fishel@Sun.COM */
6389637SRandy.Fishel@Sun.COM sti();
6399637SRandy.Fishel@Sun.COM SMT_PAUSE();
6409637SRandy.Fishel@Sun.COM cli();
6419637SRandy.Fishel@Sun.COM }
6429637SRandy.Fishel@Sun.COM }
6439637SRandy.Fishel@Sun.COM
6449637SRandy.Fishel@Sun.COM /*
6459637SRandy.Fishel@Sun.COM * Idle the present CPU until awakened via touching its monitored line
6464481Sbholler */
6478906SEric.Saxe@Sun.COM void
cpu_idle_mwait(void)6484481Sbholler cpu_idle_mwait(void)
6494481Sbholler {
6504481Sbholler volatile uint32_t *mcpu_mwait = CPU->cpu_m.mcpu_mwait;
6514481Sbholler cpu_t *cpup = CPU;
6528408SEric.Saxe@Sun.COM processorid_t cpu_sid = cpup->cpu_seqid;
6534481Sbholler cpupart_t *cp = cpup->cpu_part;
6544481Sbholler int hset_update = 1;
6554481Sbholler
6564481Sbholler /*
6578906SEric.Saxe@Sun.COM * Set our mcpu_mwait here, so we can tell if anyone tries to
6584481Sbholler * wake us between now and when we call mwait. No other cpu will
6598408SEric.Saxe@Sun.COM * attempt to set our mcpu_mwait until we add ourself to the halted
6608408SEric.Saxe@Sun.COM * CPU bitmap.
6614481Sbholler */
6624481Sbholler *mcpu_mwait = MWAIT_HALTED;
6634481Sbholler
6644481Sbholler /*
6654481Sbholler * If this CPU is online, and there's multiple CPUs
6668906SEric.Saxe@Sun.COM * in the system, then we should note our halting
6674481Sbholler * by adding ourselves to the partition's halted CPU
6684481Sbholler * bitmap. This allows other CPUs to find/awaken us when
6694481Sbholler * work becomes available.
6704481Sbholler */
6714481Sbholler if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1)
6724481Sbholler hset_update = 0;
6734481Sbholler
6744481Sbholler /*
6758408SEric.Saxe@Sun.COM * Add ourselves to the partition's halted CPUs bitmap
6764481Sbholler * and set our HALTED flag, if necessary.
6774481Sbholler *
6784481Sbholler * When a thread becomes runnable, it is placed on the queue
6798408SEric.Saxe@Sun.COM * and then the halted CPU bitmap is checked to determine who
6808906SEric.Saxe@Sun.COM * (if anyone) should be awakened. We therefore need to first
6818408SEric.Saxe@Sun.COM * add ourselves to the bitmap, and and then check if there
6824481Sbholler * is any work available.
6834481Sbholler *
6844481Sbholler * Note that memory barriers after updating the HALTED flag
6854481Sbholler * are not necessary since an atomic operation (updating the bitmap)
6864481Sbholler * immediately follows. On x86 the atomic operation acts as a
6874481Sbholler * memory barrier for the update of cpu_disp_flags.
6884481Sbholler */
6894481Sbholler if (hset_update) {
6904481Sbholler cpup->cpu_disp_flags |= CPU_DISP_HALTED;
6918408SEric.Saxe@Sun.COM bitset_atomic_add(&cp->cp_haltset, cpu_sid);
6924481Sbholler }
6934481Sbholler
6944481Sbholler /*
6954481Sbholler * Check to make sure there's really nothing to do.
6964481Sbholler * Work destined for this CPU may become available after
6974481Sbholler * this check. We'll be notified through the clearing of our
6988408SEric.Saxe@Sun.COM * bit in the halted CPU bitmap, and a write to our mcpu_mwait.
6994481Sbholler *
7004481Sbholler * disp_anywork() checks disp_nrunnable, so we do not have to later.
7014481Sbholler */
7024481Sbholler if (disp_anywork()) {
7034481Sbholler if (hset_update) {
7044481Sbholler cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
7058408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid);
7064481Sbholler }
7074481Sbholler return;
7084481Sbholler }
7094481Sbholler
7104481Sbholler /*
7114481Sbholler * We're on our way to being halted.
7124481Sbholler * To avoid a lost wakeup, arm the monitor before checking if another
7134481Sbholler * cpu wrote to mcpu_mwait to wake us up.
7144481Sbholler */
7154481Sbholler i86_monitor(mcpu_mwait, 0, 0);
7164481Sbholler if (*mcpu_mwait == MWAIT_HALTED) {
7179637SRandy.Fishel@Sun.COM if (cpu_idle_enter(IDLE_STATE_C1, 0,
7189637SRandy.Fishel@Sun.COM cpu_idle_mwait_check_wakeup, (void *)mcpu_mwait) == 0) {
7199637SRandy.Fishel@Sun.COM if (*mcpu_mwait == MWAIT_HALTED) {
7209637SRandy.Fishel@Sun.COM i86_mwait(0, 0);
7219637SRandy.Fishel@Sun.COM }
7229637SRandy.Fishel@Sun.COM cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE);
7239637SRandy.Fishel@Sun.COM }
7244481Sbholler }
7254481Sbholler
7264481Sbholler /*
7274481Sbholler * We're no longer halted
7284481Sbholler */
7294481Sbholler if (hset_update) {
7304481Sbholler cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
7318408SEric.Saxe@Sun.COM bitset_atomic_del(&cp->cp_haltset, cpu_sid);
7324481Sbholler }
7334481Sbholler }
7344481Sbholler
7354481Sbholler /*
7364481Sbholler * If "cpu" is halted in mwait, then wake it up clearing its halted bit in
7374481Sbholler * advance. Otherwise, see if other CPUs in the cpu partition are halted and
7384481Sbholler * need to be woken up so that they can steal the thread we placed on this CPU.
7394481Sbholler * This function is only used on MP systems.
7404481Sbholler */
7414481Sbholler static void
cpu_wakeup_mwait(cpu_t * cp,int bound)7424481Sbholler cpu_wakeup_mwait(cpu_t *cp, int bound)
7434481Sbholler {
7444481Sbholler cpupart_t *cpu_part;
7454481Sbholler uint_t cpu_found;
7468408SEric.Saxe@Sun.COM processorid_t cpu_sid;
7474481Sbholler
7484481Sbholler cpu_part = cp->cpu_part;
7498408SEric.Saxe@Sun.COM cpu_sid = cp->cpu_seqid;
7504481Sbholler
7514481Sbholler /*
7524481Sbholler * Clear the halted bit for that CPU since it will be woken up
7534481Sbholler * in a moment.
7544481Sbholler */
7558408SEric.Saxe@Sun.COM if (bitset_in_set(&cpu_part->cp_haltset, cpu_sid)) {
7564481Sbholler /*
7574481Sbholler * Clear the halted bit for that CPU since it will be
7584481Sbholler * poked in a moment.
7594481Sbholler */
7608408SEric.Saxe@Sun.COM bitset_atomic_del(&cpu_part->cp_haltset, cpu_sid);
7614481Sbholler /*
7624481Sbholler * We may find the current CPU present in the halted cpuset
7634481Sbholler * if we're in the context of an interrupt that occurred
7644481Sbholler * before we had a chance to clear our bit in cpu_idle().
7654481Sbholler * Waking ourself is obviously unnecessary, since if
7664481Sbholler * we're here, we're not halted.
7674481Sbholler *
7684481Sbholler * monitor/mwait wakeup via writing to our cache line is
7694481Sbholler * harmless and less expensive than always checking if we
7704481Sbholler * are waking ourself which is an uncommon case.
7714481Sbholler */
7724481Sbholler MWAIT_WAKEUP(cp); /* write to monitored line */
7734481Sbholler return;
7744481Sbholler } else {
7754481Sbholler /*
7764481Sbholler * This cpu isn't halted, but it's idle or undergoing a
7774481Sbholler * context switch. No need to awaken anyone else.
7784481Sbholler */
7794481Sbholler if (cp->cpu_thread == cp->cpu_idle_thread ||
7804481Sbholler cp->cpu_disp_flags & CPU_DISP_DONTSTEAL)
7814481Sbholler return;
7824481Sbholler }
7834481Sbholler
7844481Sbholler /*
7854481Sbholler * No need to wake up other CPUs if the thread we just enqueued
7864481Sbholler * is bound.
7874481Sbholler */
7888408SEric.Saxe@Sun.COM if (bound || ncpus == 1)
7894481Sbholler return;
7904481Sbholler
7914481Sbholler /*
7924481Sbholler * See if there's any other halted CPUs. If there are, then
7934481Sbholler * select one, and awaken it.
7944481Sbholler * It's possible that after we find a CPU, somebody else
7954481Sbholler * will awaken it before we get the chance.
7964481Sbholler * In that case, look again.
7974481Sbholler */
7984481Sbholler do {
7998408SEric.Saxe@Sun.COM cpu_found = bitset_find(&cpu_part->cp_haltset);
8008408SEric.Saxe@Sun.COM if (cpu_found == (uint_t)-1)
8014481Sbholler return;
8028408SEric.Saxe@Sun.COM } while (bitset_atomic_test_and_del(&cpu_part->cp_haltset,
8038408SEric.Saxe@Sun.COM cpu_found) < 0);
8044481Sbholler
8054481Sbholler /*
8068408SEric.Saxe@Sun.COM * Do not check if cpu_found is ourself as monitor/mwait
8078408SEric.Saxe@Sun.COM * wakeup is cheap.
8084481Sbholler */
8098408SEric.Saxe@Sun.COM MWAIT_WAKEUP(cpu_seq[cpu_found]); /* write to monitored line */
8104481Sbholler }
8118408SEric.Saxe@Sun.COM
8125084Sjohnlev #endif
8134481Sbholler
8143446Smrj void (*cpu_pause_handler)(volatile char *) = NULL;
8153446Smrj
8160Sstevel@tonic-gate static int
mp_disable_intr(int cpun)8170Sstevel@tonic-gate mp_disable_intr(int cpun)
8180Sstevel@tonic-gate {
8190Sstevel@tonic-gate /*
8200Sstevel@tonic-gate * switch to the offline cpu
8210Sstevel@tonic-gate */
8220Sstevel@tonic-gate affinity_set(cpun);
8230Sstevel@tonic-gate /*
8240Sstevel@tonic-gate * raise ipl to just below cross call
8250Sstevel@tonic-gate */
8269489SJoe.Bonasera@sun.com splx(XC_SYS_PIL - 1);
8270Sstevel@tonic-gate /*
8280Sstevel@tonic-gate * set base spl to prevent the next swtch to idle from
8290Sstevel@tonic-gate * lowering back to ipl 0
8300Sstevel@tonic-gate */
8319489SJoe.Bonasera@sun.com CPU->cpu_intr_actv |= (1 << (XC_SYS_PIL - 1));
8320Sstevel@tonic-gate set_base_spl();
8330Sstevel@tonic-gate affinity_clear();
8340Sstevel@tonic-gate return (DDI_SUCCESS);
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate
8370Sstevel@tonic-gate static void
mp_enable_intr(int cpun)8380Sstevel@tonic-gate mp_enable_intr(int cpun)
8390Sstevel@tonic-gate {
8400Sstevel@tonic-gate /*
8410Sstevel@tonic-gate * switch to the online cpu
8420Sstevel@tonic-gate */
8430Sstevel@tonic-gate affinity_set(cpun);
8440Sstevel@tonic-gate /*
8450Sstevel@tonic-gate * clear the interrupt active mask
8460Sstevel@tonic-gate */
8479489SJoe.Bonasera@sun.com CPU->cpu_intr_actv &= ~(1 << (XC_SYS_PIL - 1));
8480Sstevel@tonic-gate set_base_spl();
8490Sstevel@tonic-gate (void) spl0();
8500Sstevel@tonic-gate affinity_clear();
8510Sstevel@tonic-gate }
8520Sstevel@tonic-gate
8530Sstevel@tonic-gate static void
mach_get_platform(int owner)8540Sstevel@tonic-gate mach_get_platform(int owner)
8550Sstevel@tonic-gate {
8560Sstevel@tonic-gate void **srv_opsp;
8570Sstevel@tonic-gate void **clt_opsp;
8580Sstevel@tonic-gate int i;
8590Sstevel@tonic-gate int total_ops;
8600Sstevel@tonic-gate
8610Sstevel@tonic-gate /* fix up psm ops */
8620Sstevel@tonic-gate srv_opsp = (void **)mach_set[0];
8630Sstevel@tonic-gate clt_opsp = (void **)mach_set[owner];
8640Sstevel@tonic-gate if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01)
8650Sstevel@tonic-gate total_ops = sizeof (struct psm_ops_ver01) /
8664481Sbholler sizeof (void (*)(void));
8670Sstevel@tonic-gate else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_1)
8680Sstevel@tonic-gate /* no psm_notify_func */
8690Sstevel@tonic-gate total_ops = OFFSETOF(struct psm_ops, psm_notify_func) /
8700Sstevel@tonic-gate sizeof (void (*)(void));
8710Sstevel@tonic-gate else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_2)
8720Sstevel@tonic-gate /* no psm_timer funcs */
8730Sstevel@tonic-gate total_ops = OFFSETOF(struct psm_ops, psm_timer_reprogram) /
8740Sstevel@tonic-gate sizeof (void (*)(void));
8750Sstevel@tonic-gate else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_3)
8760Sstevel@tonic-gate /* no psm_preshutdown function */
8770Sstevel@tonic-gate total_ops = OFFSETOF(struct psm_ops, psm_preshutdown) /
8780Sstevel@tonic-gate sizeof (void (*)(void));
8790Sstevel@tonic-gate else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_4)
88012004Sjiang.liu@intel.com /* no psm_intr_ops function */
8810Sstevel@tonic-gate total_ops = OFFSETOF(struct psm_ops, psm_intr_ops) /
8820Sstevel@tonic-gate sizeof (void (*)(void));
88312004Sjiang.liu@intel.com else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_5)
88412004Sjiang.liu@intel.com /* no psm_state function */
88512004Sjiang.liu@intel.com total_ops = OFFSETOF(struct psm_ops, psm_state) /
88612004Sjiang.liu@intel.com sizeof (void (*)(void));
88712004Sjiang.liu@intel.com else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_6)
88812004Sjiang.liu@intel.com /* no psm_cpu_ops function */
88912004Sjiang.liu@intel.com total_ops = OFFSETOF(struct psm_ops, psm_cpu_ops) /
89012004Sjiang.liu@intel.com sizeof (void (*)(void));
8910Sstevel@tonic-gate else
8920Sstevel@tonic-gate total_ops = sizeof (struct psm_ops) / sizeof (void (*)(void));
8930Sstevel@tonic-gate
8940Sstevel@tonic-gate /*
8950Sstevel@tonic-gate * Save the version of the PSM module, in case we need to
8969637SRandy.Fishel@Sun.COM * behave differently based on version.
8970Sstevel@tonic-gate */
8980Sstevel@tonic-gate mach_ver[0] = mach_ver[owner];
8990Sstevel@tonic-gate
9000Sstevel@tonic-gate for (i = 0; i < total_ops; i++)
9010Sstevel@tonic-gate if (clt_opsp[i] != NULL)
9020Sstevel@tonic-gate srv_opsp[i] = clt_opsp[i];
9030Sstevel@tonic-gate }
9040Sstevel@tonic-gate
9050Sstevel@tonic-gate static void
mach_construct_info()9060Sstevel@tonic-gate mach_construct_info()
9070Sstevel@tonic-gate {
9083446Smrj struct psm_sw *swp;
9090Sstevel@tonic-gate int mach_cnt[PSM_OWN_OVERRIDE+1] = {0};
9100Sstevel@tonic-gate int conflict_owner = 0;
9110Sstevel@tonic-gate
9120Sstevel@tonic-gate if (psmsw->psw_forw == psmsw)
9130Sstevel@tonic-gate panic("No valid PSM modules found");
9140Sstevel@tonic-gate mutex_enter(&psmsw_lock);
9150Sstevel@tonic-gate for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
9160Sstevel@tonic-gate if (!(swp->psw_flag & PSM_MOD_IDENTIFY))
9170Sstevel@tonic-gate continue;
9180Sstevel@tonic-gate mach_set[swp->psw_infop->p_owner] = swp->psw_infop->p_ops;
9190Sstevel@tonic-gate mach_ver[swp->psw_infop->p_owner] = swp->psw_infop->p_version;
9200Sstevel@tonic-gate mach_cnt[swp->psw_infop->p_owner]++;
9210Sstevel@tonic-gate }
9220Sstevel@tonic-gate mutex_exit(&psmsw_lock);
9230Sstevel@tonic-gate
9240Sstevel@tonic-gate mach_get_platform(PSM_OWN_SYS_DEFAULT);
9250Sstevel@tonic-gate
9260Sstevel@tonic-gate /* check to see are there any conflicts */
9270Sstevel@tonic-gate if (mach_cnt[PSM_OWN_EXCLUSIVE] > 1)
9280Sstevel@tonic-gate conflict_owner = PSM_OWN_EXCLUSIVE;
9290Sstevel@tonic-gate if (mach_cnt[PSM_OWN_OVERRIDE] > 1)
9300Sstevel@tonic-gate conflict_owner = PSM_OWN_OVERRIDE;
9310Sstevel@tonic-gate if (conflict_owner) {
9320Sstevel@tonic-gate /* remove all psm modules except uppc */
9330Sstevel@tonic-gate cmn_err(CE_WARN,
9344481Sbholler "Conflicts detected on the following PSM modules:");
9350Sstevel@tonic-gate mutex_enter(&psmsw_lock);
9360Sstevel@tonic-gate for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
9370Sstevel@tonic-gate if (swp->psw_infop->p_owner == conflict_owner)
9380Sstevel@tonic-gate cmn_err(CE_WARN, "%s ",
9394481Sbholler swp->psw_infop->p_mach_idstring);
9400Sstevel@tonic-gate }
9410Sstevel@tonic-gate mutex_exit(&psmsw_lock);
9420Sstevel@tonic-gate cmn_err(CE_WARN,
9434481Sbholler "Setting the system back to SINGLE processor mode!");
9440Sstevel@tonic-gate cmn_err(CE_WARN,
9450Sstevel@tonic-gate "Please edit /etc/mach to remove the invalid PSM module.");
9460Sstevel@tonic-gate return;
9470Sstevel@tonic-gate }
9480Sstevel@tonic-gate
9490Sstevel@tonic-gate if (mach_set[PSM_OWN_EXCLUSIVE])
9500Sstevel@tonic-gate mach_get_platform(PSM_OWN_EXCLUSIVE);
9510Sstevel@tonic-gate
9520Sstevel@tonic-gate if (mach_set[PSM_OWN_OVERRIDE])
9530Sstevel@tonic-gate mach_get_platform(PSM_OWN_OVERRIDE);
9540Sstevel@tonic-gate }
9550Sstevel@tonic-gate
9560Sstevel@tonic-gate static void
mach_init()9570Sstevel@tonic-gate mach_init()
9580Sstevel@tonic-gate {
9593446Smrj struct psm_ops *pops;
9600Sstevel@tonic-gate
9610Sstevel@tonic-gate mach_construct_info();
9620Sstevel@tonic-gate
9630Sstevel@tonic-gate pops = mach_set[0];
9640Sstevel@tonic-gate
9650Sstevel@tonic-gate /* register the interrupt and clock initialization rotuines */
9660Sstevel@tonic-gate picinitf = mach_picinit;
9670Sstevel@tonic-gate clkinitf = mach_clkinit;
9680Sstevel@tonic-gate psm_get_clockirq = pops->psm_get_clockirq;
9690Sstevel@tonic-gate
9700Sstevel@tonic-gate /* register the interrupt setup code */
9710Sstevel@tonic-gate slvltovect = mach_softlvl_to_vect;
9720Sstevel@tonic-gate addspl = pops->psm_addspl;
9730Sstevel@tonic-gate delspl = pops->psm_delspl;
9740Sstevel@tonic-gate
9750Sstevel@tonic-gate if (pops->psm_translate_irq)
9760Sstevel@tonic-gate psm_translate_irq = pops->psm_translate_irq;
9770Sstevel@tonic-gate if (pops->psm_intr_ops)
9780Sstevel@tonic-gate psm_intr_ops = pops->psm_intr_ops;
9793446Smrj
9803446Smrj #if defined(PSMI_1_2) || defined(PSMI_1_3) || defined(PSMI_1_4)
9813446Smrj /*
9823446Smrj * Time-of-day functionality now handled in TOD modules.
9833446Smrj * (Warn about PSM modules that think that we're going to use
9843446Smrj * their ops vectors.)
9853446Smrj */
9863446Smrj if (pops->psm_tod_get)
9873446Smrj cmn_err(CE_WARN, "obsolete psm_tod_get op %p",
9883446Smrj (void *)pops->psm_tod_get);
9893446Smrj
9903446Smrj if (pops->psm_tod_set)
9913446Smrj cmn_err(CE_WARN, "obsolete psm_tod_set op %p",
9923446Smrj (void *)pops->psm_tod_set);
9933446Smrj #endif
9943446Smrj
9950Sstevel@tonic-gate if (pops->psm_notify_error) {
9960Sstevel@tonic-gate psm_notify_error = mach_notify_error;
9970Sstevel@tonic-gate notify_error = pops->psm_notify_error;
9980Sstevel@tonic-gate }
9990Sstevel@tonic-gate
10000Sstevel@tonic-gate (*pops->psm_softinit)();
10010Sstevel@tonic-gate
10020Sstevel@tonic-gate /*
10038906SEric.Saxe@Sun.COM * Initialize the dispatcher's function hooks to enable CPU halting
10048906SEric.Saxe@Sun.COM * when idle. Set both the deep-idle and non-deep-idle hooks.
10058906SEric.Saxe@Sun.COM *
10068906SEric.Saxe@Sun.COM * Assume we can use power saving deep-idle loop cpu_idle_adaptive.
10078906SEric.Saxe@Sun.COM * Platform deep-idle driver will reset our idle loop to
10088906SEric.Saxe@Sun.COM * non_deep_idle_cpu if power saving deep-idle feature is not available.
10098906SEric.Saxe@Sun.COM *
10105045Sbholler * Do not use monitor/mwait if idle_cpu_use_hlt is not set(spin idle)
10115045Sbholler * or idle_cpu_prefer_mwait is not set.
10124481Sbholler * Allocate monitor/mwait buffer for cpu0.
10130Sstevel@tonic-gate */
10148906SEric.Saxe@Sun.COM #ifndef __xpv
10158906SEric.Saxe@Sun.COM non_deep_idle_disp_enq_thread = disp_enq_thread;
10168906SEric.Saxe@Sun.COM #endif
10174481Sbholler if (idle_cpu_use_hlt) {
10188906SEric.Saxe@Sun.COM idle_cpu = cpu_idle_adaptive;
10198906SEric.Saxe@Sun.COM CPU->cpu_m.mcpu_idle_cpu = cpu_idle;
10205084Sjohnlev #ifndef __xpv
1021*12826Skuriakose.kuruvilla@oracle.com if (is_x86_feature(x86_featureset, X86FSET_MWAIT) &&
1022*12826Skuriakose.kuruvilla@oracle.com idle_cpu_prefer_mwait) {
10235045Sbholler CPU->cpu_m.mcpu_mwait = cpuid_mwait_alloc(CPU);
10245045Sbholler /*
10255045Sbholler * Protect ourself from insane mwait size.
10265045Sbholler */
10275045Sbholler if (CPU->cpu_m.mcpu_mwait == NULL) {
10285045Sbholler #ifdef DEBUG
10295045Sbholler cmn_err(CE_NOTE, "Using hlt idle. Cannot "
10305045Sbholler "handle cpu 0 mwait size.");
10315045Sbholler #endif
10325045Sbholler idle_cpu_prefer_mwait = 0;
10338906SEric.Saxe@Sun.COM CPU->cpu_m.mcpu_idle_cpu = cpu_idle;
10345045Sbholler } else {
10358906SEric.Saxe@Sun.COM CPU->cpu_m.mcpu_idle_cpu = cpu_idle_mwait;
10365045Sbholler }
10374481Sbholler } else {
10388906SEric.Saxe@Sun.COM CPU->cpu_m.mcpu_idle_cpu = cpu_idle;
10398906SEric.Saxe@Sun.COM }
10408906SEric.Saxe@Sun.COM non_deep_idle_cpu = CPU->cpu_m.mcpu_idle_cpu;
10418906SEric.Saxe@Sun.COM
10428906SEric.Saxe@Sun.COM /*
10438906SEric.Saxe@Sun.COM * Disable power saving deep idle loop?
10448906SEric.Saxe@Sun.COM */
10458906SEric.Saxe@Sun.COM if (idle_cpu_no_deep_c) {
10468906SEric.Saxe@Sun.COM idle_cpu = non_deep_idle_cpu;
10474481Sbholler }
10485084Sjohnlev #endif
10494481Sbholler }
10500Sstevel@tonic-gate
10510Sstevel@tonic-gate mach_smpinit();
10520Sstevel@tonic-gate }
10530Sstevel@tonic-gate
10540Sstevel@tonic-gate static void
mach_smpinit(void)10550Sstevel@tonic-gate mach_smpinit(void)
10560Sstevel@tonic-gate {
10572006Sandrei struct psm_ops *pops;
10582006Sandrei processorid_t cpu_id;
10592006Sandrei int cnt;
10602006Sandrei cpuset_t cpumask;
10610Sstevel@tonic-gate
10620Sstevel@tonic-gate pops = mach_set[0];
10636336Sbholler CPUSET_ZERO(cpumask);
10640Sstevel@tonic-gate
10650Sstevel@tonic-gate cpu_id = -1;
10660Sstevel@tonic-gate cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
106712004Sjiang.liu@intel.com /*
106812004Sjiang.liu@intel.com * Only add boot_ncpus CPUs to mp_cpus. Other CPUs will be handled
106912004Sjiang.liu@intel.com * by CPU DR driver at runtime.
107012004Sjiang.liu@intel.com */
107112004Sjiang.liu@intel.com for (cnt = 0; cpu_id != -1 && cnt < boot_ncpus; cnt++) {
10722006Sandrei CPUSET_ADD(cpumask, cpu_id);
10730Sstevel@tonic-gate cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
10740Sstevel@tonic-gate }
10750Sstevel@tonic-gate
10760Sstevel@tonic-gate mp_cpus = cpumask;
10770Sstevel@tonic-gate
10780Sstevel@tonic-gate /* MP related routines */
10790Sstevel@tonic-gate ap_mlsetup = pops->psm_post_cpu_start;
10800Sstevel@tonic-gate send_dirintf = pops->psm_send_ipi;
10810Sstevel@tonic-gate
10820Sstevel@tonic-gate /* optional MP related routines */
10830Sstevel@tonic-gate if (pops->psm_shutdown)
10840Sstevel@tonic-gate psm_shutdownf = pops->psm_shutdown;
10850Sstevel@tonic-gate if (pops->psm_preshutdown)
10860Sstevel@tonic-gate psm_preshutdownf = pops->psm_preshutdown;
10870Sstevel@tonic-gate if (pops->psm_notify_func)
10880Sstevel@tonic-gate psm_notifyf = pops->psm_notify_func;
10890Sstevel@tonic-gate if (pops->psm_set_idlecpu)
10900Sstevel@tonic-gate psm_set_idle_cpuf = pops->psm_set_idlecpu;
10910Sstevel@tonic-gate if (pops->psm_unset_idlecpu)
10920Sstevel@tonic-gate psm_unset_idle_cpuf = pops->psm_unset_idlecpu;
10930Sstevel@tonic-gate
10940Sstevel@tonic-gate psm_clkinit = pops->psm_clkinit;
10950Sstevel@tonic-gate
10960Sstevel@tonic-gate if (pops->psm_timer_reprogram)
10970Sstevel@tonic-gate psm_timer_reprogram = pops->psm_timer_reprogram;
10980Sstevel@tonic-gate
10990Sstevel@tonic-gate if (pops->psm_timer_enable)
11000Sstevel@tonic-gate psm_timer_enable = pops->psm_timer_enable;
11010Sstevel@tonic-gate
11020Sstevel@tonic-gate if (pops->psm_timer_disable)
11030Sstevel@tonic-gate psm_timer_disable = pops->psm_timer_disable;
11040Sstevel@tonic-gate
11050Sstevel@tonic-gate if (pops->psm_post_cyclic_setup)
11060Sstevel@tonic-gate psm_post_cyclic_setup = pops->psm_post_cyclic_setup;
11070Sstevel@tonic-gate
11085295Srandyf if (pops->psm_state)
11095295Srandyf psm_state = pops->psm_state;
11105295Srandyf
11117113Sbholler /*
11127113Sbholler * Set these vectors here so they can be used by Suspend/Resume
11137113Sbholler * on UP machines.
11147113Sbholler */
11157113Sbholler if (pops->psm_disable_intr)
11167113Sbholler psm_disable_intr = pops->psm_disable_intr;
11177113Sbholler if (pops->psm_enable_intr)
11187113Sbholler psm_enable_intr = pops->psm_enable_intr;
11197113Sbholler
11207113Sbholler /* check for multiple CPUs */
112112004Sjiang.liu@intel.com if (cnt < 2 && plat_dr_support_cpu() == B_FALSE)
11220Sstevel@tonic-gate return;
11230Sstevel@tonic-gate
11240Sstevel@tonic-gate /* check for MP platforms */
11250Sstevel@tonic-gate if (pops->psm_cpu_start == NULL)
11260Sstevel@tonic-gate return;
11270Sstevel@tonic-gate
11280Sstevel@tonic-gate /*
11290Sstevel@tonic-gate * Set the dispatcher hook to enable cpu "wake up"
11300Sstevel@tonic-gate * when a thread becomes runnable.
11310Sstevel@tonic-gate */
11325084Sjohnlev if (idle_cpu_use_hlt) {
11335084Sjohnlev disp_enq_thread = cpu_wakeup;
11345084Sjohnlev #ifndef __xpv
1135*12826Skuriakose.kuruvilla@oracle.com if (is_x86_feature(x86_featureset, X86FSET_MWAIT) &&
1136*12826Skuriakose.kuruvilla@oracle.com idle_cpu_prefer_mwait)
11374481Sbholler disp_enq_thread = cpu_wakeup_mwait;
11388906SEric.Saxe@Sun.COM non_deep_idle_disp_enq_thread = disp_enq_thread;
11395084Sjohnlev #endif
11405084Sjohnlev }
11410Sstevel@tonic-gate
11420Sstevel@tonic-gate psm_get_ipivect = pops->psm_get_ipivect;
11430Sstevel@tonic-gate
11449489SJoe.Bonasera@sun.com (void) add_avintr((void *)NULL, XC_HI_PIL, xc_serv, "xc_intr",
11454481Sbholler (*pops->psm_get_ipivect)(XC_HI_PIL, PSM_INTR_IPI_HI),
11469489SJoe.Bonasera@sun.com NULL, NULL, NULL, NULL);
11470Sstevel@tonic-gate
11480Sstevel@tonic-gate (void) (*pops->psm_get_ipivect)(XC_CPUPOKE_PIL, PSM_INTR_POKE);
11490Sstevel@tonic-gate }
11500Sstevel@tonic-gate
11510Sstevel@tonic-gate static void
mach_picinit()11520Sstevel@tonic-gate mach_picinit()
11530Sstevel@tonic-gate {
11542006Sandrei struct psm_ops *pops;
11550Sstevel@tonic-gate
11560Sstevel@tonic-gate pops = mach_set[0];
11570Sstevel@tonic-gate
11580Sstevel@tonic-gate /* register the interrupt handlers */
11590Sstevel@tonic-gate setlvl = pops->psm_intr_enter;
11600Sstevel@tonic-gate setlvlx = pops->psm_intr_exit;
11610Sstevel@tonic-gate
11620Sstevel@tonic-gate /* initialize the interrupt hardware */
11630Sstevel@tonic-gate (*pops->psm_picinit)();
11640Sstevel@tonic-gate
11650Sstevel@tonic-gate /* set interrupt mask for current ipl */
11660Sstevel@tonic-gate setspl = pops->psm_setspl;
11673446Smrj cli();
11680Sstevel@tonic-gate setspl(CPU->cpu_pri);
11690Sstevel@tonic-gate }
11700Sstevel@tonic-gate
11710Sstevel@tonic-gate uint_t cpu_freq; /* MHz */
11720Sstevel@tonic-gate uint64_t cpu_freq_hz; /* measured (in hertz) */
11730Sstevel@tonic-gate
11740Sstevel@tonic-gate #define MEGA_HZ 1000000
11750Sstevel@tonic-gate
11765084Sjohnlev #ifdef __xpv
11775084Sjohnlev
11785084Sjohnlev int xpv_cpufreq_workaround = 1;
11795084Sjohnlev int xpv_cpufreq_verbose = 0;
11805084Sjohnlev
11815084Sjohnlev #else /* __xpv */
11825084Sjohnlev
11830Sstevel@tonic-gate static uint64_t
mach_calchz(uint32_t pit_counter,uint64_t * processor_clks)11840Sstevel@tonic-gate mach_calchz(uint32_t pit_counter, uint64_t *processor_clks)
11850Sstevel@tonic-gate {
11860Sstevel@tonic-gate uint64_t cpu_hz;
11870Sstevel@tonic-gate
11880Sstevel@tonic-gate if ((pit_counter == 0) || (*processor_clks == 0) ||
11890Sstevel@tonic-gate (*processor_clks > (((uint64_t)-1) / PIT_HZ)))
11900Sstevel@tonic-gate return (0);
11910Sstevel@tonic-gate
11920Sstevel@tonic-gate cpu_hz = ((uint64_t)PIT_HZ * *processor_clks) / pit_counter;
11930Sstevel@tonic-gate
11940Sstevel@tonic-gate return (cpu_hz);
11950Sstevel@tonic-gate }
11960Sstevel@tonic-gate
11975084Sjohnlev #endif /* __xpv */
11985084Sjohnlev
11990Sstevel@tonic-gate static uint64_t
mach_getcpufreq(void)12000Sstevel@tonic-gate mach_getcpufreq(void)
12010Sstevel@tonic-gate {
12025084Sjohnlev #if defined(__xpv)
12035084Sjohnlev vcpu_time_info_t *vti = &CPU->cpu_m.mcpu_vcpu_info->time;
12045084Sjohnlev uint64_t cpu_hz;
12055084Sjohnlev
12065084Sjohnlev /*
12075084Sjohnlev * During dom0 bringup, it was noted that on at least one older
12085084Sjohnlev * Intel HT machine, the hypervisor initially gives a tsc_to_system_mul
12095084Sjohnlev * value that is quite wrong (the 3.06GHz clock was reported
12105084Sjohnlev * as 4.77GHz)
12115084Sjohnlev *
12125084Sjohnlev * The curious thing is, that if you stop the kernel at entry,
12135084Sjohnlev * breakpoint here and inspect the value with kmdb, the value
12145084Sjohnlev * is correct - but if you don't stop and simply enable the
12155084Sjohnlev * printf statement (below), you can see the bad value printed
12165084Sjohnlev * here. Almost as if something kmdb did caused the hypervisor to
12175084Sjohnlev * figure it out correctly. And, note that the hypervisor
12185084Sjohnlev * eventually -does- figure it out correctly ... if you look at
12195084Sjohnlev * the field later in the life of dom0, it is correct.
12205084Sjohnlev *
12215084Sjohnlev * For now, on dom0, we employ a slightly cheesy workaround of
12225084Sjohnlev * using the DOM0_PHYSINFO hypercall.
12235084Sjohnlev */
12245084Sjohnlev if (DOMAIN_IS_INITDOMAIN(xen_info) && xpv_cpufreq_workaround) {
122510175SStuart.Maybee@Sun.COM cpu_hz = 1000 * xpv_cpu_khz();
12265084Sjohnlev } else {
12275084Sjohnlev cpu_hz = (UINT64_C(1000000000) << 32) / vti->tsc_to_system_mul;
12285084Sjohnlev
12295084Sjohnlev if (vti->tsc_shift < 0)
12305084Sjohnlev cpu_hz <<= -vti->tsc_shift;
12315084Sjohnlev else
12325084Sjohnlev cpu_hz >>= vti->tsc_shift;
12335084Sjohnlev }
12345084Sjohnlev
12355084Sjohnlev if (xpv_cpufreq_verbose)
12365084Sjohnlev printf("mach_getcpufreq: system_mul 0x%x, shift %d, "
12375084Sjohnlev "cpu_hz %" PRId64 "Hz\n",
12385084Sjohnlev vti->tsc_to_system_mul, vti->tsc_shift, cpu_hz);
12395084Sjohnlev
12405084Sjohnlev return (cpu_hz);
12415084Sjohnlev #else /* __xpv */
12420Sstevel@tonic-gate uint32_t pit_counter;
12430Sstevel@tonic-gate uint64_t processor_clks;
12440Sstevel@tonic-gate
1245*12826Skuriakose.kuruvilla@oracle.com if (is_x86_feature(x86_featureset, X86FSET_TSC)) {
12460Sstevel@tonic-gate /*
12470Sstevel@tonic-gate * We have a TSC. freq_tsc() knows how to measure the number
12480Sstevel@tonic-gate * of clock cycles sampled against the PIT.
12490Sstevel@tonic-gate */
12503446Smrj ulong_t flags = clear_int_flag();
12510Sstevel@tonic-gate processor_clks = freq_tsc(&pit_counter);
12523446Smrj restore_int_flag(flags);
12530Sstevel@tonic-gate return (mach_calchz(pit_counter, &processor_clks));
12540Sstevel@tonic-gate } else if (x86_vendor == X86_VENDOR_Cyrix || x86_type == X86_TYPE_P5) {
12550Sstevel@tonic-gate #if defined(__amd64)
12560Sstevel@tonic-gate panic("mach_getcpufreq: no TSC!");
12570Sstevel@tonic-gate #elif defined(__i386)
12580Sstevel@tonic-gate /*
12590Sstevel@tonic-gate * We are a Cyrix based on a 6x86 core or an Intel Pentium
12600Sstevel@tonic-gate * for which freq_notsc() knows how to measure the number of
12610Sstevel@tonic-gate * elapsed clock cycles sampled against the PIT
12620Sstevel@tonic-gate */
12633446Smrj ulong_t flags = clear_int_flag();
12640Sstevel@tonic-gate processor_clks = freq_notsc(&pit_counter);
12653446Smrj restore_int_flag(flags);
12660Sstevel@tonic-gate return (mach_calchz(pit_counter, &processor_clks));
12670Sstevel@tonic-gate #endif /* __i386 */
12680Sstevel@tonic-gate }
12690Sstevel@tonic-gate
12700Sstevel@tonic-gate /* We do not know how to calculate cpu frequency for this cpu. */
12710Sstevel@tonic-gate return (0);
12725084Sjohnlev #endif /* __xpv */
12730Sstevel@tonic-gate }
12740Sstevel@tonic-gate
12750Sstevel@tonic-gate /*
12760Sstevel@tonic-gate * If the clock speed of a cpu is found to be reported incorrectly, do not add
12770Sstevel@tonic-gate * to this array, instead improve the accuracy of the algorithm that determines
12780Sstevel@tonic-gate * the clock speed of the processor or extend the implementation to support the
12790Sstevel@tonic-gate * vendor as appropriate. This is here only to support adjusting the speed on
12800Sstevel@tonic-gate * older slower processors that mach_fixcpufreq() would not be able to account
12810Sstevel@tonic-gate * for otherwise.
12820Sstevel@tonic-gate */
12830Sstevel@tonic-gate static int x86_cpu_freq[] = { 60, 75, 80, 90, 120, 160, 166, 175, 180, 233 };
12840Sstevel@tonic-gate
12850Sstevel@tonic-gate /*
12860Sstevel@tonic-gate * On fast processors the clock frequency that is measured may be off by
12870Sstevel@tonic-gate * a few MHz from the value printed on the part. This is a combination of
12880Sstevel@tonic-gate * the factors that for such fast parts being off by this much is within
12890Sstevel@tonic-gate * the tolerances for manufacture and because of the difficulties in the
12900Sstevel@tonic-gate * measurement that can lead to small error. This function uses some
12910Sstevel@tonic-gate * heuristics in order to tweak the value that was measured to match what
12920Sstevel@tonic-gate * is most likely printed on the part.
12930Sstevel@tonic-gate *
12940Sstevel@tonic-gate * Some examples:
12950Sstevel@tonic-gate * AMD Athlon 1000 mhz measured as 998 mhz
12960Sstevel@tonic-gate * Intel Pentium III Xeon 733 mhz measured as 731 mhz
12970Sstevel@tonic-gate * Intel Pentium IV 1500 mhz measured as 1495mhz
12980Sstevel@tonic-gate *
12990Sstevel@tonic-gate * If in the future this function is no longer sufficient to correct
13000Sstevel@tonic-gate * for the error in the measurement, then the algorithm used to perform
13010Sstevel@tonic-gate * the measurement will have to be improved in order to increase accuracy
13020Sstevel@tonic-gate * rather than adding horrible and questionable kludges here.
13030Sstevel@tonic-gate *
13040Sstevel@tonic-gate * This is called after the cyclics subsystem because of the potential
13050Sstevel@tonic-gate * that the heuristics within may give a worse estimate of the clock
13060Sstevel@tonic-gate * frequency than the value that was measured.
13070Sstevel@tonic-gate */
13080Sstevel@tonic-gate static void
mach_fixcpufreq(void)13090Sstevel@tonic-gate mach_fixcpufreq(void)
13100Sstevel@tonic-gate {
13110Sstevel@tonic-gate uint32_t freq, mul, near66, delta66, near50, delta50, fixed, delta, i;
13120Sstevel@tonic-gate
13130Sstevel@tonic-gate freq = (uint32_t)cpu_freq;
13140Sstevel@tonic-gate
13150Sstevel@tonic-gate /*
13160Sstevel@tonic-gate * Find the nearest integer multiple of 200/3 (about 66) MHz to the
13170Sstevel@tonic-gate * measured speed taking into account that the 667 MHz parts were
13180Sstevel@tonic-gate * the first to round-up.
13190Sstevel@tonic-gate */
13200Sstevel@tonic-gate mul = (uint32_t)((3 * (uint64_t)freq + 100) / 200);
13210Sstevel@tonic-gate near66 = (uint32_t)((200 * (uint64_t)mul + ((mul >= 10) ? 1 : 0)) / 3);
13220Sstevel@tonic-gate delta66 = (near66 > freq) ? (near66 - freq) : (freq - near66);
13230Sstevel@tonic-gate
13240Sstevel@tonic-gate /* Find the nearest integer multiple of 50 MHz to the measured speed */
13250Sstevel@tonic-gate mul = (freq + 25) / 50;
13260Sstevel@tonic-gate near50 = mul * 50;
13270Sstevel@tonic-gate delta50 = (near50 > freq) ? (near50 - freq) : (freq - near50);
13280Sstevel@tonic-gate
13290Sstevel@tonic-gate /* Find the closer of the two */
13300Sstevel@tonic-gate if (delta66 < delta50) {
13310Sstevel@tonic-gate fixed = near66;
13320Sstevel@tonic-gate delta = delta66;
13330Sstevel@tonic-gate } else {
13340Sstevel@tonic-gate fixed = near50;
13350Sstevel@tonic-gate delta = delta50;
13360Sstevel@tonic-gate }
13370Sstevel@tonic-gate
13380Sstevel@tonic-gate if (fixed > INT_MAX)
13390Sstevel@tonic-gate return;
13400Sstevel@tonic-gate
13410Sstevel@tonic-gate /*
13420Sstevel@tonic-gate * Some older parts have a core clock frequency that is not an
13430Sstevel@tonic-gate * integral multiple of 50 or 66 MHz. Check if one of the old
13440Sstevel@tonic-gate * clock frequencies is closer to the measured value than any
13450Sstevel@tonic-gate * of the integral multiples of 50 an 66, and if so set fixed
13460Sstevel@tonic-gate * and delta appropriately to represent the closest value.
13470Sstevel@tonic-gate */
13480Sstevel@tonic-gate i = sizeof (x86_cpu_freq) / sizeof (int);
13490Sstevel@tonic-gate while (i > 0) {
13500Sstevel@tonic-gate i--;
13510Sstevel@tonic-gate
13520Sstevel@tonic-gate if (x86_cpu_freq[i] <= freq) {
13530Sstevel@tonic-gate mul = freq - x86_cpu_freq[i];
13540Sstevel@tonic-gate
13550Sstevel@tonic-gate if (mul < delta) {
13560Sstevel@tonic-gate fixed = x86_cpu_freq[i];
13570Sstevel@tonic-gate delta = mul;
13580Sstevel@tonic-gate }
13590Sstevel@tonic-gate
13600Sstevel@tonic-gate break;
13610Sstevel@tonic-gate }
13620Sstevel@tonic-gate
13630Sstevel@tonic-gate mul = x86_cpu_freq[i] - freq;
13640Sstevel@tonic-gate
13650Sstevel@tonic-gate if (mul < delta) {
13660Sstevel@tonic-gate fixed = x86_cpu_freq[i];
13670Sstevel@tonic-gate delta = mul;
13680Sstevel@tonic-gate }
13690Sstevel@tonic-gate }
13700Sstevel@tonic-gate
13710Sstevel@tonic-gate /*
13720Sstevel@tonic-gate * Set a reasonable maximum for how much to correct the measured
13730Sstevel@tonic-gate * result by. This check is here to prevent the adjustment made
13740Sstevel@tonic-gate * by this function from being more harm than good. It is entirely
13750Sstevel@tonic-gate * possible that in the future parts will be made that are not
13760Sstevel@tonic-gate * integral multiples of 66 or 50 in clock frequency or that
13770Sstevel@tonic-gate * someone may overclock a part to some odd frequency. If the
13780Sstevel@tonic-gate * measured value is farther from the corrected value than
13790Sstevel@tonic-gate * allowed, then assume the corrected value is in error and use
13800Sstevel@tonic-gate * the measured value.
13810Sstevel@tonic-gate */
13820Sstevel@tonic-gate if (6 < delta)
13830Sstevel@tonic-gate return;
13840Sstevel@tonic-gate
13850Sstevel@tonic-gate cpu_freq = (int)fixed;
13860Sstevel@tonic-gate }
13870Sstevel@tonic-gate
13880Sstevel@tonic-gate
13890Sstevel@tonic-gate static int
machhztomhz(uint64_t cpu_freq_hz)13900Sstevel@tonic-gate machhztomhz(uint64_t cpu_freq_hz)
13910Sstevel@tonic-gate {
13920Sstevel@tonic-gate uint64_t cpu_mhz;
13930Sstevel@tonic-gate
13940Sstevel@tonic-gate /* Round to nearest MHZ */
13950Sstevel@tonic-gate cpu_mhz = (cpu_freq_hz + (MEGA_HZ / 2)) / MEGA_HZ;
13960Sstevel@tonic-gate
13970Sstevel@tonic-gate if (cpu_mhz > INT_MAX)
13980Sstevel@tonic-gate return (0);
13990Sstevel@tonic-gate
14000Sstevel@tonic-gate return ((int)cpu_mhz);
14010Sstevel@tonic-gate
14020Sstevel@tonic-gate }
14030Sstevel@tonic-gate
14040Sstevel@tonic-gate
14050Sstevel@tonic-gate static int
mach_clkinit(int preferred_mode,int * set_mode)14060Sstevel@tonic-gate mach_clkinit(int preferred_mode, int *set_mode)
14070Sstevel@tonic-gate {
14083446Smrj struct psm_ops *pops;
14090Sstevel@tonic-gate int resolution;
14100Sstevel@tonic-gate
14110Sstevel@tonic-gate pops = mach_set[0];
14120Sstevel@tonic-gate
14130Sstevel@tonic-gate cpu_freq_hz = mach_getcpufreq();
14140Sstevel@tonic-gate
14150Sstevel@tonic-gate cpu_freq = machhztomhz(cpu_freq_hz);
14160Sstevel@tonic-gate
1417*12826Skuriakose.kuruvilla@oracle.com if (!is_x86_feature(x86_featureset, X86FSET_TSC) || (cpu_freq == 0))
14180Sstevel@tonic-gate tsc_gethrtime_enable = 0;
14190Sstevel@tonic-gate
14205084Sjohnlev #ifndef __xpv
14210Sstevel@tonic-gate if (tsc_gethrtime_enable) {
14220Sstevel@tonic-gate tsc_hrtimeinit(cpu_freq_hz);
14235084Sjohnlev } else
14245084Sjohnlev #endif
14255084Sjohnlev {
14260Sstevel@tonic-gate if (pops->psm_hrtimeinit)
14270Sstevel@tonic-gate (*pops->psm_hrtimeinit)();
14280Sstevel@tonic-gate gethrtimef = pops->psm_gethrtime;
14290Sstevel@tonic-gate gethrtimeunscaledf = gethrtimef;
14300Sstevel@tonic-gate /* scalehrtimef will remain dummy */
14310Sstevel@tonic-gate }
14320Sstevel@tonic-gate
14330Sstevel@tonic-gate mach_fixcpufreq();
14340Sstevel@tonic-gate
14350Sstevel@tonic-gate if (mach_ver[0] >= PSM_INFO_VER01_3) {
14365084Sjohnlev if (preferred_mode == TIMER_ONESHOT) {
14370Sstevel@tonic-gate
14380Sstevel@tonic-gate resolution = (*pops->psm_clkinit)(0);
14390Sstevel@tonic-gate if (resolution != 0) {
14400Sstevel@tonic-gate *set_mode = TIMER_ONESHOT;
14410Sstevel@tonic-gate return (resolution);
14420Sstevel@tonic-gate }
14430Sstevel@tonic-gate }
14440Sstevel@tonic-gate
14450Sstevel@tonic-gate /*
14460Sstevel@tonic-gate * either periodic mode was requested or could not set to
14470Sstevel@tonic-gate * one-shot mode
14480Sstevel@tonic-gate */
14490Sstevel@tonic-gate resolution = (*pops->psm_clkinit)(hz);
14500Sstevel@tonic-gate /*
14510Sstevel@tonic-gate * psm should be able to do periodic, so we do not check
14520Sstevel@tonic-gate * for return value of psm_clkinit here.
14530Sstevel@tonic-gate */
14540Sstevel@tonic-gate *set_mode = TIMER_PERIODIC;
14550Sstevel@tonic-gate return (resolution);
14560Sstevel@tonic-gate } else {
14570Sstevel@tonic-gate /*
14580Sstevel@tonic-gate * PSMI interface prior to PSMI_3 does not define a return
14590Sstevel@tonic-gate * value for psm_clkinit, so the return value is ignored.
14600Sstevel@tonic-gate */
14610Sstevel@tonic-gate (void) (*pops->psm_clkinit)(hz);
14620Sstevel@tonic-gate *set_mode = TIMER_PERIODIC;
14630Sstevel@tonic-gate return (nsec_per_tick);
14640Sstevel@tonic-gate }
14650Sstevel@tonic-gate }
14660Sstevel@tonic-gate
14674652Scwb
1468999Slq150181 /*ARGSUSED*/
14690Sstevel@tonic-gate static int
mach_softlvl_to_vect(int ipl)14703446Smrj mach_softlvl_to_vect(int ipl)
14710Sstevel@tonic-gate {
14724652Scwb setsoftint = av_set_softint_pending;
14734652Scwb kdisetsoftint = kdi_av_set_softint_pending;
14740Sstevel@tonic-gate
14750Sstevel@tonic-gate return (PSM_SV_SOFTWARE);
14760Sstevel@tonic-gate }
14770Sstevel@tonic-gate
14783446Smrj #ifdef DEBUG
14793446Smrj /*
14803446Smrj * This is here to allow us to simulate cpus that refuse to start.
14813446Smrj */
14823446Smrj cpuset_t cpufailset;
14833446Smrj #endif
14843446Smrj
14853446Smrj int
mach_cpu_start(struct cpu * cp,void * ctx)14863446Smrj mach_cpu_start(struct cpu *cp, void *ctx)
14870Sstevel@tonic-gate {
14883446Smrj struct psm_ops *pops = mach_set[0];
14893446Smrj processorid_t id = cp->cpu_id;
14900Sstevel@tonic-gate
14913446Smrj #ifdef DEBUG
14923446Smrj if (CPU_IN_SET(cpufailset, id))
14933446Smrj return (0);
14943446Smrj #endif
14953446Smrj return ((*pops->psm_cpu_start)(id, ctx));
14960Sstevel@tonic-gate }
14970Sstevel@tonic-gate
14985295Srandyf int
mach_cpuid_start(processorid_t id,void * ctx)14995295Srandyf mach_cpuid_start(processorid_t id, void *ctx)
15005295Srandyf {
15015295Srandyf struct psm_ops *pops = mach_set[0];
15025295Srandyf
15035295Srandyf #ifdef DEBUG
15045295Srandyf if (CPU_IN_SET(cpufailset, id))
15055295Srandyf return (0);
15065295Srandyf #endif
15075295Srandyf return ((*pops->psm_cpu_start)(id, ctx));
15085295Srandyf }
15095295Srandyf
151012004Sjiang.liu@intel.com int
mach_cpu_stop(cpu_t * cp,void * ctx)151112004Sjiang.liu@intel.com mach_cpu_stop(cpu_t *cp, void *ctx)
151212004Sjiang.liu@intel.com {
151312004Sjiang.liu@intel.com struct psm_ops *pops = mach_set[0];
151412004Sjiang.liu@intel.com psm_cpu_request_t request;
151512004Sjiang.liu@intel.com
151612004Sjiang.liu@intel.com if (pops->psm_cpu_ops == NULL) {
151712004Sjiang.liu@intel.com return (ENOTSUP);
151812004Sjiang.liu@intel.com }
151912004Sjiang.liu@intel.com
152012004Sjiang.liu@intel.com ASSERT(cp->cpu_id != -1);
152112004Sjiang.liu@intel.com request.pcr_cmd = PSM_CPU_STOP;
152212004Sjiang.liu@intel.com request.req.cpu_stop.cpuid = cp->cpu_id;
152312004Sjiang.liu@intel.com request.req.cpu_stop.ctx = ctx;
152412004Sjiang.liu@intel.com
152512004Sjiang.liu@intel.com return ((*pops->psm_cpu_ops)(&request));
152612004Sjiang.liu@intel.com }
152712004Sjiang.liu@intel.com
152812004Sjiang.liu@intel.com int
mach_cpu_add(mach_cpu_add_arg_t * argp,processorid_t * cpuidp)152912004Sjiang.liu@intel.com mach_cpu_add(mach_cpu_add_arg_t *argp, processorid_t *cpuidp)
153012004Sjiang.liu@intel.com {
153112004Sjiang.liu@intel.com int rc;
153212004Sjiang.liu@intel.com struct psm_ops *pops = mach_set[0];
153312004Sjiang.liu@intel.com psm_cpu_request_t request;
153412004Sjiang.liu@intel.com
153512004Sjiang.liu@intel.com if (pops->psm_cpu_ops == NULL) {
153612004Sjiang.liu@intel.com return (ENOTSUP);
153712004Sjiang.liu@intel.com }
153812004Sjiang.liu@intel.com
153912004Sjiang.liu@intel.com request.pcr_cmd = PSM_CPU_ADD;
154012004Sjiang.liu@intel.com request.req.cpu_add.argp = argp;
154112004Sjiang.liu@intel.com request.req.cpu_add.cpuid = -1;
154212004Sjiang.liu@intel.com rc = (*pops->psm_cpu_ops)(&request);
154312004Sjiang.liu@intel.com if (rc == 0) {
154412004Sjiang.liu@intel.com ASSERT(request.req.cpu_add.cpuid != -1);
154512004Sjiang.liu@intel.com *cpuidp = request.req.cpu_add.cpuid;
154612004Sjiang.liu@intel.com }
154712004Sjiang.liu@intel.com
154812004Sjiang.liu@intel.com return (rc);
154912004Sjiang.liu@intel.com }
155012004Sjiang.liu@intel.com
155112004Sjiang.liu@intel.com int
mach_cpu_remove(processorid_t cpuid)155212004Sjiang.liu@intel.com mach_cpu_remove(processorid_t cpuid)
155312004Sjiang.liu@intel.com {
155412004Sjiang.liu@intel.com struct psm_ops *pops = mach_set[0];
155512004Sjiang.liu@intel.com psm_cpu_request_t request;
155612004Sjiang.liu@intel.com
155712004Sjiang.liu@intel.com if (pops->psm_cpu_ops == NULL) {
155812004Sjiang.liu@intel.com return (ENOTSUP);
155912004Sjiang.liu@intel.com }
156012004Sjiang.liu@intel.com
156112004Sjiang.liu@intel.com request.pcr_cmd = PSM_CPU_REMOVE;
156212004Sjiang.liu@intel.com request.req.cpu_remove.cpuid = cpuid;
156312004Sjiang.liu@intel.com
156412004Sjiang.liu@intel.com return ((*pops->psm_cpu_ops)(&request));
156512004Sjiang.liu@intel.com }
156612004Sjiang.liu@intel.com
15679652SMichael.Corcoran@Sun.COM /*
15689652SMichael.Corcoran@Sun.COM * Default handler to create device node for CPU.
15699652SMichael.Corcoran@Sun.COM * One reference count will be held on created device node.
15709652SMichael.Corcoran@Sun.COM */
15719652SMichael.Corcoran@Sun.COM static int
mach_cpu_create_devinfo(cpu_t * cp,dev_info_t ** dipp)15729652SMichael.Corcoran@Sun.COM mach_cpu_create_devinfo(cpu_t *cp, dev_info_t **dipp)
15739652SMichael.Corcoran@Sun.COM {
15749652SMichael.Corcoran@Sun.COM int rv, circ;
15759652SMichael.Corcoran@Sun.COM dev_info_t *dip;
15769652SMichael.Corcoran@Sun.COM static kmutex_t cpu_node_lock;
15779652SMichael.Corcoran@Sun.COM static dev_info_t *cpu_nex_devi = NULL;
15789652SMichael.Corcoran@Sun.COM
15799652SMichael.Corcoran@Sun.COM ASSERT(cp != NULL);
15809652SMichael.Corcoran@Sun.COM ASSERT(dipp != NULL);
15819652SMichael.Corcoran@Sun.COM *dipp = NULL;
15829652SMichael.Corcoran@Sun.COM
15839652SMichael.Corcoran@Sun.COM if (cpu_nex_devi == NULL) {
15849652SMichael.Corcoran@Sun.COM mutex_enter(&cpu_node_lock);
15859652SMichael.Corcoran@Sun.COM /* First check whether cpus exists. */
15869652SMichael.Corcoran@Sun.COM cpu_nex_devi = ddi_find_devinfo("cpus", -1, 0);
15879652SMichael.Corcoran@Sun.COM /* Create cpus if it doesn't exist. */
15889652SMichael.Corcoran@Sun.COM if (cpu_nex_devi == NULL) {
15899652SMichael.Corcoran@Sun.COM ndi_devi_enter(ddi_root_node(), &circ);
15909652SMichael.Corcoran@Sun.COM rv = ndi_devi_alloc(ddi_root_node(), "cpus",
15919652SMichael.Corcoran@Sun.COM (pnode_t)DEVI_SID_NODEID, &dip);
15929652SMichael.Corcoran@Sun.COM if (rv != NDI_SUCCESS) {
15939652SMichael.Corcoran@Sun.COM mutex_exit(&cpu_node_lock);
15949652SMichael.Corcoran@Sun.COM cmn_err(CE_CONT,
15959652SMichael.Corcoran@Sun.COM "?failed to create cpu nexus device.\n");
15969652SMichael.Corcoran@Sun.COM return (PSM_FAILURE);
15979652SMichael.Corcoran@Sun.COM }
15989652SMichael.Corcoran@Sun.COM ASSERT(dip != NULL);
15999652SMichael.Corcoran@Sun.COM (void) ndi_devi_online(dip, 0);
16009652SMichael.Corcoran@Sun.COM ndi_devi_exit(ddi_root_node(), circ);
16019652SMichael.Corcoran@Sun.COM cpu_nex_devi = dip;
16029652SMichael.Corcoran@Sun.COM }
16039652SMichael.Corcoran@Sun.COM mutex_exit(&cpu_node_lock);
16049652SMichael.Corcoran@Sun.COM }
16059652SMichael.Corcoran@Sun.COM
16069652SMichael.Corcoran@Sun.COM /*
16079652SMichael.Corcoran@Sun.COM * create a child node for cpu identified as 'cpu_id'
16089652SMichael.Corcoran@Sun.COM */
16099652SMichael.Corcoran@Sun.COM ndi_devi_enter(cpu_nex_devi, &circ);
161010537SMichael.Corcoran@Sun.COM dip = ddi_add_child(cpu_nex_devi, "cpu", DEVI_SID_NODEID, -1);
16119652SMichael.Corcoran@Sun.COM if (dip == NULL) {
16129652SMichael.Corcoran@Sun.COM cmn_err(CE_CONT,
16139652SMichael.Corcoran@Sun.COM "?failed to create device node for cpu%d.\n", cp->cpu_id);
16149652SMichael.Corcoran@Sun.COM rv = PSM_FAILURE;
16159652SMichael.Corcoran@Sun.COM } else {
16169652SMichael.Corcoran@Sun.COM *dipp = dip;
16179652SMichael.Corcoran@Sun.COM (void) ndi_hold_devi(dip);
16189652SMichael.Corcoran@Sun.COM rv = PSM_SUCCESS;
16199652SMichael.Corcoran@Sun.COM }
16209652SMichael.Corcoran@Sun.COM ndi_devi_exit(cpu_nex_devi, circ);
16219652SMichael.Corcoran@Sun.COM
16229652SMichael.Corcoran@Sun.COM return (rv);
16239652SMichael.Corcoran@Sun.COM }
16249652SMichael.Corcoran@Sun.COM
16259652SMichael.Corcoran@Sun.COM /*
16269652SMichael.Corcoran@Sun.COM * Create cpu device node in device tree and online it.
16279652SMichael.Corcoran@Sun.COM * Return created dip with reference count held if requested.
16289652SMichael.Corcoran@Sun.COM */
16299652SMichael.Corcoran@Sun.COM int
mach_cpu_create_device_node(struct cpu * cp,dev_info_t ** dipp)16309652SMichael.Corcoran@Sun.COM mach_cpu_create_device_node(struct cpu *cp, dev_info_t **dipp)
16319652SMichael.Corcoran@Sun.COM {
16329652SMichael.Corcoran@Sun.COM int rv;
16339652SMichael.Corcoran@Sun.COM dev_info_t *dip = NULL;
16349652SMichael.Corcoran@Sun.COM
16359652SMichael.Corcoran@Sun.COM ASSERT(psm_cpu_create_devinfo != NULL);
16369652SMichael.Corcoran@Sun.COM rv = psm_cpu_create_devinfo(cp, &dip);
16379652SMichael.Corcoran@Sun.COM if (rv == PSM_SUCCESS) {
16389652SMichael.Corcoran@Sun.COM cpuid_set_cpu_properties(dip, cp->cpu_id, cp->cpu_m.mcpu_cpi);
16399652SMichael.Corcoran@Sun.COM /* Recursively attach driver for parent nexus device. */
16409652SMichael.Corcoran@Sun.COM if (i_ddi_attach_node_hierarchy(ddi_get_parent(dip)) ==
16419652SMichael.Corcoran@Sun.COM DDI_SUCCESS) {
16429652SMichael.Corcoran@Sun.COM /* Configure cpu itself and descendants. */
16439652SMichael.Corcoran@Sun.COM (void) ndi_devi_online(dip,
16449652SMichael.Corcoran@Sun.COM NDI_ONLINE_ATTACH | NDI_CONFIG);
16459652SMichael.Corcoran@Sun.COM }
16469652SMichael.Corcoran@Sun.COM if (dipp != NULL) {
16479652SMichael.Corcoran@Sun.COM *dipp = dip;
16489652SMichael.Corcoran@Sun.COM } else {
16499652SMichael.Corcoran@Sun.COM (void) ndi_rele_devi(dip);
16509652SMichael.Corcoran@Sun.COM }
16519652SMichael.Corcoran@Sun.COM }
16529652SMichael.Corcoran@Sun.COM
16539652SMichael.Corcoran@Sun.COM return (rv);
16549652SMichael.Corcoran@Sun.COM }
16559652SMichael.Corcoran@Sun.COM
165612004Sjiang.liu@intel.com /*
165712004Sjiang.liu@intel.com * The dipp contains one of following values on return:
165812004Sjiang.liu@intel.com * - NULL if no device node found
165912004Sjiang.liu@intel.com * - pointer to device node if found
166012004Sjiang.liu@intel.com */
166112004Sjiang.liu@intel.com int
mach_cpu_get_device_node(struct cpu * cp,dev_info_t ** dipp)166212004Sjiang.liu@intel.com mach_cpu_get_device_node(struct cpu *cp, dev_info_t **dipp)
166312004Sjiang.liu@intel.com {
166412004Sjiang.liu@intel.com *dipp = NULL;
166512004Sjiang.liu@intel.com if (psm_cpu_get_devinfo != NULL) {
166612004Sjiang.liu@intel.com if (psm_cpu_get_devinfo(cp, dipp) == PSM_SUCCESS) {
166712004Sjiang.liu@intel.com return (PSM_SUCCESS);
166812004Sjiang.liu@intel.com }
166912004Sjiang.liu@intel.com }
167012004Sjiang.liu@intel.com
167112004Sjiang.liu@intel.com return (PSM_FAILURE);
167212004Sjiang.liu@intel.com }
167312004Sjiang.liu@intel.com
16740Sstevel@tonic-gate /*ARGSUSED*/
16750Sstevel@tonic-gate static int
mach_translate_irq(dev_info_t * dip,int irqno)16760Sstevel@tonic-gate mach_translate_irq(dev_info_t *dip, int irqno)
16770Sstevel@tonic-gate {
16780Sstevel@tonic-gate return (irqno); /* default to NO translation */
16790Sstevel@tonic-gate }
16800Sstevel@tonic-gate
16810Sstevel@tonic-gate static void
mach_notify_error(int level,char * errmsg)16820Sstevel@tonic-gate mach_notify_error(int level, char *errmsg)
16830Sstevel@tonic-gate {
16840Sstevel@tonic-gate /*
16850Sstevel@tonic-gate * SL_FATAL is pass in once panicstr is set, deliver it
16860Sstevel@tonic-gate * as CE_PANIC. Also, translate SL_ codes back to CE_
16870Sstevel@tonic-gate * codes for the psmi handler
16880Sstevel@tonic-gate */
16890Sstevel@tonic-gate if (level & SL_FATAL)
16900Sstevel@tonic-gate (*notify_error)(CE_PANIC, errmsg);
16910Sstevel@tonic-gate else if (level & SL_WARN)
16920Sstevel@tonic-gate (*notify_error)(CE_WARN, errmsg);
16930Sstevel@tonic-gate else if (level & SL_NOTE)
16940Sstevel@tonic-gate (*notify_error)(CE_NOTE, errmsg);
16950Sstevel@tonic-gate else if (level & SL_CONSOLE)
16960Sstevel@tonic-gate (*notify_error)(CE_CONT, errmsg);
16970Sstevel@tonic-gate }
16980Sstevel@tonic-gate
16990Sstevel@tonic-gate /*
17000Sstevel@tonic-gate * It provides the default basic intr_ops interface for the new DDI
17010Sstevel@tonic-gate * interrupt framework if the PSM doesn't have one.
17020Sstevel@tonic-gate *
17030Sstevel@tonic-gate * Input:
17040Sstevel@tonic-gate * dip - pointer to the dev_info structure of the requested device
17050Sstevel@tonic-gate * hdlp - pointer to the internal interrupt handle structure for the
17060Sstevel@tonic-gate * requested interrupt
17070Sstevel@tonic-gate * intr_op - opcode for this call
17080Sstevel@tonic-gate * result - pointer to the integer that will hold the result to be
17090Sstevel@tonic-gate * passed back if return value is PSM_SUCCESS
17100Sstevel@tonic-gate *
17110Sstevel@tonic-gate * Output:
17120Sstevel@tonic-gate * return value is either PSM_SUCCESS or PSM_FAILURE
17130Sstevel@tonic-gate */
17140Sstevel@tonic-gate static int
mach_intr_ops(dev_info_t * dip,ddi_intr_handle_impl_t * hdlp,psm_intr_op_t intr_op,int * result)17150Sstevel@tonic-gate mach_intr_ops(dev_info_t *dip, ddi_intr_handle_impl_t *hdlp,
17160Sstevel@tonic-gate psm_intr_op_t intr_op, int *result)
17170Sstevel@tonic-gate {
17180Sstevel@tonic-gate struct intrspec *ispec;
17190Sstevel@tonic-gate
17200Sstevel@tonic-gate switch (intr_op) {
17210Sstevel@tonic-gate case PSM_INTR_OP_CHECK_MSI:
17220Sstevel@tonic-gate *result = hdlp->ih_type & ~(DDI_INTR_TYPE_MSI |
17234481Sbholler DDI_INTR_TYPE_MSIX);
17240Sstevel@tonic-gate break;
17250Sstevel@tonic-gate case PSM_INTR_OP_ALLOC_VECTORS:
17260Sstevel@tonic-gate if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
17270Sstevel@tonic-gate *result = 1;
17280Sstevel@tonic-gate else
17290Sstevel@tonic-gate *result = 0;
17300Sstevel@tonic-gate break;
17310Sstevel@tonic-gate case PSM_INTR_OP_FREE_VECTORS:
17320Sstevel@tonic-gate break;
17330Sstevel@tonic-gate case PSM_INTR_OP_NAVAIL_VECTORS:
17340Sstevel@tonic-gate if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
17350Sstevel@tonic-gate *result = 1;
17360Sstevel@tonic-gate else
17370Sstevel@tonic-gate *result = 0;
17380Sstevel@tonic-gate break;
17390Sstevel@tonic-gate case PSM_INTR_OP_XLATE_VECTOR:
1740916Sschwartz ispec = ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp;
17410Sstevel@tonic-gate *result = psm_translate_irq(dip, ispec->intrspec_vec);
17420Sstevel@tonic-gate break;
17430Sstevel@tonic-gate case PSM_INTR_OP_GET_CAP:
17440Sstevel@tonic-gate *result = 0;
17450Sstevel@tonic-gate break;
17460Sstevel@tonic-gate case PSM_INTR_OP_GET_PENDING:
17470Sstevel@tonic-gate case PSM_INTR_OP_CLEAR_MASK:
17480Sstevel@tonic-gate case PSM_INTR_OP_SET_MASK:
17490Sstevel@tonic-gate case PSM_INTR_OP_GET_SHARED:
17500Sstevel@tonic-gate case PSM_INTR_OP_SET_PRI:
17510Sstevel@tonic-gate case PSM_INTR_OP_SET_CAP:
1752916Sschwartz case PSM_INTR_OP_SET_CPU:
1753916Sschwartz case PSM_INTR_OP_GET_INTR:
17540Sstevel@tonic-gate default:
17550Sstevel@tonic-gate return (PSM_FAILURE);
17560Sstevel@tonic-gate }
17570Sstevel@tonic-gate return (PSM_SUCCESS);
17580Sstevel@tonic-gate }
17594769Sdp78419 /*
17604769Sdp78419 * Return 1 if CMT load balancing policies should be
17614769Sdp78419 * implemented across instances of the specified hardware
17624769Sdp78419 * sharing relationship.
17634769Sdp78419 */
17644769Sdp78419 int
pg_cmt_load_bal_hw(pghw_type_t hw)17654769Sdp78419 pg_cmt_load_bal_hw(pghw_type_t hw)
17664769Sdp78419 {
17674769Sdp78419 if (hw == PGHW_IPIPE ||
17684769Sdp78419 hw == PGHW_FPU ||
176910947SSrihari.Venkatesan@Sun.COM hw == PGHW_PROCNODE ||
17704769Sdp78419 hw == PGHW_CHIP)
17714769Sdp78419 return (1);
17724769Sdp78419 else
17734769Sdp78419 return (0);
17744769Sdp78419 }
17754769Sdp78419 /*
17764769Sdp78419 * Return 1 if thread affinity polices should be implemented
17774769Sdp78419 * for instances of the specifed hardware sharing relationship.
17784769Sdp78419 */
17794769Sdp78419 int
pg_cmt_affinity_hw(pghw_type_t hw)17804769Sdp78419 pg_cmt_affinity_hw(pghw_type_t hw)
17814769Sdp78419 {
17824769Sdp78419 if (hw == PGHW_CACHE)
17834769Sdp78419 return (1);
17844769Sdp78419 else
17854769Sdp78419 return (0);
17864769Sdp78419 }
178711389SAlexander.Kolbasov@Sun.COM
178811389SAlexander.Kolbasov@Sun.COM /*
178911389SAlexander.Kolbasov@Sun.COM * Return number of counter events requested to measure hardware capacity and
179011389SAlexander.Kolbasov@Sun.COM * utilization and setup CPC requests for specified CPU as needed
179111389SAlexander.Kolbasov@Sun.COM *
179211389SAlexander.Kolbasov@Sun.COM * May return 0 when platform or processor specific code knows that no CPC
179311389SAlexander.Kolbasov@Sun.COM * events should be programmed on this CPU or -1 when platform or processor
179411389SAlexander.Kolbasov@Sun.COM * specific code doesn't know which counter events are best to use and common
179511389SAlexander.Kolbasov@Sun.COM * code should decide for itself
179611389SAlexander.Kolbasov@Sun.COM */
179711389SAlexander.Kolbasov@Sun.COM int
179811389SAlexander.Kolbasov@Sun.COM /* LINTED E_FUNC_ARG_UNUSED */
cu_plat_cpc_init(cpu_t * cp,kcpc_request_list_t * reqs,int nreqs)179911389SAlexander.Kolbasov@Sun.COM cu_plat_cpc_init(cpu_t *cp, kcpc_request_list_t *reqs, int nreqs)
180011389SAlexander.Kolbasov@Sun.COM {
180111389SAlexander.Kolbasov@Sun.COM const char *impl_name;
180211389SAlexander.Kolbasov@Sun.COM
180311389SAlexander.Kolbasov@Sun.COM /*
180411389SAlexander.Kolbasov@Sun.COM * Return error if pcbe_ops not set
180511389SAlexander.Kolbasov@Sun.COM */
180611389SAlexander.Kolbasov@Sun.COM if (pcbe_ops == NULL)
180711389SAlexander.Kolbasov@Sun.COM return (-1);
180811389SAlexander.Kolbasov@Sun.COM
180911389SAlexander.Kolbasov@Sun.COM /*
181011389SAlexander.Kolbasov@Sun.COM * Return that no CPC events should be programmed on hyperthreaded
181111389SAlexander.Kolbasov@Sun.COM * Pentium 4 and return error for all other x86 processors to tell
181211389SAlexander.Kolbasov@Sun.COM * common code to decide what counter events to program on those CPUs
181311389SAlexander.Kolbasov@Sun.COM * for measuring hardware capacity and utilization
181411389SAlexander.Kolbasov@Sun.COM */
181511389SAlexander.Kolbasov@Sun.COM impl_name = pcbe_ops->pcbe_impl_name();
181611389SAlexander.Kolbasov@Sun.COM if (impl_name != NULL && strcmp(impl_name, PCBE_IMPL_NAME_P4HT) == 0)
181711389SAlexander.Kolbasov@Sun.COM return (0);
181811389SAlexander.Kolbasov@Sun.COM else
181911389SAlexander.Kolbasov@Sun.COM return (-1);
182011389SAlexander.Kolbasov@Sun.COM }
1821