1 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 /* 26 * Copyright (c) 2009-2010, Intel Corporation. 27 * All rights reserved. 28 */ 29 30 #define PSMI_1_7 31 #include <sys/smp_impldefs.h> 32 #include <sys/psm.h> 33 #include <sys/psm_modctl.h> 34 #include <sys/pit.h> 35 #include <sys/cmn_err.h> 36 #include <sys/strlog.h> 37 #include <sys/clock.h> 38 #include <sys/debug.h> 39 #include <sys/rtc.h> 40 #include <sys/x86_archext.h> 41 #include <sys/cpupart.h> 42 #include <sys/cpuvar.h> 43 #include <sys/cpu_event.h> 44 #include <sys/cmt.h> 45 #include <sys/cpu.h> 46 #include <sys/disp.h> 47 #include <sys/archsystm.h> 48 #include <sys/machsystm.h> 49 #include <sys/sysmacros.h> 50 #include <sys/memlist.h> 51 #include <sys/param.h> 52 #include <sys/promif.h> 53 #include <sys/cpu_pm.h> 54 #if defined(__xpv) 55 #include <sys/hypervisor.h> 56 #endif 57 #include <sys/mach_intr.h> 58 #include <vm/hat_i86.h> 59 #include <sys/kdi_machimpl.h> 60 #include <sys/sdt.h> 61 #include <sys/hpet.h> 62 #include <sys/sunddi.h> 63 #include <sys/sunndi.h> 64 #include <sys/cpc_pcbe.h> 65 66 #define OFFSETOF(s, m) (size_t)(&(((s *)0)->m)) 67 68 /* 69 * Local function prototypes 70 */ 71 static int mp_disable_intr(processorid_t cpun); 72 static void mp_enable_intr(processorid_t cpun); 73 static void mach_init(); 74 static void mach_picinit(); 75 static int machhztomhz(uint64_t cpu_freq_hz); 76 static uint64_t mach_getcpufreq(void); 77 static void mach_fixcpufreq(void); 78 static int mach_clkinit(int, int *); 79 static void mach_smpinit(void); 80 static int mach_softlvl_to_vect(int ipl); 81 static void mach_get_platform(int owner); 82 static void mach_construct_info(); 83 static int mach_translate_irq(dev_info_t *dip, int irqno); 84 static int mach_intr_ops(dev_info_t *, ddi_intr_handle_impl_t *, 85 psm_intr_op_t, int *); 86 static void mach_notify_error(int level, char *errmsg); 87 static hrtime_t dummy_hrtime(void); 88 static void dummy_scalehrtime(hrtime_t *); 89 static uint64_t dummy_unscalehrtime(hrtime_t); 90 void cpu_idle(void); 91 static void cpu_wakeup(cpu_t *, int); 92 #ifndef __xpv 93 void cpu_idle_mwait(void); 94 static void cpu_wakeup_mwait(cpu_t *, int); 95 #endif 96 static int mach_cpu_create_devinfo(cpu_t *cp, dev_info_t **dipp); 97 98 /* 99 * External reference functions 100 */ 101 extern void return_instr(); 102 extern uint64_t freq_tsc(uint32_t *); 103 #if defined(__i386) 104 extern uint64_t freq_notsc(uint32_t *); 105 #endif 106 extern void pc_gethrestime(timestruc_t *); 107 extern int cpuid_get_coreid(cpu_t *); 108 extern int cpuid_get_chipid(cpu_t *); 109 110 /* 111 * PSM functions initialization 112 */ 113 void (*psm_shutdownf)(int, int) = (void (*)(int, int))return_instr; 114 void (*psm_preshutdownf)(int, int) = (void (*)(int, int))return_instr; 115 void (*psm_notifyf)(int) = (void (*)(int))return_instr; 116 void (*psm_set_idle_cpuf)(int) = (void (*)(int))return_instr; 117 void (*psm_unset_idle_cpuf)(int) = (void (*)(int))return_instr; 118 void (*psminitf)() = mach_init; 119 void (*picinitf)() = return_instr; 120 int (*clkinitf)(int, int *) = (int (*)(int, int *))return_instr; 121 int (*ap_mlsetup)() = (int (*)(void))return_instr; 122 void (*send_dirintf)() = return_instr; 123 void (*setspl)(int) = (void (*)(int))return_instr; 124 int (*addspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr; 125 int (*delspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr; 126 int (*get_pending_spl)(void) = (int (*)(void))return_instr; 127 int (*addintr)(void *, int, avfunc, char *, int, caddr_t, caddr_t, 128 uint64_t *, dev_info_t *) = NULL; 129 void (*remintr)(void *, int, avfunc, int) = NULL; 130 void (*kdisetsoftint)(int, struct av_softinfo *)= 131 (void (*)(int, struct av_softinfo *))return_instr; 132 void (*setsoftint)(int, struct av_softinfo *)= 133 (void (*)(int, struct av_softinfo *))return_instr; 134 int (*slvltovect)(int) = (int (*)(int))return_instr; 135 int (*setlvl)(int, int *) = (int (*)(int, int *))return_instr; 136 void (*setlvlx)(int, int) = (void (*)(int, int))return_instr; 137 int (*psm_disable_intr)(int) = mp_disable_intr; 138 void (*psm_enable_intr)(int) = mp_enable_intr; 139 hrtime_t (*gethrtimef)(void) = dummy_hrtime; 140 hrtime_t (*gethrtimeunscaledf)(void) = dummy_hrtime; 141 void (*scalehrtimef)(hrtime_t *) = dummy_scalehrtime; 142 uint64_t (*unscalehrtimef)(hrtime_t) = dummy_unscalehrtime; 143 int (*psm_translate_irq)(dev_info_t *, int) = mach_translate_irq; 144 void (*gethrestimef)(timestruc_t *) = pc_gethrestime; 145 void (*psm_notify_error)(int, char *) = (void (*)(int, char *))NULL; 146 int (*psm_get_clockirq)(int) = NULL; 147 int (*psm_get_ipivect)(int, int) = NULL; 148 uchar_t (*psm_get_ioapicid)(uchar_t) = NULL; 149 uint32_t (*psm_get_localapicid)(uint32_t) = NULL; 150 uchar_t (*psm_xlate_vector_by_irq)(uchar_t) = NULL; 151 152 int (*psm_clkinit)(int) = NULL; 153 void (*psm_timer_reprogram)(hrtime_t) = NULL; 154 void (*psm_timer_enable)(void) = NULL; 155 void (*psm_timer_disable)(void) = NULL; 156 void (*psm_post_cyclic_setup)(void *arg) = NULL; 157 int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *, psm_intr_op_t, 158 int *) = mach_intr_ops; 159 int (*psm_state)(psm_state_request_t *) = (int (*)(psm_state_request_t *)) 160 return_instr; 161 162 void (*notify_error)(int, char *) = (void (*)(int, char *))return_instr; 163 void (*hrtime_tick)(void) = return_instr; 164 165 int (*psm_cpu_create_devinfo)(cpu_t *, dev_info_t **) = mach_cpu_create_devinfo; 166 int (*psm_cpu_get_devinfo)(cpu_t *, dev_info_t **) = NULL; 167 168 /* global IRM pool for APIX (PSM) module */ 169 ddi_irm_pool_t *apix_irm_pool_p = NULL; 170 171 /* 172 * True if the generic TSC code is our source of hrtime, rather than whatever 173 * the PSM can provide. 174 */ 175 #ifdef __xpv 176 int tsc_gethrtime_enable = 0; 177 #else 178 int tsc_gethrtime_enable = 1; 179 #endif 180 int tsc_gethrtime_initted = 0; 181 182 /* 183 * True if the hrtime implementation is "hires"; namely, better than microdata. 184 */ 185 int gethrtime_hires = 0; 186 187 /* 188 * Local Static Data 189 */ 190 static struct psm_ops mach_ops; 191 static struct psm_ops *mach_set[4] = {&mach_ops, NULL, NULL, NULL}; 192 static ushort_t mach_ver[4] = {0, 0, 0, 0}; 193 194 /* 195 * virtualization support for psm 196 */ 197 void *psm_vt_ops = NULL; 198 /* 199 * If non-zero, idle cpus will become "halted" when there's 200 * no work to do. 201 */ 202 int idle_cpu_use_hlt = 1; 203 204 #ifndef __xpv 205 /* 206 * If non-zero, idle cpus will use mwait if available to halt instead of hlt. 207 */ 208 int idle_cpu_prefer_mwait = 1; 209 /* 210 * Set to 0 to avoid MONITOR+CLFLUSH assertion. 211 */ 212 int idle_cpu_assert_cflush_monitor = 1; 213 214 /* 215 * If non-zero, idle cpus will not use power saving Deep C-States idle loop. 216 */ 217 int idle_cpu_no_deep_c = 0; 218 /* 219 * Non-power saving idle loop and wakeup pointers. 220 * Allows user to toggle Deep Idle power saving feature on/off. 221 */ 222 void (*non_deep_idle_cpu)() = cpu_idle; 223 void (*non_deep_idle_disp_enq_thread)(cpu_t *, int); 224 225 /* 226 * Object for the kernel to access the HPET. 227 */ 228 hpet_t hpet; 229 230 #endif /* ifndef __xpv */ 231 232 uint_t cp_haltset_fanout = 0; 233 234 /*ARGSUSED*/ 235 int 236 pg_plat_hw_shared(cpu_t *cp, pghw_type_t hw) 237 { 238 switch (hw) { 239 case PGHW_IPIPE: 240 if (x86_feature & (X86_HTT)) { 241 /* 242 * Hyper-threading is SMT 243 */ 244 return (1); 245 } else { 246 return (0); 247 } 248 case PGHW_PROCNODE: 249 if (cpuid_get_procnodes_per_pkg(cp) > 1) 250 return (1); 251 else 252 return (0); 253 case PGHW_CHIP: 254 if (x86_feature & (X86_CMP|X86_HTT)) 255 return (1); 256 else 257 return (0); 258 case PGHW_CACHE: 259 if (cpuid_get_ncpu_sharing_last_cache(cp) > 1) 260 return (1); 261 else 262 return (0); 263 case PGHW_POW_ACTIVE: 264 if (cpupm_domain_id(cp, CPUPM_DTYPE_ACTIVE) != (id_t)-1) 265 return (1); 266 else 267 return (0); 268 case PGHW_POW_IDLE: 269 if (cpupm_domain_id(cp, CPUPM_DTYPE_IDLE) != (id_t)-1) 270 return (1); 271 else 272 return (0); 273 default: 274 return (0); 275 } 276 } 277 278 /* 279 * Compare two CPUs and see if they have a pghw_type_t sharing relationship 280 * If pghw_type_t is an unsupported hardware type, then return -1 281 */ 282 int 283 pg_plat_cpus_share(cpu_t *cpu_a, cpu_t *cpu_b, pghw_type_t hw) 284 { 285 id_t pgp_a, pgp_b; 286 287 pgp_a = pg_plat_hw_instance_id(cpu_a, hw); 288 pgp_b = pg_plat_hw_instance_id(cpu_b, hw); 289 290 if (pgp_a == -1 || pgp_b == -1) 291 return (-1); 292 293 return (pgp_a == pgp_b); 294 } 295 296 /* 297 * Return a physical instance identifier for known hardware sharing 298 * relationships 299 */ 300 id_t 301 pg_plat_hw_instance_id(cpu_t *cpu, pghw_type_t hw) 302 { 303 switch (hw) { 304 case PGHW_IPIPE: 305 return (cpuid_get_coreid(cpu)); 306 case PGHW_CACHE: 307 return (cpuid_get_last_lvl_cacheid(cpu)); 308 case PGHW_PROCNODE: 309 return (cpuid_get_procnodeid(cpu)); 310 case PGHW_CHIP: 311 return (cpuid_get_chipid(cpu)); 312 case PGHW_POW_ACTIVE: 313 return (cpupm_domain_id(cpu, CPUPM_DTYPE_ACTIVE)); 314 case PGHW_POW_IDLE: 315 return (cpupm_domain_id(cpu, CPUPM_DTYPE_IDLE)); 316 default: 317 return (-1); 318 } 319 } 320 321 /* 322 * Express preference for optimizing for sharing relationship 323 * hw1 vs hw2 324 */ 325 pghw_type_t 326 pg_plat_hw_rank(pghw_type_t hw1, pghw_type_t hw2) 327 { 328 int i, rank1, rank2; 329 330 static pghw_type_t hw_hier[] = { 331 PGHW_IPIPE, 332 PGHW_CACHE, 333 PGHW_PROCNODE, 334 PGHW_CHIP, 335 PGHW_POW_IDLE, 336 PGHW_POW_ACTIVE, 337 PGHW_NUM_COMPONENTS 338 }; 339 340 for (i = 0; hw_hier[i] != PGHW_NUM_COMPONENTS; i++) { 341 if (hw_hier[i] == hw1) 342 rank1 = i; 343 if (hw_hier[i] == hw2) 344 rank2 = i; 345 } 346 347 if (rank1 > rank2) 348 return (hw1); 349 else 350 return (hw2); 351 } 352 353 /* 354 * Override the default CMT dispatcher policy for the specified 355 * hardware sharing relationship 356 */ 357 pg_cmt_policy_t 358 pg_plat_cmt_policy(pghw_type_t hw) 359 { 360 /* 361 * For shared caches, also load balance across them to 362 * maximize aggregate cache capacity 363 */ 364 switch (hw) { 365 case PGHW_CACHE: 366 return (CMT_BALANCE|CMT_AFFINITY); 367 default: 368 return (CMT_NO_POLICY); 369 } 370 } 371 372 id_t 373 pg_plat_get_core_id(cpu_t *cpu) 374 { 375 return ((id_t)cpuid_get_coreid(cpu)); 376 } 377 378 void 379 cmp_set_nosteal_interval(void) 380 { 381 /* Set the nosteal interval (used by disp_getbest()) to 100us */ 382 nosteal_nsec = 100000UL; 383 } 384 385 /* 386 * Routine to ensure initial callers to hrtime gets 0 as return 387 */ 388 static hrtime_t 389 dummy_hrtime(void) 390 { 391 return (0); 392 } 393 394 /* ARGSUSED */ 395 static void 396 dummy_scalehrtime(hrtime_t *ticks) 397 {} 398 399 static uint64_t 400 dummy_unscalehrtime(hrtime_t nsecs) 401 { 402 return ((uint64_t)nsecs); 403 } 404 405 /* 406 * Supports Deep C-State power saving idle loop. 407 */ 408 void 409 cpu_idle_adaptive(void) 410 { 411 (*CPU->cpu_m.mcpu_idle_cpu)(); 412 } 413 414 /* 415 * Function called by CPU idle notification framework to check whether CPU 416 * has been awakened. It will be called with interrupt disabled. 417 * If CPU has been awakened, call cpu_idle_exit() to notify CPU idle 418 * notification framework. 419 */ 420 /*ARGSUSED*/ 421 static void 422 cpu_idle_check_wakeup(void *arg) 423 { 424 /* 425 * Toggle interrupt flag to detect pending interrupts. 426 * If interrupt happened, do_interrupt() will notify CPU idle 427 * notification framework so no need to call cpu_idle_exit() here. 428 */ 429 sti(); 430 SMT_PAUSE(); 431 cli(); 432 } 433 434 /* 435 * Idle the present CPU until wakened via an interrupt 436 */ 437 void 438 cpu_idle(void) 439 { 440 cpu_t *cpup = CPU; 441 processorid_t cpu_sid = cpup->cpu_seqid; 442 cpupart_t *cp = cpup->cpu_part; 443 int hset_update = 1; 444 445 /* 446 * If this CPU is online, and there's multiple CPUs 447 * in the system, then we should notate our halting 448 * by adding ourselves to the partition's halted CPU 449 * bitmap. This allows other CPUs to find/awaken us when 450 * work becomes available. 451 */ 452 if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1) 453 hset_update = 0; 454 455 /* 456 * Add ourselves to the partition's halted CPUs bitmap 457 * and set our HALTED flag, if necessary. 458 * 459 * When a thread becomes runnable, it is placed on the queue 460 * and then the halted CPU bitmap is checked to determine who 461 * (if anyone) should be awakened. We therefore need to first 462 * add ourselves to the bitmap, and and then check if there 463 * is any work available. The order is important to prevent a race 464 * that can lead to work languishing on a run queue somewhere while 465 * this CPU remains halted. 466 * 467 * Either the producing CPU will see we're halted and will awaken us, 468 * or this CPU will see the work available in disp_anywork(). 469 * 470 * Note that memory barriers after updating the HALTED flag 471 * are not necessary since an atomic operation (updating the bitset) 472 * immediately follows. On x86 the atomic operation acts as a 473 * memory barrier for the update of cpu_disp_flags. 474 */ 475 if (hset_update) { 476 cpup->cpu_disp_flags |= CPU_DISP_HALTED; 477 bitset_atomic_add(&cp->cp_haltset, cpu_sid); 478 } 479 480 /* 481 * Check to make sure there's really nothing to do. 482 * Work destined for this CPU may become available after 483 * this check. We'll be notified through the clearing of our 484 * bit in the halted CPU bitmap, and a poke. 485 */ 486 if (disp_anywork()) { 487 if (hset_update) { 488 cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 489 bitset_atomic_del(&cp->cp_haltset, cpu_sid); 490 } 491 return; 492 } 493 494 /* 495 * We're on our way to being halted. 496 * 497 * Disable interrupts now, so that we'll awaken immediately 498 * after halting if someone tries to poke us between now and 499 * the time we actually halt. 500 * 501 * We check for the presence of our bit after disabling interrupts. 502 * If it's cleared, we'll return. If the bit is cleared after 503 * we check then the poke will pop us out of the halted state. 504 * 505 * This means that the ordering of the poke and the clearing 506 * of the bit by cpu_wakeup is important. 507 * cpu_wakeup() must clear, then poke. 508 * cpu_idle() must disable interrupts, then check for the bit. 509 */ 510 cli(); 511 512 if (hset_update && bitset_in_set(&cp->cp_haltset, cpu_sid) == 0) { 513 cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 514 sti(); 515 return; 516 } 517 518 /* 519 * The check for anything locally runnable is here for performance 520 * and isn't needed for correctness. disp_nrunnable ought to be 521 * in our cache still, so it's inexpensive to check, and if there 522 * is anything runnable we won't have to wait for the poke. 523 */ 524 if (cpup->cpu_disp->disp_nrunnable != 0) { 525 if (hset_update) { 526 cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 527 bitset_atomic_del(&cp->cp_haltset, cpu_sid); 528 } 529 sti(); 530 return; 531 } 532 533 if (cpu_idle_enter(IDLE_STATE_C1, 0, 534 cpu_idle_check_wakeup, NULL) == 0) { 535 mach_cpu_idle(); 536 cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE); 537 } 538 539 /* 540 * We're no longer halted 541 */ 542 if (hset_update) { 543 cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 544 bitset_atomic_del(&cp->cp_haltset, cpu_sid); 545 } 546 } 547 548 549 /* 550 * If "cpu" is halted, then wake it up clearing its halted bit in advance. 551 * Otherwise, see if other CPUs in the cpu partition are halted and need to 552 * be woken up so that they can steal the thread we placed on this CPU. 553 * This function is only used on MP systems. 554 */ 555 static void 556 cpu_wakeup(cpu_t *cpu, int bound) 557 { 558 uint_t cpu_found; 559 processorid_t cpu_sid; 560 cpupart_t *cp; 561 562 cp = cpu->cpu_part; 563 cpu_sid = cpu->cpu_seqid; 564 if (bitset_in_set(&cp->cp_haltset, cpu_sid)) { 565 /* 566 * Clear the halted bit for that CPU since it will be 567 * poked in a moment. 568 */ 569 bitset_atomic_del(&cp->cp_haltset, cpu_sid); 570 /* 571 * We may find the current CPU present in the halted cpuset 572 * if we're in the context of an interrupt that occurred 573 * before we had a chance to clear our bit in cpu_idle(). 574 * Poking ourself is obviously unnecessary, since if 575 * we're here, we're not halted. 576 */ 577 if (cpu != CPU) 578 poke_cpu(cpu->cpu_id); 579 return; 580 } else { 581 /* 582 * This cpu isn't halted, but it's idle or undergoing a 583 * context switch. No need to awaken anyone else. 584 */ 585 if (cpu->cpu_thread == cpu->cpu_idle_thread || 586 cpu->cpu_disp_flags & CPU_DISP_DONTSTEAL) 587 return; 588 } 589 590 /* 591 * No need to wake up other CPUs if this is for a bound thread. 592 */ 593 if (bound) 594 return; 595 596 /* 597 * The CPU specified for wakeup isn't currently halted, so check 598 * to see if there are any other halted CPUs in the partition, 599 * and if there are then awaken one. 600 */ 601 do { 602 cpu_found = bitset_find(&cp->cp_haltset); 603 if (cpu_found == (uint_t)-1) 604 return; 605 } while (bitset_atomic_test_and_del(&cp->cp_haltset, cpu_found) < 0); 606 607 if (cpu_found != CPU->cpu_seqid) { 608 poke_cpu(cpu_seq[cpu_found]->cpu_id); 609 } 610 } 611 612 #ifndef __xpv 613 /* 614 * Function called by CPU idle notification framework to check whether CPU 615 * has been awakened. It will be called with interrupt disabled. 616 * If CPU has been awakened, call cpu_idle_exit() to notify CPU idle 617 * notification framework. 618 */ 619 static void 620 cpu_idle_mwait_check_wakeup(void *arg) 621 { 622 volatile uint32_t *mcpu_mwait = (volatile uint32_t *)arg; 623 624 ASSERT(arg != NULL); 625 if (*mcpu_mwait != MWAIT_HALTED) { 626 /* 627 * CPU has been awakened, notify CPU idle notification system. 628 */ 629 cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE); 630 } else { 631 /* 632 * Toggle interrupt flag to detect pending interrupts. 633 * If interrupt happened, do_interrupt() will notify CPU idle 634 * notification framework so no need to call cpu_idle_exit() 635 * here. 636 */ 637 sti(); 638 SMT_PAUSE(); 639 cli(); 640 } 641 } 642 643 /* 644 * Idle the present CPU until awakened via touching its monitored line 645 */ 646 void 647 cpu_idle_mwait(void) 648 { 649 volatile uint32_t *mcpu_mwait = CPU->cpu_m.mcpu_mwait; 650 cpu_t *cpup = CPU; 651 processorid_t cpu_sid = cpup->cpu_seqid; 652 cpupart_t *cp = cpup->cpu_part; 653 int hset_update = 1; 654 655 /* 656 * Set our mcpu_mwait here, so we can tell if anyone tries to 657 * wake us between now and when we call mwait. No other cpu will 658 * attempt to set our mcpu_mwait until we add ourself to the halted 659 * CPU bitmap. 660 */ 661 *mcpu_mwait = MWAIT_HALTED; 662 663 /* 664 * If this CPU is online, and there's multiple CPUs 665 * in the system, then we should note our halting 666 * by adding ourselves to the partition's halted CPU 667 * bitmap. This allows other CPUs to find/awaken us when 668 * work becomes available. 669 */ 670 if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1) 671 hset_update = 0; 672 673 /* 674 * Add ourselves to the partition's halted CPUs bitmap 675 * and set our HALTED flag, if necessary. 676 * 677 * When a thread becomes runnable, it is placed on the queue 678 * and then the halted CPU bitmap is checked to determine who 679 * (if anyone) should be awakened. We therefore need to first 680 * add ourselves to the bitmap, and and then check if there 681 * is any work available. 682 * 683 * Note that memory barriers after updating the HALTED flag 684 * are not necessary since an atomic operation (updating the bitmap) 685 * immediately follows. On x86 the atomic operation acts as a 686 * memory barrier for the update of cpu_disp_flags. 687 */ 688 if (hset_update) { 689 cpup->cpu_disp_flags |= CPU_DISP_HALTED; 690 bitset_atomic_add(&cp->cp_haltset, cpu_sid); 691 } 692 693 /* 694 * Check to make sure there's really nothing to do. 695 * Work destined for this CPU may become available after 696 * this check. We'll be notified through the clearing of our 697 * bit in the halted CPU bitmap, and a write to our mcpu_mwait. 698 * 699 * disp_anywork() checks disp_nrunnable, so we do not have to later. 700 */ 701 if (disp_anywork()) { 702 if (hset_update) { 703 cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 704 bitset_atomic_del(&cp->cp_haltset, cpu_sid); 705 } 706 return; 707 } 708 709 /* 710 * We're on our way to being halted. 711 * To avoid a lost wakeup, arm the monitor before checking if another 712 * cpu wrote to mcpu_mwait to wake us up. 713 */ 714 i86_monitor(mcpu_mwait, 0, 0); 715 if (*mcpu_mwait == MWAIT_HALTED) { 716 if (cpu_idle_enter(IDLE_STATE_C1, 0, 717 cpu_idle_mwait_check_wakeup, (void *)mcpu_mwait) == 0) { 718 if (*mcpu_mwait == MWAIT_HALTED) { 719 i86_mwait(0, 0); 720 } 721 cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE); 722 } 723 } 724 725 /* 726 * We're no longer halted 727 */ 728 if (hset_update) { 729 cpup->cpu_disp_flags &= ~CPU_DISP_HALTED; 730 bitset_atomic_del(&cp->cp_haltset, cpu_sid); 731 } 732 } 733 734 /* 735 * If "cpu" is halted in mwait, then wake it up clearing its halted bit in 736 * advance. Otherwise, see if other CPUs in the cpu partition are halted and 737 * need to be woken up so that they can steal the thread we placed on this CPU. 738 * This function is only used on MP systems. 739 */ 740 static void 741 cpu_wakeup_mwait(cpu_t *cp, int bound) 742 { 743 cpupart_t *cpu_part; 744 uint_t cpu_found; 745 processorid_t cpu_sid; 746 747 cpu_part = cp->cpu_part; 748 cpu_sid = cp->cpu_seqid; 749 750 /* 751 * Clear the halted bit for that CPU since it will be woken up 752 * in a moment. 753 */ 754 if (bitset_in_set(&cpu_part->cp_haltset, cpu_sid)) { 755 /* 756 * Clear the halted bit for that CPU since it will be 757 * poked in a moment. 758 */ 759 bitset_atomic_del(&cpu_part->cp_haltset, cpu_sid); 760 /* 761 * We may find the current CPU present in the halted cpuset 762 * if we're in the context of an interrupt that occurred 763 * before we had a chance to clear our bit in cpu_idle(). 764 * Waking ourself is obviously unnecessary, since if 765 * we're here, we're not halted. 766 * 767 * monitor/mwait wakeup via writing to our cache line is 768 * harmless and less expensive than always checking if we 769 * are waking ourself which is an uncommon case. 770 */ 771 MWAIT_WAKEUP(cp); /* write to monitored line */ 772 return; 773 } else { 774 /* 775 * This cpu isn't halted, but it's idle or undergoing a 776 * context switch. No need to awaken anyone else. 777 */ 778 if (cp->cpu_thread == cp->cpu_idle_thread || 779 cp->cpu_disp_flags & CPU_DISP_DONTSTEAL) 780 return; 781 } 782 783 /* 784 * No need to wake up other CPUs if the thread we just enqueued 785 * is bound. 786 */ 787 if (bound || ncpus == 1) 788 return; 789 790 /* 791 * See if there's any other halted CPUs. If there are, then 792 * select one, and awaken it. 793 * It's possible that after we find a CPU, somebody else 794 * will awaken it before we get the chance. 795 * In that case, look again. 796 */ 797 do { 798 cpu_found = bitset_find(&cpu_part->cp_haltset); 799 if (cpu_found == (uint_t)-1) 800 return; 801 } while (bitset_atomic_test_and_del(&cpu_part->cp_haltset, 802 cpu_found) < 0); 803 804 /* 805 * Do not check if cpu_found is ourself as monitor/mwait 806 * wakeup is cheap. 807 */ 808 MWAIT_WAKEUP(cpu_seq[cpu_found]); /* write to monitored line */ 809 } 810 811 #endif 812 813 void (*cpu_pause_handler)(volatile char *) = NULL; 814 815 static int 816 mp_disable_intr(int cpun) 817 { 818 /* 819 * switch to the offline cpu 820 */ 821 affinity_set(cpun); 822 /* 823 * raise ipl to just below cross call 824 */ 825 splx(XC_SYS_PIL - 1); 826 /* 827 * set base spl to prevent the next swtch to idle from 828 * lowering back to ipl 0 829 */ 830 CPU->cpu_intr_actv |= (1 << (XC_SYS_PIL - 1)); 831 set_base_spl(); 832 affinity_clear(); 833 return (DDI_SUCCESS); 834 } 835 836 static void 837 mp_enable_intr(int cpun) 838 { 839 /* 840 * switch to the online cpu 841 */ 842 affinity_set(cpun); 843 /* 844 * clear the interrupt active mask 845 */ 846 CPU->cpu_intr_actv &= ~(1 << (XC_SYS_PIL - 1)); 847 set_base_spl(); 848 (void) spl0(); 849 affinity_clear(); 850 } 851 852 static void 853 mach_get_platform(int owner) 854 { 855 void **srv_opsp; 856 void **clt_opsp; 857 int i; 858 int total_ops; 859 860 /* fix up psm ops */ 861 srv_opsp = (void **)mach_set[0]; 862 clt_opsp = (void **)mach_set[owner]; 863 if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01) 864 total_ops = sizeof (struct psm_ops_ver01) / 865 sizeof (void (*)(void)); 866 else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_1) 867 /* no psm_notify_func */ 868 total_ops = OFFSETOF(struct psm_ops, psm_notify_func) / 869 sizeof (void (*)(void)); 870 else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_2) 871 /* no psm_timer funcs */ 872 total_ops = OFFSETOF(struct psm_ops, psm_timer_reprogram) / 873 sizeof (void (*)(void)); 874 else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_3) 875 /* no psm_preshutdown function */ 876 total_ops = OFFSETOF(struct psm_ops, psm_preshutdown) / 877 sizeof (void (*)(void)); 878 else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_4) 879 /* no psm_intr_ops function */ 880 total_ops = OFFSETOF(struct psm_ops, psm_intr_ops) / 881 sizeof (void (*)(void)); 882 else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_5) 883 /* no psm_state function */ 884 total_ops = OFFSETOF(struct psm_ops, psm_state) / 885 sizeof (void (*)(void)); 886 else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_6) 887 /* no psm_cpu_ops function */ 888 total_ops = OFFSETOF(struct psm_ops, psm_cpu_ops) / 889 sizeof (void (*)(void)); 890 else 891 total_ops = sizeof (struct psm_ops) / sizeof (void (*)(void)); 892 893 /* 894 * Save the version of the PSM module, in case we need to 895 * behave differently based on version. 896 */ 897 mach_ver[0] = mach_ver[owner]; 898 899 for (i = 0; i < total_ops; i++) 900 if (clt_opsp[i] != NULL) 901 srv_opsp[i] = clt_opsp[i]; 902 } 903 904 static void 905 mach_construct_info() 906 { 907 struct psm_sw *swp; 908 int mach_cnt[PSM_OWN_OVERRIDE+1] = {0}; 909 int conflict_owner = 0; 910 911 if (psmsw->psw_forw == psmsw) 912 panic("No valid PSM modules found"); 913 mutex_enter(&psmsw_lock); 914 for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) { 915 if (!(swp->psw_flag & PSM_MOD_IDENTIFY)) 916 continue; 917 mach_set[swp->psw_infop->p_owner] = swp->psw_infop->p_ops; 918 mach_ver[swp->psw_infop->p_owner] = swp->psw_infop->p_version; 919 mach_cnt[swp->psw_infop->p_owner]++; 920 } 921 mutex_exit(&psmsw_lock); 922 923 mach_get_platform(PSM_OWN_SYS_DEFAULT); 924 925 /* check to see are there any conflicts */ 926 if (mach_cnt[PSM_OWN_EXCLUSIVE] > 1) 927 conflict_owner = PSM_OWN_EXCLUSIVE; 928 if (mach_cnt[PSM_OWN_OVERRIDE] > 1) 929 conflict_owner = PSM_OWN_OVERRIDE; 930 if (conflict_owner) { 931 /* remove all psm modules except uppc */ 932 cmn_err(CE_WARN, 933 "Conflicts detected on the following PSM modules:"); 934 mutex_enter(&psmsw_lock); 935 for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) { 936 if (swp->psw_infop->p_owner == conflict_owner) 937 cmn_err(CE_WARN, "%s ", 938 swp->psw_infop->p_mach_idstring); 939 } 940 mutex_exit(&psmsw_lock); 941 cmn_err(CE_WARN, 942 "Setting the system back to SINGLE processor mode!"); 943 cmn_err(CE_WARN, 944 "Please edit /etc/mach to remove the invalid PSM module."); 945 return; 946 } 947 948 if (mach_set[PSM_OWN_EXCLUSIVE]) 949 mach_get_platform(PSM_OWN_EXCLUSIVE); 950 951 if (mach_set[PSM_OWN_OVERRIDE]) 952 mach_get_platform(PSM_OWN_OVERRIDE); 953 } 954 955 static void 956 mach_init() 957 { 958 struct psm_ops *pops; 959 960 mach_construct_info(); 961 962 pops = mach_set[0]; 963 964 /* register the interrupt and clock initialization rotuines */ 965 picinitf = mach_picinit; 966 clkinitf = mach_clkinit; 967 psm_get_clockirq = pops->psm_get_clockirq; 968 969 /* register the interrupt setup code */ 970 slvltovect = mach_softlvl_to_vect; 971 addspl = pops->psm_addspl; 972 delspl = pops->psm_delspl; 973 974 if (pops->psm_translate_irq) 975 psm_translate_irq = pops->psm_translate_irq; 976 if (pops->psm_intr_ops) 977 psm_intr_ops = pops->psm_intr_ops; 978 979 #if defined(PSMI_1_2) || defined(PSMI_1_3) || defined(PSMI_1_4) 980 /* 981 * Time-of-day functionality now handled in TOD modules. 982 * (Warn about PSM modules that think that we're going to use 983 * their ops vectors.) 984 */ 985 if (pops->psm_tod_get) 986 cmn_err(CE_WARN, "obsolete psm_tod_get op %p", 987 (void *)pops->psm_tod_get); 988 989 if (pops->psm_tod_set) 990 cmn_err(CE_WARN, "obsolete psm_tod_set op %p", 991 (void *)pops->psm_tod_set); 992 #endif 993 994 if (pops->psm_notify_error) { 995 psm_notify_error = mach_notify_error; 996 notify_error = pops->psm_notify_error; 997 } 998 999 (*pops->psm_softinit)(); 1000 1001 /* 1002 * Initialize the dispatcher's function hooks to enable CPU halting 1003 * when idle. Set both the deep-idle and non-deep-idle hooks. 1004 * 1005 * Assume we can use power saving deep-idle loop cpu_idle_adaptive. 1006 * Platform deep-idle driver will reset our idle loop to 1007 * non_deep_idle_cpu if power saving deep-idle feature is not available. 1008 * 1009 * Do not use monitor/mwait if idle_cpu_use_hlt is not set(spin idle) 1010 * or idle_cpu_prefer_mwait is not set. 1011 * Allocate monitor/mwait buffer for cpu0. 1012 */ 1013 #ifndef __xpv 1014 non_deep_idle_disp_enq_thread = disp_enq_thread; 1015 #endif 1016 if (idle_cpu_use_hlt) { 1017 idle_cpu = cpu_idle_adaptive; 1018 CPU->cpu_m.mcpu_idle_cpu = cpu_idle; 1019 #ifndef __xpv 1020 if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait) { 1021 CPU->cpu_m.mcpu_mwait = cpuid_mwait_alloc(CPU); 1022 /* 1023 * Protect ourself from insane mwait size. 1024 */ 1025 if (CPU->cpu_m.mcpu_mwait == NULL) { 1026 #ifdef DEBUG 1027 cmn_err(CE_NOTE, "Using hlt idle. Cannot " 1028 "handle cpu 0 mwait size."); 1029 #endif 1030 idle_cpu_prefer_mwait = 0; 1031 CPU->cpu_m.mcpu_idle_cpu = cpu_idle; 1032 } else { 1033 CPU->cpu_m.mcpu_idle_cpu = cpu_idle_mwait; 1034 } 1035 } else { 1036 CPU->cpu_m.mcpu_idle_cpu = cpu_idle; 1037 } 1038 non_deep_idle_cpu = CPU->cpu_m.mcpu_idle_cpu; 1039 1040 /* 1041 * Disable power saving deep idle loop? 1042 */ 1043 if (idle_cpu_no_deep_c) { 1044 idle_cpu = non_deep_idle_cpu; 1045 } 1046 #endif 1047 } 1048 1049 mach_smpinit(); 1050 } 1051 1052 static void 1053 mach_smpinit(void) 1054 { 1055 struct psm_ops *pops; 1056 processorid_t cpu_id; 1057 int cnt; 1058 cpuset_t cpumask; 1059 1060 pops = mach_set[0]; 1061 CPUSET_ZERO(cpumask); 1062 1063 cpu_id = -1; 1064 cpu_id = (*pops->psm_get_next_processorid)(cpu_id); 1065 /* 1066 * Only add boot_ncpus CPUs to mp_cpus. Other CPUs will be handled 1067 * by CPU DR driver at runtime. 1068 */ 1069 for (cnt = 0; cpu_id != -1 && cnt < boot_ncpus; cnt++) { 1070 CPUSET_ADD(cpumask, cpu_id); 1071 cpu_id = (*pops->psm_get_next_processorid)(cpu_id); 1072 } 1073 1074 mp_cpus = cpumask; 1075 1076 /* MP related routines */ 1077 ap_mlsetup = pops->psm_post_cpu_start; 1078 send_dirintf = pops->psm_send_ipi; 1079 1080 /* optional MP related routines */ 1081 if (pops->psm_shutdown) 1082 psm_shutdownf = pops->psm_shutdown; 1083 if (pops->psm_preshutdown) 1084 psm_preshutdownf = pops->psm_preshutdown; 1085 if (pops->psm_notify_func) 1086 psm_notifyf = pops->psm_notify_func; 1087 if (pops->psm_set_idlecpu) 1088 psm_set_idle_cpuf = pops->psm_set_idlecpu; 1089 if (pops->psm_unset_idlecpu) 1090 psm_unset_idle_cpuf = pops->psm_unset_idlecpu; 1091 1092 psm_clkinit = pops->psm_clkinit; 1093 1094 if (pops->psm_timer_reprogram) 1095 psm_timer_reprogram = pops->psm_timer_reprogram; 1096 1097 if (pops->psm_timer_enable) 1098 psm_timer_enable = pops->psm_timer_enable; 1099 1100 if (pops->psm_timer_disable) 1101 psm_timer_disable = pops->psm_timer_disable; 1102 1103 if (pops->psm_post_cyclic_setup) 1104 psm_post_cyclic_setup = pops->psm_post_cyclic_setup; 1105 1106 if (pops->psm_state) 1107 psm_state = pops->psm_state; 1108 1109 /* 1110 * Set these vectors here so they can be used by Suspend/Resume 1111 * on UP machines. 1112 */ 1113 if (pops->psm_disable_intr) 1114 psm_disable_intr = pops->psm_disable_intr; 1115 if (pops->psm_enable_intr) 1116 psm_enable_intr = pops->psm_enable_intr; 1117 1118 /* check for multiple CPUs */ 1119 if (cnt < 2 && plat_dr_support_cpu() == B_FALSE) 1120 return; 1121 1122 /* check for MP platforms */ 1123 if (pops->psm_cpu_start == NULL) 1124 return; 1125 1126 /* 1127 * Set the dispatcher hook to enable cpu "wake up" 1128 * when a thread becomes runnable. 1129 */ 1130 if (idle_cpu_use_hlt) { 1131 disp_enq_thread = cpu_wakeup; 1132 #ifndef __xpv 1133 if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait) 1134 disp_enq_thread = cpu_wakeup_mwait; 1135 non_deep_idle_disp_enq_thread = disp_enq_thread; 1136 #endif 1137 } 1138 1139 psm_get_ipivect = pops->psm_get_ipivect; 1140 1141 (void) add_avintr((void *)NULL, XC_HI_PIL, xc_serv, "xc_intr", 1142 (*pops->psm_get_ipivect)(XC_HI_PIL, PSM_INTR_IPI_HI), 1143 NULL, NULL, NULL, NULL); 1144 1145 (void) (*pops->psm_get_ipivect)(XC_CPUPOKE_PIL, PSM_INTR_POKE); 1146 } 1147 1148 static void 1149 mach_picinit() 1150 { 1151 struct psm_ops *pops; 1152 1153 pops = mach_set[0]; 1154 1155 /* register the interrupt handlers */ 1156 setlvl = pops->psm_intr_enter; 1157 setlvlx = pops->psm_intr_exit; 1158 1159 /* initialize the interrupt hardware */ 1160 (*pops->psm_picinit)(); 1161 1162 /* set interrupt mask for current ipl */ 1163 setspl = pops->psm_setspl; 1164 cli(); 1165 setspl(CPU->cpu_pri); 1166 } 1167 1168 uint_t cpu_freq; /* MHz */ 1169 uint64_t cpu_freq_hz; /* measured (in hertz) */ 1170 1171 #define MEGA_HZ 1000000 1172 1173 #ifdef __xpv 1174 1175 int xpv_cpufreq_workaround = 1; 1176 int xpv_cpufreq_verbose = 0; 1177 1178 #else /* __xpv */ 1179 1180 static uint64_t 1181 mach_calchz(uint32_t pit_counter, uint64_t *processor_clks) 1182 { 1183 uint64_t cpu_hz; 1184 1185 if ((pit_counter == 0) || (*processor_clks == 0) || 1186 (*processor_clks > (((uint64_t)-1) / PIT_HZ))) 1187 return (0); 1188 1189 cpu_hz = ((uint64_t)PIT_HZ * *processor_clks) / pit_counter; 1190 1191 return (cpu_hz); 1192 } 1193 1194 #endif /* __xpv */ 1195 1196 static uint64_t 1197 mach_getcpufreq(void) 1198 { 1199 #if defined(__xpv) 1200 vcpu_time_info_t *vti = &CPU->cpu_m.mcpu_vcpu_info->time; 1201 uint64_t cpu_hz; 1202 1203 /* 1204 * During dom0 bringup, it was noted that on at least one older 1205 * Intel HT machine, the hypervisor initially gives a tsc_to_system_mul 1206 * value that is quite wrong (the 3.06GHz clock was reported 1207 * as 4.77GHz) 1208 * 1209 * The curious thing is, that if you stop the kernel at entry, 1210 * breakpoint here and inspect the value with kmdb, the value 1211 * is correct - but if you don't stop and simply enable the 1212 * printf statement (below), you can see the bad value printed 1213 * here. Almost as if something kmdb did caused the hypervisor to 1214 * figure it out correctly. And, note that the hypervisor 1215 * eventually -does- figure it out correctly ... if you look at 1216 * the field later in the life of dom0, it is correct. 1217 * 1218 * For now, on dom0, we employ a slightly cheesy workaround of 1219 * using the DOM0_PHYSINFO hypercall. 1220 */ 1221 if (DOMAIN_IS_INITDOMAIN(xen_info) && xpv_cpufreq_workaround) { 1222 cpu_hz = 1000 * xpv_cpu_khz(); 1223 } else { 1224 cpu_hz = (UINT64_C(1000000000) << 32) / vti->tsc_to_system_mul; 1225 1226 if (vti->tsc_shift < 0) 1227 cpu_hz <<= -vti->tsc_shift; 1228 else 1229 cpu_hz >>= vti->tsc_shift; 1230 } 1231 1232 if (xpv_cpufreq_verbose) 1233 printf("mach_getcpufreq: system_mul 0x%x, shift %d, " 1234 "cpu_hz %" PRId64 "Hz\n", 1235 vti->tsc_to_system_mul, vti->tsc_shift, cpu_hz); 1236 1237 return (cpu_hz); 1238 #else /* __xpv */ 1239 uint32_t pit_counter; 1240 uint64_t processor_clks; 1241 1242 if (x86_feature & X86_TSC) { 1243 /* 1244 * We have a TSC. freq_tsc() knows how to measure the number 1245 * of clock cycles sampled against the PIT. 1246 */ 1247 ulong_t flags = clear_int_flag(); 1248 processor_clks = freq_tsc(&pit_counter); 1249 restore_int_flag(flags); 1250 return (mach_calchz(pit_counter, &processor_clks)); 1251 } else if (x86_vendor == X86_VENDOR_Cyrix || x86_type == X86_TYPE_P5) { 1252 #if defined(__amd64) 1253 panic("mach_getcpufreq: no TSC!"); 1254 #elif defined(__i386) 1255 /* 1256 * We are a Cyrix based on a 6x86 core or an Intel Pentium 1257 * for which freq_notsc() knows how to measure the number of 1258 * elapsed clock cycles sampled against the PIT 1259 */ 1260 ulong_t flags = clear_int_flag(); 1261 processor_clks = freq_notsc(&pit_counter); 1262 restore_int_flag(flags); 1263 return (mach_calchz(pit_counter, &processor_clks)); 1264 #endif /* __i386 */ 1265 } 1266 1267 /* We do not know how to calculate cpu frequency for this cpu. */ 1268 return (0); 1269 #endif /* __xpv */ 1270 } 1271 1272 /* 1273 * If the clock speed of a cpu is found to be reported incorrectly, do not add 1274 * to this array, instead improve the accuracy of the algorithm that determines 1275 * the clock speed of the processor or extend the implementation to support the 1276 * vendor as appropriate. This is here only to support adjusting the speed on 1277 * older slower processors that mach_fixcpufreq() would not be able to account 1278 * for otherwise. 1279 */ 1280 static int x86_cpu_freq[] = { 60, 75, 80, 90, 120, 160, 166, 175, 180, 233 }; 1281 1282 /* 1283 * On fast processors the clock frequency that is measured may be off by 1284 * a few MHz from the value printed on the part. This is a combination of 1285 * the factors that for such fast parts being off by this much is within 1286 * the tolerances for manufacture and because of the difficulties in the 1287 * measurement that can lead to small error. This function uses some 1288 * heuristics in order to tweak the value that was measured to match what 1289 * is most likely printed on the part. 1290 * 1291 * Some examples: 1292 * AMD Athlon 1000 mhz measured as 998 mhz 1293 * Intel Pentium III Xeon 733 mhz measured as 731 mhz 1294 * Intel Pentium IV 1500 mhz measured as 1495mhz 1295 * 1296 * If in the future this function is no longer sufficient to correct 1297 * for the error in the measurement, then the algorithm used to perform 1298 * the measurement will have to be improved in order to increase accuracy 1299 * rather than adding horrible and questionable kludges here. 1300 * 1301 * This is called after the cyclics subsystem because of the potential 1302 * that the heuristics within may give a worse estimate of the clock 1303 * frequency than the value that was measured. 1304 */ 1305 static void 1306 mach_fixcpufreq(void) 1307 { 1308 uint32_t freq, mul, near66, delta66, near50, delta50, fixed, delta, i; 1309 1310 freq = (uint32_t)cpu_freq; 1311 1312 /* 1313 * Find the nearest integer multiple of 200/3 (about 66) MHz to the 1314 * measured speed taking into account that the 667 MHz parts were 1315 * the first to round-up. 1316 */ 1317 mul = (uint32_t)((3 * (uint64_t)freq + 100) / 200); 1318 near66 = (uint32_t)((200 * (uint64_t)mul + ((mul >= 10) ? 1 : 0)) / 3); 1319 delta66 = (near66 > freq) ? (near66 - freq) : (freq - near66); 1320 1321 /* Find the nearest integer multiple of 50 MHz to the measured speed */ 1322 mul = (freq + 25) / 50; 1323 near50 = mul * 50; 1324 delta50 = (near50 > freq) ? (near50 - freq) : (freq - near50); 1325 1326 /* Find the closer of the two */ 1327 if (delta66 < delta50) { 1328 fixed = near66; 1329 delta = delta66; 1330 } else { 1331 fixed = near50; 1332 delta = delta50; 1333 } 1334 1335 if (fixed > INT_MAX) 1336 return; 1337 1338 /* 1339 * Some older parts have a core clock frequency that is not an 1340 * integral multiple of 50 or 66 MHz. Check if one of the old 1341 * clock frequencies is closer to the measured value than any 1342 * of the integral multiples of 50 an 66, and if so set fixed 1343 * and delta appropriately to represent the closest value. 1344 */ 1345 i = sizeof (x86_cpu_freq) / sizeof (int); 1346 while (i > 0) { 1347 i--; 1348 1349 if (x86_cpu_freq[i] <= freq) { 1350 mul = freq - x86_cpu_freq[i]; 1351 1352 if (mul < delta) { 1353 fixed = x86_cpu_freq[i]; 1354 delta = mul; 1355 } 1356 1357 break; 1358 } 1359 1360 mul = x86_cpu_freq[i] - freq; 1361 1362 if (mul < delta) { 1363 fixed = x86_cpu_freq[i]; 1364 delta = mul; 1365 } 1366 } 1367 1368 /* 1369 * Set a reasonable maximum for how much to correct the measured 1370 * result by. This check is here to prevent the adjustment made 1371 * by this function from being more harm than good. It is entirely 1372 * possible that in the future parts will be made that are not 1373 * integral multiples of 66 or 50 in clock frequency or that 1374 * someone may overclock a part to some odd frequency. If the 1375 * measured value is farther from the corrected value than 1376 * allowed, then assume the corrected value is in error and use 1377 * the measured value. 1378 */ 1379 if (6 < delta) 1380 return; 1381 1382 cpu_freq = (int)fixed; 1383 } 1384 1385 1386 static int 1387 machhztomhz(uint64_t cpu_freq_hz) 1388 { 1389 uint64_t cpu_mhz; 1390 1391 /* Round to nearest MHZ */ 1392 cpu_mhz = (cpu_freq_hz + (MEGA_HZ / 2)) / MEGA_HZ; 1393 1394 if (cpu_mhz > INT_MAX) 1395 return (0); 1396 1397 return ((int)cpu_mhz); 1398 1399 } 1400 1401 1402 static int 1403 mach_clkinit(int preferred_mode, int *set_mode) 1404 { 1405 struct psm_ops *pops; 1406 int resolution; 1407 1408 pops = mach_set[0]; 1409 1410 cpu_freq_hz = mach_getcpufreq(); 1411 1412 cpu_freq = machhztomhz(cpu_freq_hz); 1413 1414 if (!(x86_feature & X86_TSC) || (cpu_freq == 0)) 1415 tsc_gethrtime_enable = 0; 1416 1417 #ifndef __xpv 1418 if (tsc_gethrtime_enable) { 1419 tsc_hrtimeinit(cpu_freq_hz); 1420 } else 1421 #endif 1422 { 1423 if (pops->psm_hrtimeinit) 1424 (*pops->psm_hrtimeinit)(); 1425 gethrtimef = pops->psm_gethrtime; 1426 gethrtimeunscaledf = gethrtimef; 1427 /* scalehrtimef will remain dummy */ 1428 } 1429 1430 mach_fixcpufreq(); 1431 1432 if (mach_ver[0] >= PSM_INFO_VER01_3) { 1433 if (preferred_mode == TIMER_ONESHOT) { 1434 1435 resolution = (*pops->psm_clkinit)(0); 1436 if (resolution != 0) { 1437 *set_mode = TIMER_ONESHOT; 1438 return (resolution); 1439 } 1440 } 1441 1442 /* 1443 * either periodic mode was requested or could not set to 1444 * one-shot mode 1445 */ 1446 resolution = (*pops->psm_clkinit)(hz); 1447 /* 1448 * psm should be able to do periodic, so we do not check 1449 * for return value of psm_clkinit here. 1450 */ 1451 *set_mode = TIMER_PERIODIC; 1452 return (resolution); 1453 } else { 1454 /* 1455 * PSMI interface prior to PSMI_3 does not define a return 1456 * value for psm_clkinit, so the return value is ignored. 1457 */ 1458 (void) (*pops->psm_clkinit)(hz); 1459 *set_mode = TIMER_PERIODIC; 1460 return (nsec_per_tick); 1461 } 1462 } 1463 1464 1465 /*ARGSUSED*/ 1466 static int 1467 mach_softlvl_to_vect(int ipl) 1468 { 1469 setsoftint = av_set_softint_pending; 1470 kdisetsoftint = kdi_av_set_softint_pending; 1471 1472 return (PSM_SV_SOFTWARE); 1473 } 1474 1475 #ifdef DEBUG 1476 /* 1477 * This is here to allow us to simulate cpus that refuse to start. 1478 */ 1479 cpuset_t cpufailset; 1480 #endif 1481 1482 int 1483 mach_cpu_start(struct cpu *cp, void *ctx) 1484 { 1485 struct psm_ops *pops = mach_set[0]; 1486 processorid_t id = cp->cpu_id; 1487 1488 #ifdef DEBUG 1489 if (CPU_IN_SET(cpufailset, id)) 1490 return (0); 1491 #endif 1492 return ((*pops->psm_cpu_start)(id, ctx)); 1493 } 1494 1495 int 1496 mach_cpuid_start(processorid_t id, void *ctx) 1497 { 1498 struct psm_ops *pops = mach_set[0]; 1499 1500 #ifdef DEBUG 1501 if (CPU_IN_SET(cpufailset, id)) 1502 return (0); 1503 #endif 1504 return ((*pops->psm_cpu_start)(id, ctx)); 1505 } 1506 1507 int 1508 mach_cpu_stop(cpu_t *cp, void *ctx) 1509 { 1510 struct psm_ops *pops = mach_set[0]; 1511 psm_cpu_request_t request; 1512 1513 if (pops->psm_cpu_ops == NULL) { 1514 return (ENOTSUP); 1515 } 1516 1517 ASSERT(cp->cpu_id != -1); 1518 request.pcr_cmd = PSM_CPU_STOP; 1519 request.req.cpu_stop.cpuid = cp->cpu_id; 1520 request.req.cpu_stop.ctx = ctx; 1521 1522 return ((*pops->psm_cpu_ops)(&request)); 1523 } 1524 1525 int 1526 mach_cpu_add(mach_cpu_add_arg_t *argp, processorid_t *cpuidp) 1527 { 1528 int rc; 1529 struct psm_ops *pops = mach_set[0]; 1530 psm_cpu_request_t request; 1531 1532 if (pops->psm_cpu_ops == NULL) { 1533 return (ENOTSUP); 1534 } 1535 1536 request.pcr_cmd = PSM_CPU_ADD; 1537 request.req.cpu_add.argp = argp; 1538 request.req.cpu_add.cpuid = -1; 1539 rc = (*pops->psm_cpu_ops)(&request); 1540 if (rc == 0) { 1541 ASSERT(request.req.cpu_add.cpuid != -1); 1542 *cpuidp = request.req.cpu_add.cpuid; 1543 } 1544 1545 return (rc); 1546 } 1547 1548 int 1549 mach_cpu_remove(processorid_t cpuid) 1550 { 1551 struct psm_ops *pops = mach_set[0]; 1552 psm_cpu_request_t request; 1553 1554 if (pops->psm_cpu_ops == NULL) { 1555 return (ENOTSUP); 1556 } 1557 1558 request.pcr_cmd = PSM_CPU_REMOVE; 1559 request.req.cpu_remove.cpuid = cpuid; 1560 1561 return ((*pops->psm_cpu_ops)(&request)); 1562 } 1563 1564 /* 1565 * Default handler to create device node for CPU. 1566 * One reference count will be held on created device node. 1567 */ 1568 static int 1569 mach_cpu_create_devinfo(cpu_t *cp, dev_info_t **dipp) 1570 { 1571 int rv, circ; 1572 dev_info_t *dip; 1573 static kmutex_t cpu_node_lock; 1574 static dev_info_t *cpu_nex_devi = NULL; 1575 1576 ASSERT(cp != NULL); 1577 ASSERT(dipp != NULL); 1578 *dipp = NULL; 1579 1580 if (cpu_nex_devi == NULL) { 1581 mutex_enter(&cpu_node_lock); 1582 /* First check whether cpus exists. */ 1583 cpu_nex_devi = ddi_find_devinfo("cpus", -1, 0); 1584 /* Create cpus if it doesn't exist. */ 1585 if (cpu_nex_devi == NULL) { 1586 ndi_devi_enter(ddi_root_node(), &circ); 1587 rv = ndi_devi_alloc(ddi_root_node(), "cpus", 1588 (pnode_t)DEVI_SID_NODEID, &dip); 1589 if (rv != NDI_SUCCESS) { 1590 mutex_exit(&cpu_node_lock); 1591 cmn_err(CE_CONT, 1592 "?failed to create cpu nexus device.\n"); 1593 return (PSM_FAILURE); 1594 } 1595 ASSERT(dip != NULL); 1596 (void) ndi_devi_online(dip, 0); 1597 ndi_devi_exit(ddi_root_node(), circ); 1598 cpu_nex_devi = dip; 1599 } 1600 mutex_exit(&cpu_node_lock); 1601 } 1602 1603 /* 1604 * create a child node for cpu identified as 'cpu_id' 1605 */ 1606 ndi_devi_enter(cpu_nex_devi, &circ); 1607 dip = ddi_add_child(cpu_nex_devi, "cpu", DEVI_SID_NODEID, -1); 1608 if (dip == NULL) { 1609 cmn_err(CE_CONT, 1610 "?failed to create device node for cpu%d.\n", cp->cpu_id); 1611 rv = PSM_FAILURE; 1612 } else { 1613 *dipp = dip; 1614 (void) ndi_hold_devi(dip); 1615 rv = PSM_SUCCESS; 1616 } 1617 ndi_devi_exit(cpu_nex_devi, circ); 1618 1619 return (rv); 1620 } 1621 1622 /* 1623 * Create cpu device node in device tree and online it. 1624 * Return created dip with reference count held if requested. 1625 */ 1626 int 1627 mach_cpu_create_device_node(struct cpu *cp, dev_info_t **dipp) 1628 { 1629 int rv; 1630 dev_info_t *dip = NULL; 1631 1632 ASSERT(psm_cpu_create_devinfo != NULL); 1633 rv = psm_cpu_create_devinfo(cp, &dip); 1634 if (rv == PSM_SUCCESS) { 1635 cpuid_set_cpu_properties(dip, cp->cpu_id, cp->cpu_m.mcpu_cpi); 1636 /* Recursively attach driver for parent nexus device. */ 1637 if (i_ddi_attach_node_hierarchy(ddi_get_parent(dip)) == 1638 DDI_SUCCESS) { 1639 /* Configure cpu itself and descendants. */ 1640 (void) ndi_devi_online(dip, 1641 NDI_ONLINE_ATTACH | NDI_CONFIG); 1642 } 1643 if (dipp != NULL) { 1644 *dipp = dip; 1645 } else { 1646 (void) ndi_rele_devi(dip); 1647 } 1648 } 1649 1650 return (rv); 1651 } 1652 1653 /* 1654 * The dipp contains one of following values on return: 1655 * - NULL if no device node found 1656 * - pointer to device node if found 1657 */ 1658 int 1659 mach_cpu_get_device_node(struct cpu *cp, dev_info_t **dipp) 1660 { 1661 *dipp = NULL; 1662 if (psm_cpu_get_devinfo != NULL) { 1663 if (psm_cpu_get_devinfo(cp, dipp) == PSM_SUCCESS) { 1664 return (PSM_SUCCESS); 1665 } 1666 } 1667 1668 return (PSM_FAILURE); 1669 } 1670 1671 /*ARGSUSED*/ 1672 static int 1673 mach_translate_irq(dev_info_t *dip, int irqno) 1674 { 1675 return (irqno); /* default to NO translation */ 1676 } 1677 1678 static void 1679 mach_notify_error(int level, char *errmsg) 1680 { 1681 /* 1682 * SL_FATAL is pass in once panicstr is set, deliver it 1683 * as CE_PANIC. Also, translate SL_ codes back to CE_ 1684 * codes for the psmi handler 1685 */ 1686 if (level & SL_FATAL) 1687 (*notify_error)(CE_PANIC, errmsg); 1688 else if (level & SL_WARN) 1689 (*notify_error)(CE_WARN, errmsg); 1690 else if (level & SL_NOTE) 1691 (*notify_error)(CE_NOTE, errmsg); 1692 else if (level & SL_CONSOLE) 1693 (*notify_error)(CE_CONT, errmsg); 1694 } 1695 1696 /* 1697 * It provides the default basic intr_ops interface for the new DDI 1698 * interrupt framework if the PSM doesn't have one. 1699 * 1700 * Input: 1701 * dip - pointer to the dev_info structure of the requested device 1702 * hdlp - pointer to the internal interrupt handle structure for the 1703 * requested interrupt 1704 * intr_op - opcode for this call 1705 * result - pointer to the integer that will hold the result to be 1706 * passed back if return value is PSM_SUCCESS 1707 * 1708 * Output: 1709 * return value is either PSM_SUCCESS or PSM_FAILURE 1710 */ 1711 static int 1712 mach_intr_ops(dev_info_t *dip, ddi_intr_handle_impl_t *hdlp, 1713 psm_intr_op_t intr_op, int *result) 1714 { 1715 struct intrspec *ispec; 1716 1717 switch (intr_op) { 1718 case PSM_INTR_OP_CHECK_MSI: 1719 *result = hdlp->ih_type & ~(DDI_INTR_TYPE_MSI | 1720 DDI_INTR_TYPE_MSIX); 1721 break; 1722 case PSM_INTR_OP_ALLOC_VECTORS: 1723 if (hdlp->ih_type == DDI_INTR_TYPE_FIXED) 1724 *result = 1; 1725 else 1726 *result = 0; 1727 break; 1728 case PSM_INTR_OP_FREE_VECTORS: 1729 break; 1730 case PSM_INTR_OP_NAVAIL_VECTORS: 1731 if (hdlp->ih_type == DDI_INTR_TYPE_FIXED) 1732 *result = 1; 1733 else 1734 *result = 0; 1735 break; 1736 case PSM_INTR_OP_XLATE_VECTOR: 1737 ispec = ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp; 1738 *result = psm_translate_irq(dip, ispec->intrspec_vec); 1739 break; 1740 case PSM_INTR_OP_GET_CAP: 1741 *result = 0; 1742 break; 1743 case PSM_INTR_OP_GET_PENDING: 1744 case PSM_INTR_OP_CLEAR_MASK: 1745 case PSM_INTR_OP_SET_MASK: 1746 case PSM_INTR_OP_GET_SHARED: 1747 case PSM_INTR_OP_SET_PRI: 1748 case PSM_INTR_OP_SET_CAP: 1749 case PSM_INTR_OP_SET_CPU: 1750 case PSM_INTR_OP_GET_INTR: 1751 default: 1752 return (PSM_FAILURE); 1753 } 1754 return (PSM_SUCCESS); 1755 } 1756 /* 1757 * Return 1 if CMT load balancing policies should be 1758 * implemented across instances of the specified hardware 1759 * sharing relationship. 1760 */ 1761 int 1762 pg_cmt_load_bal_hw(pghw_type_t hw) 1763 { 1764 if (hw == PGHW_IPIPE || 1765 hw == PGHW_FPU || 1766 hw == PGHW_PROCNODE || 1767 hw == PGHW_CHIP) 1768 return (1); 1769 else 1770 return (0); 1771 } 1772 /* 1773 * Return 1 if thread affinity polices should be implemented 1774 * for instances of the specifed hardware sharing relationship. 1775 */ 1776 int 1777 pg_cmt_affinity_hw(pghw_type_t hw) 1778 { 1779 if (hw == PGHW_CACHE) 1780 return (1); 1781 else 1782 return (0); 1783 } 1784 1785 /* 1786 * Return number of counter events requested to measure hardware capacity and 1787 * utilization and setup CPC requests for specified CPU as needed 1788 * 1789 * May return 0 when platform or processor specific code knows that no CPC 1790 * events should be programmed on this CPU or -1 when platform or processor 1791 * specific code doesn't know which counter events are best to use and common 1792 * code should decide for itself 1793 */ 1794 int 1795 /* LINTED E_FUNC_ARG_UNUSED */ 1796 cu_plat_cpc_init(cpu_t *cp, kcpc_request_list_t *reqs, int nreqs) 1797 { 1798 const char *impl_name; 1799 1800 /* 1801 * Return error if pcbe_ops not set 1802 */ 1803 if (pcbe_ops == NULL) 1804 return (-1); 1805 1806 /* 1807 * Return that no CPC events should be programmed on hyperthreaded 1808 * Pentium 4 and return error for all other x86 processors to tell 1809 * common code to decide what counter events to program on those CPUs 1810 * for measuring hardware capacity and utilization 1811 */ 1812 impl_name = pcbe_ops->pcbe_impl_name(); 1813 if (impl_name != NULL && strcmp(impl_name, PCBE_IMPL_NAME_P4HT) == 0) 1814 return (0); 1815 else 1816 return (-1); 1817 } 1818