30#include "opt_cpu.h" 31#include "opt_kstack_pages.h" 32#include "opt_swtch.h" 33 34#ifdef SMP 35#include <machine/smptests.h> 36#else 37#error 38#endif 39 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/bus.h> 43#include <sys/cons.h> /* cngetc() */ 44#ifdef GPROF 45#include <sys/gmon.h> 46#endif 47#include <sys/kernel.h> 48#include <sys/ktr.h> 49#include <sys/lock.h> 50#include <sys/malloc.h> 51#include <sys/memrange.h> 52#include <sys/mutex.h> 53#include <sys/pcpu.h> 54#include <sys/proc.h> 55#include <sys/smp.h> 56#include <sys/sysctl.h> 57#include <sys/user.h> 58 59#include <vm/vm.h> 60#include <vm/vm_param.h> 61#include <vm/pmap.h> 62#include <vm/vm_kern.h> 63#include <vm/vm_extern.h> 64#include <vm/vm_map.h> 65 66#include <machine/apic.h> 67#include <machine/atomic.h> 68#include <machine/clock.h> 69#include <machine/cpu.h> 70#include <machine/cpufunc.h> 71#include <machine/mpapic.h> 72#include <machine/psl.h> 73#include <machine/segments.h> 74#include <machine/smp.h> 75#include <machine/smptests.h> /** TEST_DEFAULT_CONFIG, TEST_TEST1 */ 76#include <machine/tss.h> 77#include <machine/specialreg.h> 78#include <machine/privatespace.h> 79 80#if defined(APIC_IO) 81#include <machine/md_var.h> /* setidt() */ 82#include <i386/isa/icu.h> /* IPIs */ 83#include <i386/isa/intr_machdep.h> /* IPIs */ 84#endif /* APIC_IO */ 85 86#if defined(TEST_DEFAULT_CONFIG) 87#define MPFPS_MPFB1 TEST_DEFAULT_CONFIG 88#else 89#define MPFPS_MPFB1 mpfps->mpfb1 90#endif /* TEST_DEFAULT_CONFIG */ 91 92#define WARMBOOT_TARGET 0 93#define WARMBOOT_OFF (KERNBASE + 0x0467) 94#define WARMBOOT_SEG (KERNBASE + 0x0469) 95 96#ifdef PC98 97#define BIOS_BASE (0xe8000) 98#define BIOS_SIZE (0x18000) 99#else 100#define BIOS_BASE (0xf0000) 101#define BIOS_SIZE (0x10000) 102#endif 103#define BIOS_COUNT (BIOS_SIZE/4) 104 105#define CMOS_REG (0x70) 106#define CMOS_DATA (0x71) 107#define BIOS_RESET (0x0f) 108#define BIOS_WARM (0x0a) 109 110#define PROCENTRY_FLAG_EN 0x01 111#define PROCENTRY_FLAG_BP 0x02 112#define IOAPICENTRY_FLAG_EN 0x01 113 114 115/* MP Floating Pointer Structure */ 116typedef struct MPFPS { 117 char signature[4]; 118 void *pap; 119 u_char length; 120 u_char spec_rev; 121 u_char checksum; 122 u_char mpfb1; 123 u_char mpfb2; 124 u_char mpfb3; 125 u_char mpfb4; 126 u_char mpfb5; 127} *mpfps_t; 128 129/* MP Configuration Table Header */ 130typedef struct MPCTH { 131 char signature[4]; 132 u_short base_table_length; 133 u_char spec_rev; 134 u_char checksum; 135 u_char oem_id[8]; 136 u_char product_id[12]; 137 void *oem_table_pointer; 138 u_short oem_table_size; 139 u_short entry_count; 140 void *apic_address; 141 u_short extended_table_length; 142 u_char extended_table_checksum; 143 u_char reserved; 144} *mpcth_t; 145 146 147typedef struct PROCENTRY { 148 u_char type; 149 u_char apic_id; 150 u_char apic_version; 151 u_char cpu_flags; 152 u_long cpu_signature; 153 u_long feature_flags; 154 u_long reserved1; 155 u_long reserved2; 156} *proc_entry_ptr; 157 158typedef struct BUSENTRY { 159 u_char type; 160 u_char bus_id; 161 char bus_type[6]; 162} *bus_entry_ptr; 163 164typedef struct IOAPICENTRY { 165 u_char type; 166 u_char apic_id; 167 u_char apic_version; 168 u_char apic_flags; 169 void *apic_address; 170} *io_apic_entry_ptr; 171 172typedef struct INTENTRY { 173 u_char type; 174 u_char int_type; 175 u_short int_flags; 176 u_char src_bus_id; 177 u_char src_bus_irq; 178 u_char dst_apic_id; 179 u_char dst_apic_int; 180} *int_entry_ptr; 181 182/* descriptions of MP basetable entries */ 183typedef struct BASETABLE_ENTRY { 184 u_char type; 185 u_char length; 186 char name[16]; 187} basetable_entry; 188 189/* 190 * this code MUST be enabled here and in mpboot.s. 191 * it follows the very early stages of AP boot by placing values in CMOS ram. 192 * it NORMALLY will never be needed and thus the primitive method for enabling. 193 * 194#define CHECK_POINTS 195 */ 196 197#if defined(CHECK_POINTS) && !defined(PC98) 198#define CHECK_READ(A) (outb(CMOS_REG, (A)), inb(CMOS_DATA)) 199#define CHECK_WRITE(A,D) (outb(CMOS_REG, (A)), outb(CMOS_DATA, (D))) 200 201#define CHECK_INIT(D); \ 202 CHECK_WRITE(0x34, (D)); \ 203 CHECK_WRITE(0x35, (D)); \ 204 CHECK_WRITE(0x36, (D)); \ 205 CHECK_WRITE(0x37, (D)); \ 206 CHECK_WRITE(0x38, (D)); \ 207 CHECK_WRITE(0x39, (D)); 208 209#define CHECK_PRINT(S); \ 210 printf("%s: %d, %d, %d, %d, %d, %d\n", \ 211 (S), \ 212 CHECK_READ(0x34), \ 213 CHECK_READ(0x35), \ 214 CHECK_READ(0x36), \ 215 CHECK_READ(0x37), \ 216 CHECK_READ(0x38), \ 217 CHECK_READ(0x39)); 218 219#else /* CHECK_POINTS */ 220 221#define CHECK_INIT(D) 222#define CHECK_PRINT(S) 223 224#endif /* CHECK_POINTS */ 225 226/* 227 * Values to send to the POST hardware. 228 */ 229#define MP_BOOTADDRESS_POST 0x10 230#define MP_PROBE_POST 0x11 231#define MPTABLE_PASS1_POST 0x12 232 233#define MP_START_POST 0x13 234#define MP_ENABLE_POST 0x14 235#define MPTABLE_PASS2_POST 0x15 236 237#define START_ALL_APS_POST 0x16 238#define INSTALL_AP_TRAMP_POST 0x17 239#define START_AP_POST 0x18 240 241#define MP_ANNOUNCE_POST 0x19 242 243static int need_hyperthreading_fixup; 244static u_int logical_cpus; 245static u_int logical_cpus_mask; 246 247/* used to hold the AP's until we are ready to release them */ 248static struct mtx ap_boot_mtx; 249 250/** XXX FIXME: where does this really belong, isa.h/isa.c perhaps? */ 251int current_postcode; 252 253/** XXX FIXME: what system files declare these??? */ 254extern struct region_descriptor r_gdt, r_idt; 255 256int bsp_apic_ready = 0; /* flags useability of BSP apic */ 257int mp_naps; /* # of Applications processors */ 258int mp_nbusses; /* # of busses */ 259int mp_napics; /* # of IO APICs */ 260int boot_cpu_id; /* designated BSP */ 261vm_offset_t cpu_apic_address; 262vm_offset_t io_apic_address[NAPICID]; /* NAPICID is more than enough */ 263extern int nkpt; 264 265u_int32_t cpu_apic_versions[MAXCPU]; 266u_int32_t *io_apic_versions; 267 268#ifdef APIC_INTR_REORDER 269struct { 270 volatile int *location; 271 int bit; 272} apic_isrbit_location[32]; 273#endif 274 275struct apic_intmapinfo int_to_apicintpin[APIC_INTMAPSIZE]; 276 277/* 278 * APIC ID logical/physical mapping structures. 279 * We oversize these to simplify boot-time config. 280 */ 281int cpu_num_to_apic_id[NAPICID]; 282int io_num_to_apic_id[NAPICID]; 283int apic_id_to_logical[NAPICID]; 284 285 286/* AP uses this during bootstrap. Do not staticize. */ 287char *bootSTK; 288static int bootAP; 289 290/* Hotwire a 0->4MB V==P mapping */ 291extern pt_entry_t *KPTphys; 292 293/* SMP page table page */ 294extern pt_entry_t *SMPpt; 295 296struct pcb stoppcbs[MAXCPU]; 297 298#ifdef APIC_IO 299/* Variables needed for SMP tlb shootdown. */ 300vm_offset_t smp_tlb_addr1; 301vm_offset_t smp_tlb_addr2; 302volatile int smp_tlb_wait; 303static struct mtx smp_tlb_mtx; 304#endif 305 306/* 307 * Local data and functions. 308 */ 309 310/* Set to 1 once we're ready to let the APs out of the pen. */ 311static volatile int aps_ready = 0; 312 313static int mp_capable; 314static u_int boot_address; 315static u_int base_memory; 316 317static int picmode; /* 0: virtual wire mode, 1: PIC mode */ 318static mpfps_t mpfps; 319static int search_for_sig(u_int32_t target, int count); 320static void mp_enable(u_int boot_addr); 321 322static void mptable_hyperthread_fixup(u_int id_mask); 323static void mptable_pass1(void); 324static int mptable_pass2(void); 325static void default_mp_table(int type); 326static void fix_mp_table(void); 327static void setup_apic_irq_mapping(void); 328static void init_locks(void); 329static int start_all_aps(u_int boot_addr); 330static void install_ap_tramp(u_int boot_addr); 331static int start_ap(int logicalCpu, u_int boot_addr); 332void ap_init(void); 333static int apic_int_is_bus_type(int intr, int bus_type); 334static void release_aps(void *dummy); 335 336/* 337 * initialize all the SMP locks 338 */ 339 340/* lock region used by kernel profiling */ 341int mcount_lock; 342 343#ifdef USE_COMLOCK 344/* locks com (tty) data/hardware accesses: a FASTINTR() */ 345struct mtx com_mtx; 346#endif /* USE_COMLOCK */ 347 348static void 349init_locks(void) 350{ 351 352#ifdef USE_COMLOCK 353 mtx_init(&com_mtx, "com", NULL, MTX_SPIN); 354#endif /* USE_COMLOCK */ 355#ifdef APIC_IO 356 mtx_init(&smp_tlb_mtx, "tlb", NULL, MTX_SPIN); 357#endif 358} 359 360/* 361 * Calculate usable address in base memory for AP trampoline code. 362 */ 363u_int 364mp_bootaddress(u_int basemem) 365{ 366 POSTCODE(MP_BOOTADDRESS_POST); 367 368 base_memory = basemem * 1024; /* convert to bytes */ 369 370 boot_address = base_memory & ~0xfff; /* round down to 4k boundary */ 371 if ((base_memory - boot_address) < bootMP_size) 372 boot_address -= 4096; /* not enough, lower by 4k */ 373 374 return boot_address; 375} 376 377 378/* 379 * Look for an Intel MP spec table (ie, SMP capable hardware). 380 */ 381void 382i386_mp_probe(void) 383{ 384 int x; 385 u_long segment; 386 u_int32_t target; 387 388 POSTCODE(MP_PROBE_POST); 389 390 /* see if EBDA exists */ 391 if ((segment = (u_long) * (u_short *) (KERNBASE + 0x40e)) != 0) { 392 /* search first 1K of EBDA */ 393 target = (u_int32_t) (segment << 4); 394 if ((x = search_for_sig(target, 1024 / 4)) >= 0) 395 goto found; 396 } else { 397 /* last 1K of base memory, effective 'top of base' passed in */ 398 target = (u_int32_t) (base_memory - 0x400); 399 if ((x = search_for_sig(target, 1024 / 4)) >= 0) 400 goto found; 401 } 402 403 /* search the BIOS */ 404 target = (u_int32_t) BIOS_BASE; 405 if ((x = search_for_sig(target, BIOS_COUNT)) >= 0) 406 goto found; 407 408 /* nothing found */ 409 mpfps = (mpfps_t)0; 410 mp_capable = 0; 411 return; 412 413found: 414 /* calculate needed resources */ 415 mpfps = (mpfps_t)x; 416 mptable_pass1(); 417 418 /* flag fact that we are running multiple processors */ 419 mp_capable = 1; 420} 421 422int 423cpu_mp_probe(void) 424{ 425 /* 426 * Record BSP in CPU map 427 * This is done here so that MBUF init code works correctly. 428 */ 429 all_cpus = 1; 430 431 return (mp_capable); 432} 433 434/* 435 * Initialize the SMP hardware and the APIC and start up the AP's. 436 */ 437void 438cpu_mp_start(void) 439{ 440 POSTCODE(MP_START_POST); 441 442 /* look for MP capable motherboard */ 443 if (mp_capable) 444 mp_enable(boot_address); 445 else 446 panic("MP hardware not found!"); 447 448 cpu_setregs(); 449} 450 451 452/* 453 * Print various information about the SMP system hardware and setup. 454 */ 455void 456cpu_mp_announce(void) 457{ 458 int x; 459 460 POSTCODE(MP_ANNOUNCE_POST); 461 462 printf(" cpu0 (BSP): apic id: %2d", CPU_TO_ID(0)); 463 printf(", version: 0x%08x", cpu_apic_versions[0]); 464 printf(", at 0x%08x\n", cpu_apic_address); 465 for (x = 1; x <= mp_naps; ++x) { 466 printf(" cpu%d (AP): apic id: %2d", x, CPU_TO_ID(x)); 467 printf(", version: 0x%08x", cpu_apic_versions[x]); 468 printf(", at 0x%08x\n", cpu_apic_address); 469 } 470 471#if defined(APIC_IO) 472 for (x = 0; x < mp_napics; ++x) { 473 printf(" io%d (APIC): apic id: %2d", x, IO_TO_ID(x)); 474 printf(", version: 0x%08x", io_apic_versions[x]); 475 printf(", at 0x%08x\n", io_apic_address[x]); 476 } 477#else 478 printf(" Warning: APIC I/O disabled\n"); 479#endif /* APIC_IO */ 480} 481 482/* 483 * AP cpu's call this to sync up protected mode. 484 */ 485void 486init_secondary(void) 487{ 488 int gsel_tss; 489 int x, myid = bootAP; 490 u_int cr0; 491 492 gdt_segs[GPRIV_SEL].ssd_base = (int) &SMP_prvspace[myid]; 493 gdt_segs[GPROC0_SEL].ssd_base = 494 (int) &SMP_prvspace[myid].pcpu.pc_common_tss; 495 SMP_prvspace[myid].pcpu.pc_prvspace = 496 &SMP_prvspace[myid].pcpu; 497 498 for (x = 0; x < NGDT; x++) { 499 ssdtosd(&gdt_segs[x], &gdt[myid * NGDT + x].sd); 500 } 501 502 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1; 503 r_gdt.rd_base = (int) &gdt[myid * NGDT]; 504 lgdt(&r_gdt); /* does magic intra-segment return */ 505 506 lidt(&r_idt); 507 508 lldt(_default_ldt); 509 PCPU_SET(currentldt, _default_ldt); 510 511 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL); 512 gdt[myid * NGDT + GPROC0_SEL].sd.sd_type = SDT_SYS386TSS; 513 PCPU_SET(common_tss.tss_esp0, 0); /* not used until after switch */ 514 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL)); 515 PCPU_SET(common_tss.tss_ioopt, (sizeof (struct i386tss)) << 16); 516 PCPU_SET(tss_gdt, &gdt[myid * NGDT + GPROC0_SEL].sd); 517 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt)); 518 ltr(gsel_tss); 519 520 /* 521 * Set to a known state: 522 * Set by mpboot.s: CR0_PG, CR0_PE 523 * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM 524 */ 525 cr0 = rcr0(); 526 cr0 &= ~(CR0_CD | CR0_NW | CR0_EM); 527 load_cr0(cr0); 528 529 pmap_set_opt(); 530} 531 532 533#if defined(APIC_IO) 534/* 535 * Final configuration of the BSP's local APIC: 536 * - disable 'pic mode'. 537 * - disable 'virtual wire mode'. 538 * - enable NMI. 539 */ 540void 541bsp_apic_configure(void) 542{ 543 u_char byte; 544 u_int32_t temp; 545 546 /* leave 'pic mode' if necessary */ 547 if (picmode) { 548 outb(0x22, 0x70); /* select IMCR */ 549 byte = inb(0x23); /* current contents */ 550 byte |= 0x01; /* mask external INTR */ 551 outb(0x23, byte); /* disconnect 8259s/NMI */ 552 } 553 554 /* mask lint0 (the 8259 'virtual wire' connection) */ 555 temp = lapic.lvt_lint0; 556 temp |= APIC_LVT_M; /* set the mask */ 557 lapic.lvt_lint0 = temp; 558 559 /* setup lint1 to handle NMI */ 560 temp = lapic.lvt_lint1; 561 temp &= ~APIC_LVT_M; /* clear the mask */ 562 lapic.lvt_lint1 = temp; 563 564 if (bootverbose) 565 apic_dump("bsp_apic_configure()"); 566} 567#endif /* APIC_IO */ 568 569 570/******************************************************************* 571 * local functions and data 572 */ 573 574/* 575 * start the SMP system 576 */ 577static void 578mp_enable(u_int boot_addr) 579{ 580 int x; 581#if defined(APIC_IO) 582 int apic; 583 u_int ux; 584#endif /* APIC_IO */ 585 586 POSTCODE(MP_ENABLE_POST); 587 588 /* turn on 4MB of V == P addressing so we can get to MP table */ 589 *(int *)PTD = PG_V | PG_RW | ((uintptr_t)(void *)KPTphys & PG_FRAME); 590 invltlb(); 591 592 /* examine the MP table for needed info, uses physical addresses */ 593 x = mptable_pass2(); 594 595 *(int *)PTD = 0; 596 invltlb(); 597 598 /* can't process default configs till the CPU APIC is pmapped */ 599 if (x) 600 default_mp_table(x); 601 602 /* post scan cleanup */ 603 fix_mp_table(); 604 setup_apic_irq_mapping(); 605 606#if defined(APIC_IO) 607 608 /* fill the LOGICAL io_apic_versions table */ 609 for (apic = 0; apic < mp_napics; ++apic) { 610 ux = io_apic_read(apic, IOAPIC_VER); 611 io_apic_versions[apic] = ux; 612 io_apic_set_id(apic, IO_TO_ID(apic)); 613 } 614 615 /* program each IO APIC in the system */ 616 for (apic = 0; apic < mp_napics; ++apic) 617 if (io_apic_setup(apic) < 0) 618 panic("IO APIC setup failure"); 619 620 /* install a 'Spurious INTerrupt' vector */ 621 setidt(XSPURIOUSINT_OFFSET, Xspuriousint, 622 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 623 624 /* install an inter-CPU IPI for TLB invalidation */ 625 setidt(XINVLTLB_OFFSET, Xinvltlb, 626 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 627 setidt(XINVLPG_OFFSET, Xinvlpg, 628 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 629 setidt(XINVLRNG_OFFSET, Xinvlrng, 630 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 631 632 /* install an inter-CPU IPI for forwarding hardclock() */ 633 setidt(XHARDCLOCK_OFFSET, Xhardclock, 634 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 635 636 /* install an inter-CPU IPI for forwarding statclock() */ 637 setidt(XSTATCLOCK_OFFSET, Xstatclock, 638 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 639 640#ifdef LAZY_SWITCH 641 /* install an inter-CPU IPI for lazy pmap release */ 642 setidt(XLAZYPMAP_OFFSET, Xlazypmap, 643 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 644#endif 645 646 /* install an inter-CPU IPI for all-CPU rendezvous */ 647 setidt(XRENDEZVOUS_OFFSET, Xrendezvous, 648 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 649 650 /* install an inter-CPU IPI for forcing an additional software trap */ 651 setidt(XCPUAST_OFFSET, Xcpuast, 652 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 653 654 /* install an inter-CPU IPI for CPU stop/restart */ 655 setidt(XCPUSTOP_OFFSET, Xcpustop, 656 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 657 658#if defined(TEST_TEST1) 659 /* install a "fake hardware INTerrupt" vector */ 660 setidt(XTEST1_OFFSET, Xtest1, 661 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 662#endif /** TEST_TEST1 */ 663 664#endif /* APIC_IO */ 665 666 /* initialize all SMP locks */ 667 init_locks(); 668 669 /* start each Application Processor */ 670 start_all_aps(boot_addr); 671} 672 673 674/* 675 * look for the MP spec signature 676 */ 677 678/* string defined by the Intel MP Spec as identifying the MP table */ 679#define MP_SIG 0x5f504d5f /* _MP_ */ 680#define NEXT(X) ((X) += 4) 681static int 682search_for_sig(u_int32_t target, int count) 683{ 684 int x; 685 u_int32_t *addr = (u_int32_t *) (KERNBASE + target); 686 687 for (x = 0; x < count; NEXT(x)) 688 if (addr[x] == MP_SIG) 689 /* make array index a byte index */ 690 return (target + (x * sizeof(u_int32_t))); 691 692 return -1; 693} 694 695 696static basetable_entry basetable_entry_types[] = 697{ 698 {0, 20, "Processor"}, 699 {1, 8, "Bus"}, 700 {2, 8, "I/O APIC"}, 701 {3, 8, "I/O INT"}, 702 {4, 8, "Local INT"} 703}; 704 705typedef struct BUSDATA { 706 u_char bus_id; 707 enum busTypes bus_type; 708} bus_datum; 709 710typedef struct INTDATA { 711 u_char int_type; 712 u_short int_flags; 713 u_char src_bus_id; 714 u_char src_bus_irq; 715 u_char dst_apic_id; 716 u_char dst_apic_int; 717 u_char int_vector; 718} io_int, local_int; 719 720typedef struct BUSTYPENAME { 721 u_char type; 722 char name[7]; 723} bus_type_name; 724 725static bus_type_name bus_type_table[] = 726{ 727 {CBUS, "CBUS"}, 728 {CBUSII, "CBUSII"}, 729 {EISA, "EISA"}, 730 {MCA, "MCA"}, 731 {UNKNOWN_BUSTYPE, "---"}, 732 {ISA, "ISA"}, 733 {MCA, "MCA"}, 734 {UNKNOWN_BUSTYPE, "---"}, 735 {UNKNOWN_BUSTYPE, "---"}, 736 {UNKNOWN_BUSTYPE, "---"}, 737 {UNKNOWN_BUSTYPE, "---"}, 738 {UNKNOWN_BUSTYPE, "---"}, 739 {PCI, "PCI"}, 740 {UNKNOWN_BUSTYPE, "---"}, 741 {UNKNOWN_BUSTYPE, "---"}, 742 {UNKNOWN_BUSTYPE, "---"}, 743 {UNKNOWN_BUSTYPE, "---"}, 744 {XPRESS, "XPRESS"}, 745 {UNKNOWN_BUSTYPE, "---"} 746}; 747/* from MP spec v1.4, table 5-1 */ 748static int default_data[7][5] = 749{ 750/* nbus, id0, type0, id1, type1 */ 751 {1, 0, ISA, 255, 255}, 752 {1, 0, EISA, 255, 255}, 753 {1, 0, EISA, 255, 255}, 754 {1, 0, MCA, 255, 255}, 755 {2, 0, ISA, 1, PCI}, 756 {2, 0, EISA, 1, PCI}, 757 {2, 0, MCA, 1, PCI} 758}; 759 760 761/* the bus data */ 762static bus_datum *bus_data; 763 764/* the IO INT data, one entry per possible APIC INTerrupt */ 765static io_int *io_apic_ints; 766 767static int nintrs; 768 769static int processor_entry(proc_entry_ptr entry, int cpu); 770static int bus_entry(bus_entry_ptr entry, int bus); 771static int io_apic_entry(io_apic_entry_ptr entry, int apic); 772static int int_entry(int_entry_ptr entry, int intr); 773static int lookup_bus_type(char *name); 774 775 776/* 777 * 1st pass on motherboard's Intel MP specification table. 778 * 779 * initializes: 780 * mp_ncpus = 1 781 * 782 * determines: 783 * cpu_apic_address (common to all CPUs) 784 * io_apic_address[N] 785 * mp_naps 786 * mp_nbusses 787 * mp_napics 788 * nintrs 789 */ 790static void 791mptable_pass1(void) 792{ 793 int x; 794 mpcth_t cth; 795 int totalSize; 796 void* position; 797 int count; 798 int type; 799 u_int id_mask; 800 801 POSTCODE(MPTABLE_PASS1_POST); 802 803 /* clear various tables */ 804 for (x = 0; x < NAPICID; ++x) { 805 io_apic_address[x] = ~0; /* IO APIC address table */ 806 } 807 808 /* init everything to empty */ 809 mp_naps = 0; 810 mp_nbusses = 0; 811 mp_napics = 0; 812 nintrs = 0; 813 id_mask = 0; 814 815 /* check for use of 'default' configuration */ 816 if (MPFPS_MPFB1 != 0) { 817 /* use default addresses */ 818 cpu_apic_address = DEFAULT_APIC_BASE; 819 io_apic_address[0] = DEFAULT_IO_APIC_BASE; 820 821 /* fill in with defaults */ 822 mp_naps = 2; /* includes BSP */ 823 mp_maxid = 1; 824 mp_nbusses = default_data[MPFPS_MPFB1 - 1][0]; 825#if defined(APIC_IO) 826 mp_napics = 1; 827 nintrs = 16; 828#endif /* APIC_IO */ 829 } 830 else { 831 if ((cth = mpfps->pap) == 0) 832 panic("MP Configuration Table Header MISSING!"); 833 834 cpu_apic_address = (vm_offset_t) cth->apic_address; 835 836 /* walk the table, recording info of interest */ 837 totalSize = cth->base_table_length - sizeof(struct MPCTH); 838 position = (u_char *) cth + sizeof(struct MPCTH); 839 count = cth->entry_count; 840 841 while (count--) { 842 switch (type = *(u_char *) position) { 843 case 0: /* processor_entry */ 844 if (((proc_entry_ptr)position)->cpu_flags 845 & PROCENTRY_FLAG_EN) { 846 ++mp_naps; 847 mp_maxid++; 848 id_mask |= 1 << 849 ((proc_entry_ptr)position)->apic_id; 850 } 851 break; 852 case 1: /* bus_entry */ 853 ++mp_nbusses; 854 break; 855 case 2: /* io_apic_entry */ 856 if (((io_apic_entry_ptr)position)->apic_flags 857 & IOAPICENTRY_FLAG_EN) 858 io_apic_address[mp_napics++] = 859 (vm_offset_t)((io_apic_entry_ptr) 860 position)->apic_address; 861 break; 862 case 3: /* int_entry */ 863 ++nintrs; 864 break; 865 case 4: /* int_entry */ 866 break; 867 default: 868 panic("mpfps Base Table HOSED!"); 869 /* NOTREACHED */ 870 } 871 872 totalSize -= basetable_entry_types[type].length; 873 (u_char*)position += basetable_entry_types[type].length; 874 } 875 } 876 877 /* qualify the numbers */ 878 if (mp_naps > MAXCPU) { 879 printf("Warning: only using %d of %d available CPUs!\n", 880 MAXCPU, mp_naps); 881 mp_naps = MAXCPU; 882 } 883 884 /* See if we need to fixup HT logical CPUs. */ 885 mptable_hyperthread_fixup(id_mask); 886 887 /* 888 * Count the BSP. 889 * This is also used as a counter while starting the APs. 890 */ 891 mp_ncpus = 1; 892 893 --mp_naps; /* subtract the BSP */ 894} 895 896 897/* 898 * 2nd pass on motherboard's Intel MP specification table. 899 * 900 * sets: 901 * boot_cpu_id 902 * ID_TO_IO(N), phy APIC ID to log CPU/IO table 903 * CPU_TO_ID(N), logical CPU to APIC ID table 904 * IO_TO_ID(N), logical IO to APIC ID table 905 * bus_data[N] 906 * io_apic_ints[N] 907 */ 908static int 909mptable_pass2(void) 910{ 911 struct PROCENTRY proc; 912 int x; 913 mpcth_t cth; 914 int totalSize; 915 void* position; 916 int count; 917 int type; 918 int apic, bus, cpu, intr; 919 int i, j; 920 int pgeflag; 921 922 POSTCODE(MPTABLE_PASS2_POST); 923 924 /* Initialize fake proc entry for use with HT fixup. */ 925 bzero(&proc, sizeof(proc)); 926 proc.type = 0; 927 proc.cpu_flags = PROCENTRY_FLAG_EN; 928 929 pgeflag = 0; /* XXX - Not used under SMP yet. */ 930 931 MALLOC(io_apic_versions, u_int32_t *, sizeof(u_int32_t) * mp_napics, 932 M_DEVBUF, M_WAITOK); 933 MALLOC(ioapic, volatile ioapic_t **, sizeof(ioapic_t *) * mp_napics, 934 M_DEVBUF, M_WAITOK); 935 MALLOC(io_apic_ints, io_int *, sizeof(io_int) * (nintrs + 1), 936 M_DEVBUF, M_WAITOK); 937 MALLOC(bus_data, bus_datum *, sizeof(bus_datum) * mp_nbusses, 938 M_DEVBUF, M_WAITOK); 939 940 bzero(ioapic, sizeof(ioapic_t *) * mp_napics); 941 942 for (i = 0; i < mp_napics; i++) { 943 for (j = 0; j < mp_napics; j++) { 944 /* same page frame as a previous IO apic? */ 945 if (((vm_offset_t)SMPpt[NPTEPG-2-j] & PG_FRAME) == 946 (io_apic_address[i] & PG_FRAME)) { 947 ioapic[i] = (ioapic_t *)((u_int)SMP_prvspace 948 + (NPTEPG-2-j) * PAGE_SIZE 949 + (io_apic_address[i] & PAGE_MASK)); 950 break; 951 } 952 /* use this slot if available */ 953 if (((vm_offset_t)SMPpt[NPTEPG-2-j] & PG_FRAME) == 0) { 954 SMPpt[NPTEPG-2-j] = (pt_entry_t)(PG_V | PG_RW | 955 pgeflag | (io_apic_address[i] & PG_FRAME)); 956 ioapic[i] = (ioapic_t *)((u_int)SMP_prvspace 957 + (NPTEPG-2-j) * PAGE_SIZE 958 + (io_apic_address[i] & PAGE_MASK)); 959 break; 960 } 961 } 962 } 963 964 /* clear various tables */ 965 for (x = 0; x < NAPICID; ++x) { 966 ID_TO_IO(x) = -1; /* phy APIC ID to log CPU/IO table */ 967 CPU_TO_ID(x) = -1; /* logical CPU to APIC ID table */ 968 IO_TO_ID(x) = -1; /* logical IO to APIC ID table */ 969 } 970 971 /* clear bus data table */ 972 for (x = 0; x < mp_nbusses; ++x) 973 bus_data[x].bus_id = 0xff; 974 975 /* clear IO APIC INT table */ 976 for (x = 0; x < (nintrs + 1); ++x) { 977 io_apic_ints[x].int_type = 0xff; 978 io_apic_ints[x].int_vector = 0xff; 979 } 980 981 /* setup the cpu/apic mapping arrays */ 982 boot_cpu_id = -1; 983 984 /* record whether PIC or virtual-wire mode */ 985 picmode = (mpfps->mpfb2 & 0x80) ? 1 : 0; 986 987 /* check for use of 'default' configuration */ 988 if (MPFPS_MPFB1 != 0) 989 return MPFPS_MPFB1; /* return default configuration type */ 990 991 if ((cth = mpfps->pap) == 0) 992 panic("MP Configuration Table Header MISSING!"); 993 994 /* walk the table, recording info of interest */ 995 totalSize = cth->base_table_length - sizeof(struct MPCTH); 996 position = (u_char *) cth + sizeof(struct MPCTH); 997 count = cth->entry_count; 998 apic = bus = intr = 0; 999 cpu = 1; /* pre-count the BSP */ 1000 1001 while (count--) { 1002 switch (type = *(u_char *) position) { 1003 case 0: 1004 if (processor_entry(position, cpu)) { 1005 if (logical_cpus != 0 && 1006 cpu % logical_cpus != 0) 1007 logical_cpus_mask |= (1 << cpu); 1008 ++cpu; 1009 } 1010 if (need_hyperthreading_fixup) { 1011 /* 1012 * Create fake mptable processor entries 1013 * and feed them to processor_entry() to 1014 * enumerate the logical CPUs. 1015 */ 1016 proc.apic_id = ((proc_entry_ptr)position)->apic_id; 1017 for (i = 1; i < logical_cpus; i++) { 1018 proc.apic_id++; 1019 (void)processor_entry(&proc, cpu); 1020 logical_cpus_mask |= (1 << cpu); 1021 cpu++; 1022 } 1023 } 1024 break; 1025 case 1: 1026 if (bus_entry(position, bus)) 1027 ++bus; 1028 break; 1029 case 2: 1030 if (io_apic_entry(position, apic)) 1031 ++apic; 1032 break; 1033 case 3: 1034 if (int_entry(position, intr)) 1035 ++intr; 1036 break; 1037 case 4: 1038 /* int_entry(position); */ 1039 break; 1040 default: 1041 panic("mpfps Base Table HOSED!"); 1042 /* NOTREACHED */ 1043 } 1044 1045 totalSize -= basetable_entry_types[type].length; 1046 (u_char *) position += basetable_entry_types[type].length; 1047 } 1048 1049 if (boot_cpu_id == -1) 1050 panic("NO BSP found!"); 1051 1052 /* report fact that its NOT a default configuration */ 1053 return 0; 1054} 1055 1056/* 1057 * Check if we should perform a hyperthreading "fix-up" to 1058 * enumerate any logical CPU's that aren't already listed 1059 * in the table. 1060 * 1061 * XXX: We assume that all of the physical CPUs in the 1062 * system have the same number of logical CPUs. 1063 * 1064 * XXX: We assume that APIC ID's are allocated such that 1065 * the APIC ID's for a physical processor are aligned 1066 * with the number of logical CPU's in the processor. 1067 */ 1068static void 1069mptable_hyperthread_fixup(u_int id_mask) 1070{ 1071 u_int i, id; 1072 1073 /* Nothing to do if there is no HTT support. */ 1074 if ((cpu_feature & CPUID_HTT) == 0) 1075 return; 1076 logical_cpus = (cpu_procinfo & CPUID_HTT_CORES) >> 16; 1077 if (logical_cpus <= 1) 1078 return; 1079 1080 /* 1081 * For each APIC ID of a CPU that is set in the mask, 1082 * scan the other candidate APIC ID's for this 1083 * physical processor. If any of those ID's are 1084 * already in the table, then kill the fixup. 1085 */ 1086 for (id = 0; id <= MAXCPU; id++) { 1087 if ((id_mask & 1 << id) == 0) 1088 continue; 1089 /* First, make sure we are on a logical_cpus boundary. */ 1090 if (id % logical_cpus != 0) 1091 return; 1092 for (i = id + 1; i < id + logical_cpus; i++) 1093 if ((id_mask & 1 << i) != 0) 1094 return; 1095 } 1096 1097 /* 1098 * Ok, the ID's checked out, so enable the fixup. We have to fixup 1099 * mp_naps and mp_maxid right now. 1100 */ 1101 need_hyperthreading_fixup = 1; 1102 mp_maxid *= logical_cpus; 1103 mp_naps *= logical_cpus; 1104} 1105 1106void 1107assign_apic_irq(int apic, int intpin, int irq) 1108{ 1109 int x; 1110 1111 if (int_to_apicintpin[irq].ioapic != -1) 1112 panic("assign_apic_irq: inconsistent table"); 1113 1114 int_to_apicintpin[irq].ioapic = apic; 1115 int_to_apicintpin[irq].int_pin = intpin; 1116 int_to_apicintpin[irq].apic_address = ioapic[apic]; 1117 int_to_apicintpin[irq].redirindex = IOAPIC_REDTBL + 2 * intpin; 1118 1119 for (x = 0; x < nintrs; x++) { 1120 if ((io_apic_ints[x].int_type == 0 || 1121 io_apic_ints[x].int_type == 3) && 1122 io_apic_ints[x].int_vector == 0xff && 1123 io_apic_ints[x].dst_apic_id == IO_TO_ID(apic) && 1124 io_apic_ints[x].dst_apic_int == intpin) 1125 io_apic_ints[x].int_vector = irq; 1126 } 1127} 1128 1129void 1130revoke_apic_irq(int irq) 1131{ 1132 int x; 1133 int oldapic; 1134 int oldintpin; 1135 1136 if (int_to_apicintpin[irq].ioapic == -1) 1137 panic("revoke_apic_irq: inconsistent table"); 1138 1139 oldapic = int_to_apicintpin[irq].ioapic; 1140 oldintpin = int_to_apicintpin[irq].int_pin; 1141 1142 int_to_apicintpin[irq].ioapic = -1; 1143 int_to_apicintpin[irq].int_pin = 0; 1144 int_to_apicintpin[irq].apic_address = NULL; 1145 int_to_apicintpin[irq].redirindex = 0; 1146 1147 for (x = 0; x < nintrs; x++) { 1148 if ((io_apic_ints[x].int_type == 0 || 1149 io_apic_ints[x].int_type == 3) && 1150 io_apic_ints[x].int_vector != 0xff && 1151 io_apic_ints[x].dst_apic_id == IO_TO_ID(oldapic) && 1152 io_apic_ints[x].dst_apic_int == oldintpin) 1153 io_apic_ints[x].int_vector = 0xff; 1154 } 1155} 1156 1157 1158static void 1159allocate_apic_irq(int intr) 1160{ 1161 int apic; 1162 int intpin; 1163 int irq; 1164 1165 if (io_apic_ints[intr].int_vector != 0xff) 1166 return; /* Interrupt handler already assigned */ 1167 1168 if (io_apic_ints[intr].int_type != 0 && 1169 (io_apic_ints[intr].int_type != 3 || 1170 (io_apic_ints[intr].dst_apic_id == IO_TO_ID(0) && 1171 io_apic_ints[intr].dst_apic_int == 0))) 1172 return; /* Not INT or ExtInt on != (0, 0) */ 1173 1174 irq = 0; 1175 while (irq < APIC_INTMAPSIZE && 1176 int_to_apicintpin[irq].ioapic != -1) 1177 irq++; 1178 1179 if (irq >= APIC_INTMAPSIZE) 1180 return; /* No free interrupt handlers */ 1181 1182 apic = ID_TO_IO(io_apic_ints[intr].dst_apic_id); 1183 intpin = io_apic_ints[intr].dst_apic_int; 1184 1185 assign_apic_irq(apic, intpin, irq); 1186 io_apic_setup_intpin(apic, intpin); 1187} 1188 1189 1190static void 1191swap_apic_id(int apic, int oldid, int newid) 1192{ 1193 int x; 1194 int oapic; 1195 1196 1197 if (oldid == newid) 1198 return; /* Nothing to do */ 1199 1200 printf("Changing APIC ID for IO APIC #%d from %d to %d in MP table\n", 1201 apic, oldid, newid); 1202 1203 /* Swap physical APIC IDs in interrupt entries */ 1204 for (x = 0; x < nintrs; x++) { 1205 if (io_apic_ints[x].dst_apic_id == oldid) 1206 io_apic_ints[x].dst_apic_id = newid; 1207 else if (io_apic_ints[x].dst_apic_id == newid) 1208 io_apic_ints[x].dst_apic_id = oldid; 1209 } 1210 1211 /* Swap physical APIC IDs in IO_TO_ID mappings */ 1212 for (oapic = 0; oapic < mp_napics; oapic++) 1213 if (IO_TO_ID(oapic) == newid) 1214 break; 1215 1216 if (oapic < mp_napics) { 1217 printf("Changing APIC ID for IO APIC #%d from " 1218 "%d to %d in MP table\n", 1219 oapic, newid, oldid); 1220 IO_TO_ID(oapic) = oldid; 1221 } 1222 IO_TO_ID(apic) = newid; 1223} 1224 1225 1226static void 1227fix_id_to_io_mapping(void) 1228{ 1229 int x; 1230 1231 for (x = 0; x < NAPICID; x++) 1232 ID_TO_IO(x) = -1; 1233 1234 for (x = 0; x <= mp_naps; x++) 1235 if (CPU_TO_ID(x) < NAPICID) 1236 ID_TO_IO(CPU_TO_ID(x)) = x; 1237 1238 for (x = 0; x < mp_napics; x++) 1239 if (IO_TO_ID(x) < NAPICID) 1240 ID_TO_IO(IO_TO_ID(x)) = x; 1241} 1242 1243 1244static int 1245first_free_apic_id(void) 1246{ 1247 int freeid, x; 1248 1249 for (freeid = 0; freeid < NAPICID; freeid++) { 1250 for (x = 0; x <= mp_naps; x++) 1251 if (CPU_TO_ID(x) == freeid) 1252 break; 1253 if (x <= mp_naps) 1254 continue; 1255 for (x = 0; x < mp_napics; x++) 1256 if (IO_TO_ID(x) == freeid) 1257 break; 1258 if (x < mp_napics) 1259 continue; 1260 return freeid; 1261 } 1262 return freeid; 1263} 1264 1265 1266static int 1267io_apic_id_acceptable(int apic, int id) 1268{ 1269 int cpu; /* Logical CPU number */ 1270 int oapic; /* Logical IO APIC number for other IO APIC */ 1271 1272 if (id >= NAPICID) 1273 return 0; /* Out of range */ 1274 1275 for (cpu = 0; cpu <= mp_naps; cpu++) 1276 if (CPU_TO_ID(cpu) == id) 1277 return 0; /* Conflict with CPU */ 1278 1279 for (oapic = 0; oapic < mp_napics && oapic < apic; oapic++) 1280 if (IO_TO_ID(oapic) == id) 1281 return 0; /* Conflict with other APIC */ 1282 1283 return 1; /* ID is acceptable for IO APIC */ 1284} 1285 1286 1287/* 1288 * parse an Intel MP specification table 1289 */ 1290static void 1291fix_mp_table(void) 1292{ 1293 int x; 1294 int id; 1295 int bus_0 = 0; /* Stop GCC warning */ 1296 int bus_pci = 0; /* Stop GCC warning */ 1297 int num_pci_bus; 1298 int apic; /* IO APIC unit number */ 1299 int freeid; /* Free physical APIC ID */ 1300 int physid; /* Current physical IO APIC ID */ 1301 1302 /* 1303 * Fix mis-numbering of the PCI bus and its INT entries if the BIOS 1304 * did it wrong. The MP spec says that when more than 1 PCI bus 1305 * exists the BIOS must begin with bus entries for the PCI bus and use 1306 * actual PCI bus numbering. This implies that when only 1 PCI bus 1307 * exists the BIOS can choose to ignore this ordering, and indeed many 1308 * MP motherboards do ignore it. This causes a problem when the PCI 1309 * sub-system makes requests of the MP sub-system based on PCI bus 1310 * numbers. So here we look for the situation and renumber the 1311 * busses and associated INTs in an effort to "make it right". 1312 */ 1313 1314 /* find bus 0, PCI bus, count the number of PCI busses */ 1315 for (num_pci_bus = 0, x = 0; x < mp_nbusses; ++x) { 1316 if (bus_data[x].bus_id == 0) { 1317 bus_0 = x; 1318 } 1319 if (bus_data[x].bus_type == PCI) { 1320 ++num_pci_bus; 1321 bus_pci = x; 1322 } 1323 } 1324 /* 1325 * bus_0 == slot of bus with ID of 0 1326 * bus_pci == slot of last PCI bus encountered 1327 */ 1328 1329 /* check the 1 PCI bus case for sanity */ 1330 /* if it is number 0 all is well */ 1331 if (num_pci_bus == 1 && 1332 bus_data[bus_pci].bus_id != 0) { 1333 1334 /* mis-numbered, swap with whichever bus uses slot 0 */ 1335 1336 /* swap the bus entry types */ 1337 bus_data[bus_pci].bus_type = bus_data[bus_0].bus_type; 1338 bus_data[bus_0].bus_type = PCI; 1339 1340 /* swap each relavant INTerrupt entry */ 1341 id = bus_data[bus_pci].bus_id; 1342 for (x = 0; x < nintrs; ++x) { 1343 if (io_apic_ints[x].src_bus_id == id) { 1344 io_apic_ints[x].src_bus_id = 0; 1345 } 1346 else if (io_apic_ints[x].src_bus_id == 0) { 1347 io_apic_ints[x].src_bus_id = id; 1348 } 1349 } 1350 } 1351 1352 /* Assign IO APIC IDs. 1353 * 1354 * First try the existing ID. If a conflict is detected, try 1355 * the ID in the MP table. If a conflict is still detected, find 1356 * a free id. 1357 * 1358 * We cannot use the ID_TO_IO table before all conflicts has been 1359 * resolved and the table has been corrected. 1360 */ 1361 for (apic = 0; apic < mp_napics; ++apic) { /* For all IO APICs */ 1362 1363 /* First try to use the value set by the BIOS */ 1364 physid = io_apic_get_id(apic); 1365 if (io_apic_id_acceptable(apic, physid)) { 1366 if (IO_TO_ID(apic) != physid) 1367 swap_apic_id(apic, IO_TO_ID(apic), physid); 1368 continue; 1369 } 1370 1371 /* Then check if the value in the MP table is acceptable */ 1372 if (io_apic_id_acceptable(apic, IO_TO_ID(apic))) 1373 continue; 1374 1375 /* Last resort, find a free APIC ID and use it */ 1376 freeid = first_free_apic_id(); 1377 if (freeid >= NAPICID) 1378 panic("No free physical APIC IDs found"); 1379 1380 if (io_apic_id_acceptable(apic, freeid)) { 1381 swap_apic_id(apic, IO_TO_ID(apic), freeid); 1382 continue; 1383 } 1384 panic("Free physical APIC ID not usable"); 1385 } 1386 fix_id_to_io_mapping(); 1387 1388 /* detect and fix broken Compaq MP table */ 1389 if (apic_int_type(0, 0) == -1) { 1390 printf("APIC_IO: MP table broken: 8259->APIC entry missing!\n"); 1391 io_apic_ints[nintrs].int_type = 3; /* ExtInt */ 1392 io_apic_ints[nintrs].int_vector = 0xff; /* Unassigned */ 1393 /* XXX fixme, set src bus id etc, but it doesn't seem to hurt */ 1394 io_apic_ints[nintrs].dst_apic_id = IO_TO_ID(0); 1395 io_apic_ints[nintrs].dst_apic_int = 0; /* Pin 0 */ 1396 nintrs++; 1397 } 1398} 1399 1400 1401/* Assign low level interrupt handlers */ 1402static void 1403setup_apic_irq_mapping(void) 1404{ 1405 int x; 1406 int int_vector; 1407 1408 /* Clear array */ 1409 for (x = 0; x < APIC_INTMAPSIZE; x++) { 1410 int_to_apicintpin[x].ioapic = -1; 1411 int_to_apicintpin[x].int_pin = 0; 1412 int_to_apicintpin[x].apic_address = NULL; 1413 int_to_apicintpin[x].redirindex = 0; 1414 } 1415 1416 /* First assign ISA/EISA interrupts */ 1417 for (x = 0; x < nintrs; x++) { 1418 int_vector = io_apic_ints[x].src_bus_irq; 1419 if (int_vector < APIC_INTMAPSIZE && 1420 io_apic_ints[x].int_vector == 0xff && 1421 int_to_apicintpin[int_vector].ioapic == -1 && 1422 (apic_int_is_bus_type(x, ISA) || 1423 apic_int_is_bus_type(x, EISA)) && 1424 io_apic_ints[x].int_type == 0) { 1425 assign_apic_irq(ID_TO_IO(io_apic_ints[x].dst_apic_id), 1426 io_apic_ints[x].dst_apic_int, 1427 int_vector); 1428 } 1429 } 1430 1431 /* Assign ExtInt entry if no ISA/EISA interrupt 0 entry */ 1432 for (x = 0; x < nintrs; x++) { 1433 if (io_apic_ints[x].dst_apic_int == 0 && 1434 io_apic_ints[x].dst_apic_id == IO_TO_ID(0) && 1435 io_apic_ints[x].int_vector == 0xff && 1436 int_to_apicintpin[0].ioapic == -1 && 1437 io_apic_ints[x].int_type == 3) { 1438 assign_apic_irq(0, 0, 0); 1439 break; 1440 } 1441 } 1442 /* PCI interrupt assignment is deferred */ 1443} 1444 1445 1446static int 1447processor_entry(proc_entry_ptr entry, int cpu) 1448{ 1449 /* check for usability */ 1450 if (!(entry->cpu_flags & PROCENTRY_FLAG_EN)) 1451 return 0; 1452 1453 if(entry->apic_id >= NAPICID) 1454 panic("CPU APIC ID out of range (0..%d)", NAPICID - 1); 1455 /* check for BSP flag */ 1456 if (entry->cpu_flags & PROCENTRY_FLAG_BP) { 1457 boot_cpu_id = entry->apic_id; 1458 CPU_TO_ID(0) = entry->apic_id; 1459 ID_TO_CPU(entry->apic_id) = 0; 1460 return 0; /* its already been counted */ 1461 } 1462 1463 /* add another AP to list, if less than max number of CPUs */ 1464 else if (cpu < MAXCPU) { 1465 CPU_TO_ID(cpu) = entry->apic_id; 1466 ID_TO_CPU(entry->apic_id) = cpu; 1467 return 1; 1468 } 1469 1470 return 0; 1471} 1472 1473 1474static int 1475bus_entry(bus_entry_ptr entry, int bus) 1476{ 1477 int x; 1478 char c, name[8]; 1479 1480 /* encode the name into an index */ 1481 for (x = 0; x < 6; ++x) { 1482 if ((c = entry->bus_type[x]) == ' ') 1483 break; 1484 name[x] = c; 1485 } 1486 name[x] = '\0'; 1487 1488 if ((x = lookup_bus_type(name)) == UNKNOWN_BUSTYPE) 1489 panic("unknown bus type: '%s'", name); 1490 1491 bus_data[bus].bus_id = entry->bus_id; 1492 bus_data[bus].bus_type = x; 1493 1494 return 1; 1495} 1496 1497 1498static int 1499io_apic_entry(io_apic_entry_ptr entry, int apic) 1500{ 1501 if (!(entry->apic_flags & IOAPICENTRY_FLAG_EN)) 1502 return 0; 1503 1504 IO_TO_ID(apic) = entry->apic_id; 1505 if (entry->apic_id < NAPICID) 1506 ID_TO_IO(entry->apic_id) = apic; 1507 1508 return 1; 1509} 1510 1511 1512static int 1513lookup_bus_type(char *name) 1514{ 1515 int x; 1516 1517 for (x = 0; x < MAX_BUSTYPE; ++x) 1518 if (strcmp(bus_type_table[x].name, name) == 0) 1519 return bus_type_table[x].type; 1520 1521 return UNKNOWN_BUSTYPE; 1522} 1523 1524 1525static int 1526int_entry(int_entry_ptr entry, int intr) 1527{ 1528 int apic; 1529 1530 io_apic_ints[intr].int_type = entry->int_type; 1531 io_apic_ints[intr].int_flags = entry->int_flags; 1532 io_apic_ints[intr].src_bus_id = entry->src_bus_id; 1533 io_apic_ints[intr].src_bus_irq = entry->src_bus_irq; 1534 if (entry->dst_apic_id == 255) { 1535 /* This signal goes to all IO APICS. Select an IO APIC 1536 with sufficient number of interrupt pins */ 1537 for (apic = 0; apic < mp_napics; apic++) 1538 if (((io_apic_read(apic, IOAPIC_VER) & 1539 IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) >= 1540 entry->dst_apic_int) 1541 break; 1542 if (apic < mp_napics) 1543 io_apic_ints[intr].dst_apic_id = IO_TO_ID(apic); 1544 else 1545 io_apic_ints[intr].dst_apic_id = entry->dst_apic_id; 1546 } else 1547 io_apic_ints[intr].dst_apic_id = entry->dst_apic_id; 1548 io_apic_ints[intr].dst_apic_int = entry->dst_apic_int; 1549 1550 return 1; 1551} 1552 1553 1554static int 1555apic_int_is_bus_type(int intr, int bus_type) 1556{ 1557 int bus; 1558 1559 for (bus = 0; bus < mp_nbusses; ++bus) 1560 if ((bus_data[bus].bus_id == io_apic_ints[intr].src_bus_id) 1561 && ((int) bus_data[bus].bus_type == bus_type)) 1562 return 1; 1563 1564 return 0; 1565} 1566 1567 1568/* 1569 * Given a traditional ISA INT mask, return an APIC mask. 1570 */ 1571u_int 1572isa_apic_mask(u_int isa_mask) 1573{ 1574 int isa_irq; 1575 int apic_pin; 1576 1577#if defined(SKIP_IRQ15_REDIRECT) 1578 if (isa_mask == (1 << 15)) { 1579 printf("skipping ISA IRQ15 redirect\n"); 1580 return isa_mask; 1581 } 1582#endif /* SKIP_IRQ15_REDIRECT */ 1583 1584 isa_irq = ffs(isa_mask); /* find its bit position */ 1585 if (isa_irq == 0) /* doesn't exist */ 1586 return 0; 1587 --isa_irq; /* make it zero based */ 1588 1589 apic_pin = isa_apic_irq(isa_irq); /* look for APIC connection */ 1590 if (apic_pin == -1) 1591 return 0; 1592 1593 return (1 << apic_pin); /* convert pin# to a mask */ 1594} 1595 1596 1597/* 1598 * Determine which APIC pin an ISA/EISA INT is attached to. 1599 */ 1600#define INTTYPE(I) (io_apic_ints[(I)].int_type) 1601#define INTPIN(I) (io_apic_ints[(I)].dst_apic_int) 1602#define INTIRQ(I) (io_apic_ints[(I)].int_vector) 1603#define INTAPIC(I) (ID_TO_IO(io_apic_ints[(I)].dst_apic_id)) 1604 1605#define SRCBUSIRQ(I) (io_apic_ints[(I)].src_bus_irq) 1606int 1607isa_apic_irq(int isa_irq) 1608{ 1609 int intr; 1610 1611 for (intr = 0; intr < nintrs; ++intr) { /* check each record */ 1612 if (INTTYPE(intr) == 0) { /* standard INT */ 1613 if (SRCBUSIRQ(intr) == isa_irq) { 1614 if (apic_int_is_bus_type(intr, ISA) || 1615 apic_int_is_bus_type(intr, EISA)) { 1616 if (INTIRQ(intr) == 0xff) 1617 return -1; /* unassigned */ 1618 return INTIRQ(intr); /* found */ 1619 } 1620 } 1621 } 1622 } 1623 return -1; /* NOT found */ 1624} 1625 1626 1627/* 1628 * Determine which APIC pin a PCI INT is attached to. 1629 */ 1630#define SRCBUSID(I) (io_apic_ints[(I)].src_bus_id) 1631#define SRCBUSDEVICE(I) ((io_apic_ints[(I)].src_bus_irq >> 2) & 0x1f) 1632#define SRCBUSLINE(I) (io_apic_ints[(I)].src_bus_irq & 0x03) 1633int 1634pci_apic_irq(int pciBus, int pciDevice, int pciInt) 1635{ 1636 int intr; 1637 1638 --pciInt; /* zero based */ 1639 1640 for (intr = 0; intr < nintrs; ++intr) /* check each record */ 1641 if ((INTTYPE(intr) == 0) /* standard INT */ 1642 && (SRCBUSID(intr) == pciBus) 1643 && (SRCBUSDEVICE(intr) == pciDevice) 1644 && (SRCBUSLINE(intr) == pciInt)) /* a candidate IRQ */ 1645 if (apic_int_is_bus_type(intr, PCI)) { 1646 if (INTIRQ(intr) == 0xff) 1647 allocate_apic_irq(intr); 1648 if (INTIRQ(intr) == 0xff) 1649 return -1; /* unassigned */ 1650 return INTIRQ(intr); /* exact match */ 1651 } 1652 1653 return -1; /* NOT found */ 1654} 1655 1656int 1657next_apic_irq(int irq) 1658{ 1659 int intr, ointr; 1660 int bus, bustype; 1661 1662 bus = 0; 1663 bustype = 0; 1664 for (intr = 0; intr < nintrs; intr++) { 1665 if (INTIRQ(intr) != irq || INTTYPE(intr) != 0) 1666 continue; 1667 bus = SRCBUSID(intr); 1668 bustype = apic_bus_type(bus); 1669 if (bustype != ISA && 1670 bustype != EISA && 1671 bustype != PCI) 1672 continue; 1673 break; 1674 } 1675 if (intr >= nintrs) { 1676 return -1; 1677 } 1678 for (ointr = intr + 1; ointr < nintrs; ointr++) { 1679 if (INTTYPE(ointr) != 0) 1680 continue; 1681 if (bus != SRCBUSID(ointr)) 1682 continue; 1683 if (bustype == PCI) { 1684 if (SRCBUSDEVICE(intr) != SRCBUSDEVICE(ointr)) 1685 continue; 1686 if (SRCBUSLINE(intr) != SRCBUSLINE(ointr)) 1687 continue; 1688 } 1689 if (bustype == ISA || bustype == EISA) { 1690 if (SRCBUSIRQ(intr) != SRCBUSIRQ(ointr)) 1691 continue; 1692 } 1693 if (INTPIN(intr) == INTPIN(ointr)) 1694 continue; 1695 break; 1696 } 1697 if (ointr >= nintrs) { 1698 return -1; 1699 } 1700 return INTIRQ(ointr); 1701} 1702#undef SRCBUSLINE 1703#undef SRCBUSDEVICE 1704#undef SRCBUSID 1705#undef SRCBUSIRQ 1706 1707#undef INTPIN 1708#undef INTIRQ 1709#undef INTAPIC 1710#undef INTTYPE 1711 1712 1713/* 1714 * Reprogram the MB chipset to NOT redirect an ISA INTerrupt. 1715 * 1716 * XXX FIXME: 1717 * Exactly what this means is unclear at this point. It is a solution 1718 * for motherboards that redirect the MBIRQ0 pin. Generically a motherboard 1719 * could route any of the ISA INTs to upper (>15) IRQ values. But most would 1720 * NOT be redirected via MBIRQ0, thus "undirect()ing" them would NOT be an 1721 * option. 1722 */ 1723int 1724undirect_isa_irq(int rirq) 1725{ 1726#if defined(READY) 1727 if (bootverbose) 1728 printf("Freeing redirected ISA irq %d.\n", rirq); 1729 /** FIXME: tickle the MB redirector chip */ 1730 return -1; 1731#else 1732 if (bootverbose) 1733 printf("Freeing (NOT implemented) redirected ISA irq %d.\n", rirq); 1734 return 0; 1735#endif /* READY */ 1736} 1737 1738 1739/* 1740 * Reprogram the MB chipset to NOT redirect a PCI INTerrupt 1741 */ 1742int 1743undirect_pci_irq(int rirq) 1744{ 1745#if defined(READY) 1746 if (bootverbose) 1747 printf("Freeing redirected PCI irq %d.\n", rirq); 1748 1749 /** FIXME: tickle the MB redirector chip */ 1750 return -1; 1751#else 1752 if (bootverbose) 1753 printf("Freeing (NOT implemented) redirected PCI irq %d.\n", 1754 rirq); 1755 return 0; 1756#endif /* READY */ 1757} 1758 1759 1760/* 1761 * given a bus ID, return: 1762 * the bus type if found 1763 * -1 if NOT found 1764 */ 1765int 1766apic_bus_type(int id) 1767{ 1768 int x; 1769 1770 for (x = 0; x < mp_nbusses; ++x) 1771 if (bus_data[x].bus_id == id) 1772 return bus_data[x].bus_type; 1773 1774 return -1; 1775} 1776 1777 1778/* 1779 * given a LOGICAL APIC# and pin#, return: 1780 * the associated src bus ID if found 1781 * -1 if NOT found 1782 */ 1783int 1784apic_src_bus_id(int apic, int pin) 1785{ 1786 int x; 1787 1788 /* search each of the possible INTerrupt sources */ 1789 for (x = 0; x < nintrs; ++x) 1790 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) && 1791 (pin == io_apic_ints[x].dst_apic_int)) 1792 return (io_apic_ints[x].src_bus_id); 1793 1794 return -1; /* NOT found */ 1795} 1796 1797 1798/* 1799 * given a LOGICAL APIC# and pin#, return: 1800 * the associated src bus IRQ if found 1801 * -1 if NOT found 1802 */ 1803int 1804apic_src_bus_irq(int apic, int pin) 1805{ 1806 int x; 1807 1808 for (x = 0; x < nintrs; x++) 1809 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) && 1810 (pin == io_apic_ints[x].dst_apic_int)) 1811 return (io_apic_ints[x].src_bus_irq); 1812 1813 return -1; /* NOT found */ 1814} 1815 1816 1817/* 1818 * given a LOGICAL APIC# and pin#, return: 1819 * the associated INTerrupt type if found 1820 * -1 if NOT found 1821 */ 1822int 1823apic_int_type(int apic, int pin) 1824{ 1825 int x; 1826 1827 /* search each of the possible INTerrupt sources */ 1828 for (x = 0; x < nintrs; ++x) 1829 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) && 1830 (pin == io_apic_ints[x].dst_apic_int)) 1831 return (io_apic_ints[x].int_type); 1832 1833 return -1; /* NOT found */ 1834} 1835 1836int 1837apic_irq(int apic, int pin) 1838{ 1839 int x; 1840 int res; 1841 1842 for (x = 0; x < nintrs; ++x) 1843 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) && 1844 (pin == io_apic_ints[x].dst_apic_int)) { 1845 res = io_apic_ints[x].int_vector; 1846 if (res == 0xff) 1847 return -1; 1848 if (apic != int_to_apicintpin[res].ioapic) 1849 panic("apic_irq: inconsistent table"); 1850 if (pin != int_to_apicintpin[res].int_pin) 1851 panic("apic_irq inconsistent table (2)"); 1852 return res; 1853 } 1854 return -1; 1855} 1856 1857 1858/* 1859 * given a LOGICAL APIC# and pin#, return: 1860 * the associated trigger mode if found 1861 * -1 if NOT found 1862 */ 1863int 1864apic_trigger(int apic, int pin) 1865{ 1866 int x; 1867 1868 /* search each of the possible INTerrupt sources */ 1869 for (x = 0; x < nintrs; ++x) 1870 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) && 1871 (pin == io_apic_ints[x].dst_apic_int)) 1872 return ((io_apic_ints[x].int_flags >> 2) & 0x03); 1873 1874 return -1; /* NOT found */ 1875} 1876 1877 1878/* 1879 * given a LOGICAL APIC# and pin#, return: 1880 * the associated 'active' level if found 1881 * -1 if NOT found 1882 */ 1883int 1884apic_polarity(int apic, int pin) 1885{ 1886 int x; 1887 1888 /* search each of the possible INTerrupt sources */ 1889 for (x = 0; x < nintrs; ++x) 1890 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) && 1891 (pin == io_apic_ints[x].dst_apic_int)) 1892 return (io_apic_ints[x].int_flags & 0x03); 1893 1894 return -1; /* NOT found */ 1895} 1896 1897 1898/* 1899 * set data according to MP defaults 1900 * FIXME: probably not complete yet... 1901 */ 1902static void 1903default_mp_table(int type) 1904{ 1905 int ap_cpu_id; 1906#if defined(APIC_IO) 1907 int io_apic_id; 1908 int pin; 1909#endif /* APIC_IO */ 1910 1911#if 0 1912 printf(" MP default config type: %d\n", type); 1913 switch (type) { 1914 case 1: 1915 printf(" bus: ISA, APIC: 82489DX\n"); 1916 break; 1917 case 2: 1918 printf(" bus: EISA, APIC: 82489DX\n"); 1919 break; 1920 case 3: 1921 printf(" bus: EISA, APIC: 82489DX\n"); 1922 break; 1923 case 4: 1924 printf(" bus: MCA, APIC: 82489DX\n"); 1925 break; 1926 case 5: 1927 printf(" bus: ISA+PCI, APIC: Integrated\n"); 1928 break; 1929 case 6: 1930 printf(" bus: EISA+PCI, APIC: Integrated\n"); 1931 break; 1932 case 7: 1933 printf(" bus: MCA+PCI, APIC: Integrated\n"); 1934 break; 1935 default: 1936 printf(" future type\n"); 1937 break; 1938 /* NOTREACHED */ 1939 } 1940#endif /* 0 */ 1941 1942 boot_cpu_id = (lapic.id & APIC_ID_MASK) >> 24; 1943 ap_cpu_id = (boot_cpu_id == 0) ? 1 : 0; 1944 1945 /* BSP */ 1946 CPU_TO_ID(0) = boot_cpu_id; 1947 ID_TO_CPU(boot_cpu_id) = 0; 1948 1949 /* one and only AP */ 1950 CPU_TO_ID(1) = ap_cpu_id; 1951 ID_TO_CPU(ap_cpu_id) = 1; 1952 1953#if defined(APIC_IO) 1954 /* one and only IO APIC */ 1955 io_apic_id = (io_apic_read(0, IOAPIC_ID) & APIC_ID_MASK) >> 24; 1956 1957 /* 1958 * sanity check, refer to MP spec section 3.6.6, last paragraph 1959 * necessary as some hardware isn't properly setting up the IO APIC 1960 */ 1961#if defined(REALLY_ANAL_IOAPICID_VALUE) 1962 if (io_apic_id != 2) { 1963#else 1964 if ((io_apic_id == 0) || (io_apic_id == 1) || (io_apic_id == 15)) { 1965#endif /* REALLY_ANAL_IOAPICID_VALUE */ 1966 io_apic_set_id(0, 2); 1967 io_apic_id = 2; 1968 } 1969 IO_TO_ID(0) = io_apic_id; 1970 ID_TO_IO(io_apic_id) = 0; 1971#endif /* APIC_IO */ 1972 1973 /* fill out bus entries */ 1974 switch (type) { 1975 case 1: 1976 case 2: 1977 case 3: 1978 case 4: 1979 case 5: 1980 case 6: 1981 case 7: 1982 bus_data[0].bus_id = default_data[type - 1][1]; 1983 bus_data[0].bus_type = default_data[type - 1][2]; 1984 bus_data[1].bus_id = default_data[type - 1][3]; 1985 bus_data[1].bus_type = default_data[type - 1][4]; 1986 break; 1987 1988 /* case 4: case 7: MCA NOT supported */ 1989 default: /* illegal/reserved */ 1990 panic("BAD default MP config: %d", type); 1991 /* NOTREACHED */ 1992 } 1993 1994#if defined(APIC_IO) 1995 /* general cases from MP v1.4, table 5-2 */ 1996 for (pin = 0; pin < 16; ++pin) { 1997 io_apic_ints[pin].int_type = 0; 1998 io_apic_ints[pin].int_flags = 0x05; /* edge/active-hi */ 1999 io_apic_ints[pin].src_bus_id = 0; 2000 io_apic_ints[pin].src_bus_irq = pin; /* IRQ2 caught below */ 2001 io_apic_ints[pin].dst_apic_id = io_apic_id; 2002 io_apic_ints[pin].dst_apic_int = pin; /* 1-to-1 */ 2003 } 2004 2005 /* special cases from MP v1.4, table 5-2 */ 2006 if (type == 2) { 2007 io_apic_ints[2].int_type = 0xff; /* N/C */ 2008 io_apic_ints[13].int_type = 0xff; /* N/C */ 2009#if !defined(APIC_MIXED_MODE) 2010 /** FIXME: ??? */ 2011 panic("sorry, can't support type 2 default yet"); 2012#endif /* APIC_MIXED_MODE */ 2013 } 2014 else 2015 io_apic_ints[2].src_bus_irq = 0; /* ISA IRQ0 is on APIC INT 2 */ 2016 2017 if (type == 7) 2018 io_apic_ints[0].int_type = 0xff; /* N/C */ 2019 else 2020 io_apic_ints[0].int_type = 3; /* vectored 8259 */ 2021#endif /* APIC_IO */ 2022} 2023 2024 2025/* 2026 * start each AP in our list 2027 */ 2028static int 2029start_all_aps(u_int boot_addr) 2030{ 2031 int x, i, pg; 2032#ifndef PC98 2033 u_char mpbiosreason; 2034#endif 2035 u_long mpbioswarmvec; 2036 struct pcpu *pc; 2037 char *stack; 2038 uintptr_t kptbase; 2039 2040 POSTCODE(START_ALL_APS_POST); 2041 2042 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN); 2043 2044 /* initialize BSP's local APIC */ 2045 apic_initialize(); 2046 bsp_apic_ready = 1; 2047 2048 /* install the AP 1st level boot code */ 2049 install_ap_tramp(boot_addr); 2050 2051 2052 /* save the current value of the warm-start vector */ 2053 mpbioswarmvec = *((u_long *) WARMBOOT_OFF); 2054#ifndef PC98 2055 outb(CMOS_REG, BIOS_RESET); 2056 mpbiosreason = inb(CMOS_DATA); 2057#endif 2058 2059 /* set up temporary P==V mapping for AP boot */ 2060 /* XXX this is a hack, we should boot the AP on its own stack/PTD */ 2061 kptbase = (uintptr_t)(void *)KPTphys; 2062 for (x = 0; x < NKPT; x++) 2063 PTD[x] = (pd_entry_t)(PG_V | PG_RW | 2064 ((kptbase + x * PAGE_SIZE) & PG_FRAME)); 2065 invltlb(); 2066 2067 /* start each AP */ 2068 for (x = 1; x <= mp_naps; ++x) { 2069 2070 /* This is a bit verbose, it will go away soon. */ 2071 2072 /* first page of AP's private space */ 2073 pg = x * i386_btop(sizeof(struct privatespace)); 2074 2075 /* allocate a new private data page */ 2076 pc = (struct pcpu *)kmem_alloc(kernel_map, PAGE_SIZE); 2077 2078 /* wire it into the private page table page */ 2079 SMPpt[pg] = (pt_entry_t)(PG_V | PG_RW | vtophys(pc)); 2080 2081 /* allocate and set up an idle stack data page */ 2082 stack = (char *)kmem_alloc(kernel_map, KSTACK_PAGES * PAGE_SIZE); /* XXXKSE */ 2083 for (i = 0; i < KSTACK_PAGES; i++) 2084 SMPpt[pg + 1 + i] = (pt_entry_t) 2085 (PG_V | PG_RW | vtophys(PAGE_SIZE * i + stack)); 2086 2087 /* prime data page for it to use */ 2088 pcpu_init(pc, x, sizeof(struct pcpu)); 2089 2090 /* setup a vector to our boot code */ 2091 *((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET; 2092 *((volatile u_short *) WARMBOOT_SEG) = (boot_addr >> 4); 2093#ifndef PC98 2094 outb(CMOS_REG, BIOS_RESET); 2095 outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */ 2096#endif 2097 2098 bootSTK = &SMP_prvspace[x].idlekstack[KSTACK_PAGES * PAGE_SIZE]; 2099 bootAP = x; 2100 2101 /* attempt to start the Application Processor */ 2102 CHECK_INIT(99); /* setup checkpoints */ 2103 if (!start_ap(x, boot_addr)) { 2104 printf("AP #%d (PHY# %d) failed!\n", x, CPU_TO_ID(x)); 2105 CHECK_PRINT("trace"); /* show checkpoints */ 2106 /* better panic as the AP may be running loose */ 2107 printf("panic y/n? [y] "); 2108 if (cngetc() != 'n') 2109 panic("bye-bye"); 2110 } 2111 CHECK_PRINT("trace"); /* show checkpoints */ 2112 2113 /* record its version info */ 2114 cpu_apic_versions[x] = cpu_apic_versions[0]; 2115 2116 all_cpus |= (1 << x); /* record AP in CPU map */ 2117 } 2118 2119 /* build our map of 'other' CPUs */ 2120 PCPU_SET(other_cpus, all_cpus & ~PCPU_GET(cpumask)); 2121 2122 /* fill in our (BSP) APIC version */ 2123 cpu_apic_versions[0] = lapic.version; 2124 2125 /* restore the warmstart vector */ 2126 *(u_long *) WARMBOOT_OFF = mpbioswarmvec; 2127#ifndef PC98 2128 outb(CMOS_REG, BIOS_RESET); 2129 outb(CMOS_DATA, mpbiosreason); 2130#endif 2131 2132 /* 2133 * Set up the idle context for the BSP. Similar to above except 2134 * that some was done by locore, some by pmap.c and some is implicit 2135 * because the BSP is cpu#0 and the page is initially zero, and also 2136 * because we can refer to variables by name on the BSP.. 2137 */ 2138 2139 /* Allocate and setup BSP idle stack */ 2140 stack = (char *)kmem_alloc(kernel_map, KSTACK_PAGES * PAGE_SIZE); 2141 for (i = 0; i < KSTACK_PAGES; i++) 2142 SMPpt[1 + i] = (pt_entry_t) 2143 (PG_V | PG_RW | vtophys(PAGE_SIZE * i + stack)); 2144 2145 for (x = 0; x < NKPT; x++) 2146 PTD[x] = 0; 2147 pmap_set_opt(); 2148 2149 /* number of APs actually started */ 2150 return mp_ncpus - 1; 2151} 2152 2153 2154/* 2155 * load the 1st level AP boot code into base memory. 2156 */ 2157 2158/* targets for relocation */ 2159extern void bigJump(void); 2160extern void bootCodeSeg(void); 2161extern void bootDataSeg(void); 2162extern void MPentry(void); 2163extern u_int MP_GDT; 2164extern u_int mp_gdtbase; 2165 2166static void 2167install_ap_tramp(u_int boot_addr) 2168{ 2169 int x; 2170 int size = *(int *) ((u_long) & bootMP_size); 2171 u_char *src = (u_char *) ((u_long) bootMP); 2172 u_char *dst = (u_char *) boot_addr + KERNBASE; 2173 u_int boot_base = (u_int) bootMP; 2174 u_int8_t *dst8; 2175 u_int16_t *dst16; 2176 u_int32_t *dst32; 2177 2178 POSTCODE(INSTALL_AP_TRAMP_POST); 2179 2180 for (x = 0; x < size; ++x) 2181 *dst++ = *src++; 2182 2183 /* 2184 * modify addresses in code we just moved to basemem. unfortunately we 2185 * need fairly detailed info about mpboot.s for this to work. changes 2186 * to mpboot.s might require changes here. 2187 */ 2188 2189 /* boot code is located in KERNEL space */ 2190 dst = (u_char *) boot_addr + KERNBASE; 2191 2192 /* modify the lgdt arg */ 2193 dst32 = (u_int32_t *) (dst + ((u_int) & mp_gdtbase - boot_base)); 2194 *dst32 = boot_addr + ((u_int) & MP_GDT - boot_base); 2195 2196 /* modify the ljmp target for MPentry() */ 2197 dst32 = (u_int32_t *) (dst + ((u_int) bigJump - boot_base) + 1); 2198 *dst32 = ((u_int) MPentry - KERNBASE); 2199 2200 /* modify the target for boot code segment */ 2201 dst16 = (u_int16_t *) (dst + ((u_int) bootCodeSeg - boot_base)); 2202 dst8 = (u_int8_t *) (dst16 + 1); 2203 *dst16 = (u_int) boot_addr & 0xffff; 2204 *dst8 = ((u_int) boot_addr >> 16) & 0xff; 2205 2206 /* modify the target for boot data segment */ 2207 dst16 = (u_int16_t *) (dst + ((u_int) bootDataSeg - boot_base)); 2208 dst8 = (u_int8_t *) (dst16 + 1); 2209 *dst16 = (u_int) boot_addr & 0xffff; 2210 *dst8 = ((u_int) boot_addr >> 16) & 0xff; 2211} 2212 2213 2214/* 2215 * this function starts the AP (application processor) identified 2216 * by the APIC ID 'physicalCpu'. It does quite a "song and dance" 2217 * to accomplish this. This is necessary because of the nuances 2218 * of the different hardware we might encounter. It ain't pretty, 2219 * but it seems to work. 2220 */ 2221static int 2222start_ap(int logical_cpu, u_int boot_addr) 2223{ 2224 int physical_cpu; 2225 int vector; 2226 int cpus; 2227 u_long icr_lo, icr_hi; 2228 2229 POSTCODE(START_AP_POST); 2230 2231 /* get the PHYSICAL APIC ID# */ 2232 physical_cpu = CPU_TO_ID(logical_cpu); 2233 2234 /* calculate the vector */ 2235 vector = (boot_addr >> 12) & 0xff; 2236 2237 /* used as a watchpoint to signal AP startup */ 2238 cpus = mp_ncpus; 2239 2240 /* 2241 * first we do an INIT/RESET IPI this INIT IPI might be run, reseting 2242 * and running the target CPU. OR this INIT IPI might be latched (P5 2243 * bug), CPU waiting for STARTUP IPI. OR this INIT IPI might be 2244 * ignored. 2245 */ 2246 2247 /* setup the address for the target AP */ 2248 icr_hi = lapic.icr_hi & ~APIC_ID_MASK; 2249 icr_hi |= (physical_cpu << 24); 2250 lapic.icr_hi = icr_hi; 2251 2252 /* do an INIT IPI: assert RESET */ 2253 icr_lo = lapic.icr_lo & 0xfff00000; 2254 lapic.icr_lo = icr_lo | 0x0000c500; 2255 2256 /* wait for pending status end */ 2257 while (lapic.icr_lo & APIC_DELSTAT_MASK) 2258 /* spin */ ; 2259 2260 /* do an INIT IPI: deassert RESET */ 2261 lapic.icr_lo = icr_lo | 0x00008500; 2262 2263 /* wait for pending status end */ 2264 u_sleep(10000); /* wait ~10mS */ 2265 while (lapic.icr_lo & APIC_DELSTAT_MASK) 2266 /* spin */ ; 2267 2268 /* 2269 * next we do a STARTUP IPI: the previous INIT IPI might still be 2270 * latched, (P5 bug) this 1st STARTUP would then terminate 2271 * immediately, and the previously started INIT IPI would continue. OR 2272 * the previous INIT IPI has already run. and this STARTUP IPI will 2273 * run. OR the previous INIT IPI was ignored. and this STARTUP IPI 2274 * will run. 2275 */ 2276 2277 /* do a STARTUP IPI */ 2278 lapic.icr_lo = icr_lo | 0x00000600 | vector; 2279 while (lapic.icr_lo & APIC_DELSTAT_MASK) 2280 /* spin */ ; 2281 u_sleep(200); /* wait ~200uS */ 2282 2283 /* 2284 * finally we do a 2nd STARTUP IPI: this 2nd STARTUP IPI should run IF 2285 * the previous STARTUP IPI was cancelled by a latched INIT IPI. OR 2286 * this STARTUP IPI will be ignored, as only ONE STARTUP IPI is 2287 * recognized after hardware RESET or INIT IPI. 2288 */ 2289 2290 lapic.icr_lo = icr_lo | 0x00000600 | vector; 2291 while (lapic.icr_lo & APIC_DELSTAT_MASK) 2292 /* spin */ ; 2293 u_sleep(200); /* wait ~200uS */ 2294 2295 /* wait for it to start */ 2296 set_apic_timer(5000000);/* == 5 seconds */ 2297 while (read_apic_timer()) 2298 if (mp_ncpus > cpus) 2299 return 1; /* return SUCCESS */ 2300 2301 return 0; /* return FAILURE */ 2302} 2303 2304#if defined(APIC_IO) 2305 2306#ifdef COUNT_XINVLTLB_HITS 2307u_int xhits_gbl[MAXCPU]; 2308u_int xhits_pg[MAXCPU]; 2309u_int xhits_rng[MAXCPU]; 2310SYSCTL_NODE(_debug, OID_AUTO, xhits, CTLFLAG_RW, 0, ""); 2311SYSCTL_OPAQUE(_debug_xhits, OID_AUTO, global, CTLFLAG_RW, &xhits_gbl, 2312 sizeof(xhits_gbl), "IU", ""); 2313SYSCTL_OPAQUE(_debug_xhits, OID_AUTO, page, CTLFLAG_RW, &xhits_pg, 2314 sizeof(xhits_pg), "IU", ""); 2315SYSCTL_OPAQUE(_debug_xhits, OID_AUTO, range, CTLFLAG_RW, &xhits_rng, 2316 sizeof(xhits_rng), "IU", ""); 2317 2318u_int ipi_global; 2319u_int ipi_page; 2320u_int ipi_range; 2321u_int ipi_range_size; 2322SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_global, CTLFLAG_RW, &ipi_global, 0, ""); 2323SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_page, CTLFLAG_RW, &ipi_page, 0, ""); 2324SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_range, CTLFLAG_RW, &ipi_range, 0, ""); 2325SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_range_size, CTLFLAG_RW, &ipi_range_size, 2326 0, ""); 2327 2328u_int ipi_masked_global; 2329u_int ipi_masked_page; 2330u_int ipi_masked_range; 2331u_int ipi_masked_range_size; 2332SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_masked_global, CTLFLAG_RW, 2333 &ipi_masked_global, 0, ""); 2334SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_masked_page, CTLFLAG_RW, 2335 &ipi_masked_page, 0, ""); 2336SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_masked_range, CTLFLAG_RW, 2337 &ipi_masked_range, 0, ""); 2338SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_masked_range_size, CTLFLAG_RW, 2339 &ipi_masked_range_size, 0, ""); 2340#endif 2341 2342/* 2343 * Flush the TLB on all other CPU's 2344 */ 2345static void 2346smp_tlb_shootdown(u_int vector, vm_offset_t addr1, vm_offset_t addr2) 2347{ 2348 u_int ncpu; 2349 register_t eflags; 2350 2351 ncpu = mp_ncpus - 1; /* does not shootdown self */ 2352 if (ncpu < 1) 2353 return; /* no other cpus */ 2354 eflags = read_eflags(); 2355 if ((eflags & PSL_I) == 0) 2356 panic("absolutely cannot call smp_ipi_shootdown with interrupts already disabled"); 2357 mtx_lock_spin(&smp_tlb_mtx); 2358 smp_tlb_addr1 = addr1; 2359 smp_tlb_addr2 = addr2; 2360 atomic_store_rel_int(&smp_tlb_wait, 0); 2361 ipi_all_but_self(vector); 2362 while (smp_tlb_wait < ncpu) 2363 ia32_pause(); 2364 mtx_unlock_spin(&smp_tlb_mtx); 2365} 2366 2367/* 2368 * This is about as magic as it gets. fortune(1) has got similar code 2369 * for reversing bits in a word. Who thinks up this stuff?? 2370 * 2371 * Yes, it does appear to be consistently faster than: 2372 * while (i = ffs(m)) { 2373 * m >>= i; 2374 * bits++; 2375 * } 2376 * and 2377 * while (lsb = (m & -m)) { // This is magic too 2378 * m &= ~lsb; // or: m ^= lsb 2379 * bits++; 2380 * } 2381 * Both of these latter forms do some very strange things on gcc-3.1 with 2382 * -mcpu=pentiumpro and/or -march=pentiumpro and/or -O or -O2. 2383 * There is probably an SSE or MMX popcnt instruction. 2384 * 2385 * I wonder if this should be in libkern? 2386 * 2387 * XXX Stop the presses! Another one: 2388 * static __inline u_int32_t 2389 * popcnt1(u_int32_t v) 2390 * { 2391 * v -= ((v >> 1) & 0x55555555); 2392 * v = (v & 0x33333333) + ((v >> 2) & 0x33333333); 2393 * v = (v + (v >> 4)) & 0x0F0F0F0F; 2394 * return (v * 0x01010101) >> 24; 2395 * } 2396 * The downside is that it has a multiply. With a pentium3 with 2397 * -mcpu=pentiumpro and -march=pentiumpro then gcc-3.1 will use 2398 * an imull, and in that case it is faster. In most other cases 2399 * it appears slightly slower. 2400 */ 2401static __inline u_int32_t 2402popcnt(u_int32_t m) 2403{ 2404 2405 m = (m & 0x55555555) + ((m & 0xaaaaaaaa) >> 1); 2406 m = (m & 0x33333333) + ((m & 0xcccccccc) >> 2); 2407 m = (m & 0x0f0f0f0f) + ((m & 0xf0f0f0f0) >> 4); 2408 m = (m & 0x00ff00ff) + ((m & 0xff00ff00) >> 8); 2409 m = (m & 0x0000ffff) + ((m & 0xffff0000) >> 16); 2410 return m; 2411} 2412 2413static void 2414smp_targeted_tlb_shootdown(u_int mask, u_int vector, vm_offset_t addr1, vm_offset_t addr2) 2415{ 2416 int ncpu, othercpus; 2417 register_t eflags; 2418 2419 othercpus = mp_ncpus - 1; 2420 if (mask == (u_int)-1) { 2421 ncpu = othercpus; 2422 if (ncpu < 1) 2423 return; 2424 } else { 2425 /* XXX there should be a pcpu self mask */ 2426 mask &= ~(1 << PCPU_GET(cpuid)); 2427 if (mask == 0) 2428 return; 2429 ncpu = popcnt(mask); 2430 if (ncpu > othercpus) { 2431 /* XXX this should be a panic offence */ 2432 printf("SMP: tlb shootdown to %d other cpus (only have %d)\n", 2433 ncpu, othercpus); 2434 ncpu = othercpus; 2435 } 2436 /* XXX should be a panic, implied by mask == 0 above */ 2437 if (ncpu < 1) 2438 return; 2439 } 2440 eflags = read_eflags(); 2441 if ((eflags & PSL_I) == 0) 2442 panic("absolutely cannot call smp_targeted_ipi_shootdown with interrupts already disabled"); 2443 mtx_lock_spin(&smp_tlb_mtx); 2444 smp_tlb_addr1 = addr1; 2445 smp_tlb_addr2 = addr2; 2446 atomic_store_rel_int(&smp_tlb_wait, 0); 2447 if (mask == (u_int)-1) 2448 ipi_all_but_self(vector); 2449 else 2450 ipi_selected(mask, vector); 2451 while (smp_tlb_wait < ncpu) 2452 ia32_pause(); 2453 mtx_unlock_spin(&smp_tlb_mtx); 2454} 2455#endif 2456 2457void 2458smp_invltlb(void) 2459{ 2460#if defined(APIC_IO) 2461 if (smp_started) { 2462 smp_tlb_shootdown(IPI_INVLTLB, 0, 0); 2463#ifdef COUNT_XINVLTLB_HITS 2464 ipi_global++; 2465#endif 2466 } 2467#endif /* APIC_IO */ 2468} 2469 2470void 2471smp_invlpg(vm_offset_t addr) 2472{ 2473#if defined(APIC_IO) 2474 if (smp_started) { 2475 smp_tlb_shootdown(IPI_INVLPG, addr, 0); 2476#ifdef COUNT_XINVLTLB_HITS 2477 ipi_page++; 2478#endif 2479 } 2480#endif /* APIC_IO */ 2481} 2482 2483void 2484smp_invlpg_range(vm_offset_t addr1, vm_offset_t addr2) 2485{ 2486#if defined(APIC_IO) 2487 if (smp_started) { 2488 smp_tlb_shootdown(IPI_INVLRNG, addr1, addr2); 2489#ifdef COUNT_XINVLTLB_HITS 2490 ipi_range++; 2491 ipi_range_size += (addr2 - addr1) / PAGE_SIZE; 2492#endif 2493 } 2494#endif /* APIC_IO */ 2495} 2496 2497void 2498smp_masked_invltlb(u_int mask) 2499{ 2500#if defined(APIC_IO) 2501 if (smp_started) { 2502 smp_targeted_tlb_shootdown(mask, IPI_INVLTLB, 0, 0); 2503#ifdef COUNT_XINVLTLB_HITS 2504 ipi_masked_global++; 2505#endif 2506 } 2507#endif /* APIC_IO */ 2508} 2509 2510void 2511smp_masked_invlpg(u_int mask, vm_offset_t addr) 2512{ 2513#if defined(APIC_IO) 2514 if (smp_started) { 2515 smp_targeted_tlb_shootdown(mask, IPI_INVLPG, addr, 0); 2516#ifdef COUNT_XINVLTLB_HITS 2517 ipi_masked_page++; 2518#endif 2519 } 2520#endif /* APIC_IO */ 2521} 2522 2523void 2524smp_masked_invlpg_range(u_int mask, vm_offset_t addr1, vm_offset_t addr2) 2525{ 2526#if defined(APIC_IO) 2527 if (smp_started) { 2528 smp_targeted_tlb_shootdown(mask, IPI_INVLRNG, addr1, addr2); 2529#ifdef COUNT_XINVLTLB_HITS 2530 ipi_masked_range++; 2531 ipi_masked_range_size += (addr2 - addr1) / PAGE_SIZE; 2532#endif 2533 } 2534#endif /* APIC_IO */ 2535} 2536 2537 2538/* 2539 * This is called once the rest of the system is up and running and we're 2540 * ready to let the AP's out of the pen. 2541 */ 2542void 2543ap_init(void) 2544{ 2545 u_int apic_id; 2546 2547 /* spin until all the AP's are ready */ 2548 while (!aps_ready) 2549 ia32_pause(); 2550 2551 /* BSP may have changed PTD while we were waiting */ 2552 invltlb(); 2553 2554#if defined(I586_CPU) && !defined(NO_F00F_HACK) 2555 lidt(&r_idt); 2556#endif 2557 2558 /* set up CPU registers and state */ 2559 cpu_setregs(); 2560 2561 /* set up FPU state on the AP */ 2562 npxinit(__INITIAL_NPXCW__); 2563 2564 /* set up SSE registers */ 2565 enable_sse(); 2566 2567 /* A quick check from sanity claus */ 2568 apic_id = (apic_id_to_logical[(lapic.id & 0x0f000000) >> 24]); 2569 if (PCPU_GET(cpuid) != apic_id) { 2570 printf("SMP: cpuid = %d\n", PCPU_GET(cpuid)); 2571 printf("SMP: apic_id = %d\n", apic_id); 2572 printf("PTD[MPPTDI] = %#jx\n", (uintmax_t)PTD[MPPTDI]); 2573 panic("cpuid mismatch! boom!!"); 2574 } 2575 2576 /* Init local apic for irq's */ 2577 apic_initialize(); 2578 2579 /* Set memory range attributes for this CPU to match the BSP */ 2580 mem_range_AP_init(); 2581 2582 mtx_lock_spin(&ap_boot_mtx); 2583 2584 smp_cpus++; 2585 2586 CTR1(KTR_SMP, "SMP: AP CPU #%d Launched", PCPU_GET(cpuid)); 2587 printf("SMP: AP CPU #%d Launched!\n", PCPU_GET(cpuid)); 2588 2589 /* Build our map of 'other' CPUs. */ 2590 PCPU_SET(other_cpus, all_cpus & ~PCPU_GET(cpumask)); 2591 2592 if (bootverbose) 2593 apic_dump("ap_init()"); 2594 2595 if (smp_cpus == mp_ncpus) { 2596 /* enable IPI's, tlb shootdown, freezes etc */ 2597 atomic_store_rel_int(&smp_started, 1); 2598 smp_active = 1; /* historic */ 2599 } 2600 2601 mtx_unlock_spin(&ap_boot_mtx); 2602 2603 /* wait until all the AP's are up */ 2604 while (smp_started == 0) 2605 ia32_pause(); 2606 2607 /* ok, now grab sched_lock and enter the scheduler */ 2608 mtx_lock_spin(&sched_lock); 2609 2610 binuptime(PCPU_PTR(switchtime)); 2611 PCPU_SET(switchticks, ticks); 2612 2613 cpu_throw(NULL, choosethread()); /* doesn't return */ 2614 2615 panic("scheduler returned us to %s", __func__); 2616} 2617 2618/* 2619 * For statclock, we send an IPI to all CPU's to have them call this 2620 * function. 2621 * 2622 * WARNING! unpend() will call statclock() directly and skip this 2623 * routine. 2624 */ 2625void 2626forwarded_statclock(struct clockframe frame) 2627{ 2628 2629 if (profprocs != 0) 2630 profclock(&frame); 2631 if (pscnt == psdiv) 2632 statclock(&frame); 2633} 2634 2635void 2636forward_statclock(void) 2637{ 2638 int map; 2639 2640 CTR0(KTR_SMP, "forward_statclock"); 2641 2642 if (!smp_started || cold || panicstr) 2643 return; 2644 2645 map = PCPU_GET(other_cpus) & ~stopped_cpus ; 2646 if (map != 0) 2647 ipi_selected(map, IPI_STATCLOCK); 2648} 2649 2650/* 2651 * For each hardclock(), we send an IPI to all other CPU's to have them 2652 * execute this function. It would be nice to reduce contention on 2653 * sched_lock if we could simply peek at the CPU to determine the user/kernel 2654 * state and call hardclock_process() on the CPU receiving the clock interrupt 2655 * and then just use a simple IPI to handle any ast's if needed. 2656 * 2657 * WARNING! unpend() will call hardclock_process() directly and skip this 2658 * routine. 2659 */ 2660void 2661forwarded_hardclock(struct clockframe frame) 2662{ 2663 2664 hardclock_process(&frame); 2665} 2666 2667void 2668forward_hardclock(void) 2669{ 2670 u_int map; 2671 2672 CTR0(KTR_SMP, "forward_hardclock"); 2673 2674 if (!smp_started || cold || panicstr) 2675 return; 2676 2677 map = PCPU_GET(other_cpus) & ~stopped_cpus ; 2678 if (map != 0) 2679 ipi_selected(map, IPI_HARDCLOCK); 2680} 2681 2682#ifdef APIC_INTR_REORDER 2683/* 2684 * Maintain mapping from softintr vector to isr bit in local apic. 2685 */ 2686void 2687set_lapic_isrloc(int intr, int vector) 2688{ 2689 if (intr < 0 || intr > 32) 2690 panic("set_apic_isrloc: bad intr argument: %d",intr); 2691 if (vector < ICU_OFFSET || vector > 255) 2692 panic("set_apic_isrloc: bad vector argument: %d",vector); 2693 apic_isrbit_location[intr].location = &lapic.isr0 + ((vector>>5)<<2); 2694 apic_isrbit_location[intr].bit = (1<<(vector & 31)); 2695} 2696#endif 2697 2698/* 2699 * send an IPI to a set of cpus. 2700 */ 2701void 2702ipi_selected(u_int32_t cpus, u_int ipi) 2703{ 2704 2705 CTR3(KTR_SMP, "%s: cpus: %x ipi: %x", __func__, cpus, ipi); 2706 selected_apic_ipi(cpus, ipi, APIC_DELMODE_FIXED); 2707} 2708 2709/* 2710 * send an IPI INTerrupt containing 'vector' to all CPUs, including myself 2711 */ 2712void 2713ipi_all(u_int ipi) 2714{ 2715 2716 CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi); 2717 apic_ipi(APIC_DEST_ALLISELF, ipi, APIC_DELMODE_FIXED); 2718} 2719 2720/* 2721 * send an IPI to all CPUs EXCEPT myself 2722 */ 2723void 2724ipi_all_but_self(u_int ipi) 2725{ 2726 2727 CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi); 2728 apic_ipi(APIC_DEST_ALLESELF, ipi, APIC_DELMODE_FIXED); 2729} 2730 2731/* 2732 * send an IPI to myself 2733 */ 2734void 2735ipi_self(u_int ipi) 2736{ 2737 2738 CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi); 2739 apic_ipi(APIC_DEST_SELF, ipi, APIC_DELMODE_FIXED); 2740} 2741 2742static void 2743release_aps(void *dummy __unused) 2744{ 2745 2746 if (mp_ncpus == 1) 2747 return; 2748 mtx_lock_spin(&sched_lock); 2749 atomic_store_rel_int(&aps_ready, 1); 2750 while (smp_started == 0) 2751 ia32_pause(); 2752 mtx_unlock_spin(&sched_lock); 2753} 2754 2755SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL); 2756 2757static int hlt_cpus_mask; 2758static int hlt_logical_cpus = 1; 2759static struct sysctl_ctx_list logical_cpu_clist; 2760 2761static int 2762sysctl_hlt_cpus(SYSCTL_HANDLER_ARGS) 2763{ 2764 u_int mask; 2765 int error; 2766 2767 mask = hlt_cpus_mask; 2768 error = sysctl_handle_int(oidp, &mask, 0, req); 2769 if (error || !req->newptr) 2770 return (error); 2771 2772 if (logical_cpus_mask != 0 && 2773 (mask & logical_cpus_mask) == logical_cpus_mask) 2774 hlt_logical_cpus = 1; 2775 else 2776 hlt_logical_cpus = 0; 2777 2778 if ((mask & all_cpus) == all_cpus) 2779 mask &= ~(1<<0); 2780 hlt_cpus_mask = mask; 2781 return (error); 2782} 2783SYSCTL_PROC(_machdep, OID_AUTO, hlt_cpus, CTLTYPE_INT|CTLFLAG_RW, 2784 0, 0, sysctl_hlt_cpus, "IU", ""); 2785 2786static int 2787sysctl_hlt_logical_cpus(SYSCTL_HANDLER_ARGS) 2788{ 2789 int disable, error; 2790 2791 disable = hlt_logical_cpus; 2792 error = sysctl_handle_int(oidp, &disable, 0, req); 2793 if (error || !req->newptr) 2794 return (error); 2795 2796 if (disable) 2797 hlt_cpus_mask |= logical_cpus_mask; 2798 else 2799 hlt_cpus_mask &= ~logical_cpus_mask; 2800 2801 if ((hlt_cpus_mask & all_cpus) == all_cpus) 2802 hlt_cpus_mask &= ~(1<<0); 2803 2804 hlt_logical_cpus = disable; 2805 return (error); 2806} 2807 2808static void 2809cpu_hlt_setup(void *dummy __unused) 2810{ 2811 2812 if (logical_cpus_mask != 0) { 2813 TUNABLE_INT_FETCH("machdep.hlt_logical_cpus", 2814 &hlt_logical_cpus); 2815 sysctl_ctx_init(&logical_cpu_clist); 2816 SYSCTL_ADD_PROC(&logical_cpu_clist, 2817 SYSCTL_STATIC_CHILDREN(_machdep), OID_AUTO, 2818 "hlt_logical_cpus", CTLTYPE_INT|CTLFLAG_RW, 0, 0, 2819 sysctl_hlt_logical_cpus, "IU", ""); 2820 SYSCTL_ADD_UINT(&logical_cpu_clist, 2821 SYSCTL_STATIC_CHILDREN(_machdep), OID_AUTO, 2822 "logical_cpus_mask", CTLTYPE_INT|CTLFLAG_RD, 2823 &logical_cpus_mask, 0, ""); 2824 2825 if (hlt_logical_cpus) 2826 hlt_cpus_mask |= logical_cpus_mask; 2827 } 2828} 2829SYSINIT(cpu_hlt, SI_SUB_SMP, SI_ORDER_ANY, cpu_hlt_setup, NULL); 2830 2831int 2832mp_grab_cpu_hlt(void) 2833{ 2834 u_int mask = PCPU_GET(cpumask); 2835 int retval; 2836 2837 retval = mask & hlt_cpus_mask; 2838 while (mask & hlt_cpus_mask) 2839 __asm __volatile("sti; hlt" : : : "memory"); 2840 return (retval); 2841}
|