mptable.c revision 28669
190075Sobrien/* 2169689Skan * Copyright (c) 1996, by Steve Passe 3169689Skan * All rights reserved. 490075Sobrien * 590075Sobrien * Redistribution and use in source and binary forms, with or without 6132718Skan * modification, are permitted provided that the following conditions 790075Sobrien * are met: 8132718Skan * 1. Redistributions of source code must retain the above copyright 990075Sobrien * notice, this list of conditions and the following disclaimer. 1090075Sobrien * 2. The name of the developer may NOT be used to endorse or promote products 1190075Sobrien * derived from this software without specific prior written permission. 1290075Sobrien * 13132718Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1490075Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1590075Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1690075Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1790075Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1890075Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19132718Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21169689Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2290075Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2390075Sobrien * SUCH DAMAGE. 2490075Sobrien * 25132718Skan * $Id: mp_machdep.c,v 1.30 1997/08/24 20:19:47 smp Exp smp $ 26132718Skan */ 2790075Sobrien 2890075Sobrien#include "opt_smp.h" 2990075Sobrien 3090075Sobrien#include <sys/param.h> /* for KERNBASE */ 31132718Skan#include <sys/systm.h> 3290075Sobrien 33132718Skan#include <vm/vm.h> /* for KERNBASE */ 3490075Sobrien#include <vm/vm_param.h> /* for KERNBASE */ 3590075Sobrien#include <vm/pmap.h> /* for KERNBASE */ 3690075Sobrien#include <vm/vm_kern.h> 3790075Sobrien#include <vm/vm_extern.h> 3890075Sobrien 39132718Skan#include <machine/smp.h> 4090075Sobrien#include <machine/apic.h> 4190075Sobrien#include <machine/mpapic.h> 42169689Skan#include <machine/segments.h> 4390075Sobrien#include <machine/smptests.h> /** TEST_DEFAULT_CONFIG, TEST_TEST1 */ 44169689Skan#include <machine/tss.h> 4590075Sobrien#include <machine/specialreg.h> 46169689Skan 4790075Sobrien#include <i386/i386/cons.h> /* cngetc() */ 4890075Sobrien 4990075Sobrien#if defined(APIC_IO) 50117395Skan#include <machine/md_var.h> /* setidt() */ 5190075Sobrien#include <i386/isa/icu.h> /* IPIs */ 5290075Sobrien#include <i386/isa/intr_machdep.h> /* IPIs */ 53132718Skan#endif /* APIC_IO */ 5490075Sobrien 5590075Sobrien#if defined(TEST_DEFAULT_CONFIG) 5690075Sobrien#define MPFPS_MPFB1 TEST_DEFAULT_CONFIG 5790075Sobrien#else 5890075Sobrien#define MPFPS_MPFB1 mpfps->mpfb1 5990075Sobrien#endif /* TEST_DEFAULT_CONFIG */ 6090075Sobrien 6190075Sobrien#define WARMBOOT_TARGET 0 6290075Sobrien#define WARMBOOT_OFF (KERNBASE + 0x0467) 6390075Sobrien#define WARMBOOT_SEG (KERNBASE + 0x0469) 6490075Sobrien 6590075Sobrien#define BIOS_BASE (0xf0000) 6690075Sobrien#define BIOS_SIZE (0x10000) 6790075Sobrien#define BIOS_COUNT (BIOS_SIZE/4) 68169689Skan 6990075Sobrien#define CMOS_REG (0x70) 7090075Sobrien#define CMOS_DATA (0x71) 7190075Sobrien#define BIOS_RESET (0x0f) 7290075Sobrien#define BIOS_WARM (0x0a) 7390075Sobrien 7490075Sobrien#define PROCENTRY_FLAG_EN 0x01 7590075Sobrien#define PROCENTRY_FLAG_BP 0x02 7690075Sobrien#define IOAPICENTRY_FLAG_EN 0x01 7790075Sobrien 7890075Sobrien 7990075Sobrien/* MP Floating Pointer Structure */ 8090075Sobrientypedef struct MPFPS { 8190075Sobrien char signature[4]; 8290075Sobrien void *pap; 8390075Sobrien u_char length; 8490075Sobrien u_char spec_rev; 8590075Sobrien u_char checksum; 8690075Sobrien u_char mpfb1; 8790075Sobrien u_char mpfb2; 8890075Sobrien u_char mpfb3; 8990075Sobrien u_char mpfb4; 9090075Sobrien u_char mpfb5; 9190075Sobrien} *mpfps_t; 9290075Sobrien 9390075Sobrien/* MP Configuration Table Header */ 94169689Skantypedef struct MPCTH { 9590075Sobrien char signature[4]; 96169689Skan u_short base_table_length; 9790075Sobrien u_char spec_rev; 9890075Sobrien u_char checksum; 9990075Sobrien u_char oem_id[8]; 100169689Skan u_char product_id[12]; 10190075Sobrien void *oem_table_pointer; 102169689Skan u_short oem_table_size; 10390075Sobrien u_short entry_count; 10490075Sobrien void *apic_address; 10590075Sobrien u_short extended_table_length; 106169689Skan u_char extended_table_checksum; 10790075Sobrien u_char reserved; 108169689Skan} *mpcth_t; 10990075Sobrien 11090075Sobrien 11190075Sobrientypedef struct PROCENTRY { 112169689Skan u_char type; 11390075Sobrien u_char apic_id; 114169689Skan u_char apic_version; 11590075Sobrien u_char cpu_flags; 11690075Sobrien u_long cpu_signature; 11790075Sobrien u_long feature_flags; 118169689Skan u_long reserved1; 11990075Sobrien u_long reserved2; 120169689Skan} *proc_entry_ptr; 12190075Sobrien 12290075Sobrientypedef struct BUSENTRY { 12390075Sobrien u_char type; 124169689Skan u_char bus_id; 12590075Sobrien char bus_type[6]; 126169689Skan} *bus_entry_ptr; 12790075Sobrien 12890075Sobrientypedef struct IOAPICENTRY { 12990075Sobrien u_char type; 130169689Skan u_char apic_id; 13190075Sobrien u_char apic_version; 132169689Skan u_char apic_flags; 13390075Sobrien void *apic_address; 13490075Sobrien} *io_apic_entry_ptr; 13590075Sobrien 136169689Skantypedef struct INTENTRY { 13790075Sobrien u_char type; 138169689Skan u_char int_type; 13990075Sobrien u_short int_flags; 14090075Sobrien u_char src_bus_id; 14190075Sobrien u_char src_bus_irq; 142169689Skan u_char dst_apic_id; 14390075Sobrien u_char dst_apic_int; 144169689Skan} *int_entry_ptr; 14590075Sobrien 14690075Sobrien/* descriptions of MP basetable entries */ 14790075Sobrientypedef struct BASETABLE_ENTRY { 148169689Skan u_char type; 14990075Sobrien u_char length; 150169689Skan char name[16]; 15190075Sobrien} basetable_entry; 15290075Sobrien 15390075Sobrien/* 15490075Sobrien * this code MUST be enabled here and in mpboot.s. 15590075Sobrien * it follows the very early stages of AP boot by placing values in CMOS ram. 15690075Sobrien * it NORMALLY will never be needed and thus the primitive method for enabling. 15790075Sobrien * 15890075Sobrien#define CHECK_POINTS 15990075Sobrien */ 16090075Sobrien 16190075Sobrien#if defined(CHECK_POINTS) 16290075Sobrien#define CHECK_READ(A) (outb(CMOS_REG, (A)), inb(CMOS_DATA)) 16390075Sobrien#define CHECK_WRITE(A,D) (outb(CMOS_REG, (A)), outb(CMOS_DATA, (D))) 16490075Sobrien 16590075Sobrien#define CHECK_INIT(D); \ 16690075Sobrien CHECK_WRITE(0x34, (D)); \ 16790075Sobrien CHECK_WRITE(0x35, (D)); \ 16890075Sobrien CHECK_WRITE(0x36, (D)); \ 16990075Sobrien CHECK_WRITE(0x37, (D)); \ 17090075Sobrien CHECK_WRITE(0x38, (D)); \ 17190075Sobrien CHECK_WRITE(0x39, (D)); 17290075Sobrien 17390075Sobrien#define CHECK_PRINT(S); \ 17490075Sobrien printf("%s: %d, %d, %d, %d, %d, %d\n", \ 17590075Sobrien (S), \ 17690075Sobrien CHECK_READ(0x34), \ 17790075Sobrien CHECK_READ(0x35), \ 17890075Sobrien CHECK_READ(0x36), \ 17990075Sobrien CHECK_READ(0x37), \ 18090075Sobrien CHECK_READ(0x38), \ 18190075Sobrien CHECK_READ(0x39)); 18290075Sobrien 18390075Sobrien#else /* CHECK_POINTS */ 18490075Sobrien 18590075Sobrien#define CHECK_INIT(D) 18690075Sobrien#define CHECK_PRINT(S) 18790075Sobrien 18890075Sobrien#endif /* CHECK_POINTS */ 189169689Skan 19090075Sobrien/* 19190075Sobrien * Values to send to the POST hardware. 19290075Sobrien */ 19390075Sobrien#define MP_BOOTADDRESS_POST 0x10 19490075Sobrien#define MP_PROBE_POST 0x11 19590075Sobrien#define MP_START_POST 0x12 19690075Sobrien#define MP_ANNOUNCE_POST 0x13 19790075Sobrien#define MPTABLE_PASS1_POST 0x14 19890075Sobrien#define MPTABLE_PASS2_POST 0x15 199169689Skan#define MP_ENABLE_POST 0x16 20090075Sobrien#define START_ALL_APS_POST 0x17 20190075Sobrien#define INSTALL_AP_TRAMP_POST 0x18 20290075Sobrien#define START_AP_POST 0x19 20390075Sobrien 20490075Sobrien/** XXX FIXME: where does this really belong, isa.h/isa.c perhaps? */ 20590075Sobrienint current_postcode; 206132718Skan 207132718Skan/** XXX FIXME: what system files declare these??? */ 20890075Sobrienextern struct region_descriptor r_gdt, r_idt; 20990075Sobrien 21090075Sobrienint bsp_apic_ready = 0; /* flags useability of BSP apic */ 21190075Sobrienint mp_ncpus; /* # of CPUs, including BSP */ 21290075Sobrienint mp_naps; /* # of Applications processors */ 21390075Sobrienint mp_nbusses; /* # of busses */ 21490075Sobrienint mp_napics; /* # of IO APICs */ 21590075Sobrienint boot_cpu_id; /* designated BSP */ 21690075Sobrienvm_offset_t cpu_apic_address; 21790075Sobrienvm_offset_t io_apic_address[NAPICID]; /* NAPICID is more than enough */ 218169689Skan 21990075Sobrienu_int32_t cpu_apic_versions[NCPU]; 22090075Sobrienu_int32_t io_apic_versions[NAPIC]; 22190075Sobrien 22290075Sobrien/* 22390075Sobrien * APIC ID logical/physical mapping structures. 22490075Sobrien * We oversize these to simplify boot-time config. 22590075Sobrien */ 226169689Skanint cpu_num_to_apic_id[NAPICID]; 227132718Skanint io_num_to_apic_id[NAPICID]; 22890075Sobrienint apic_id_to_logical[NAPICID]; 22990075Sobrien 23090075Sobrien/* Bitmap of all available CPUs */ 23190075Sobrienu_int all_cpus; 232132718Skan 23390075Sobrien/* Boot of AP uses this PTD */ 23490075Sobrienu_int *bootPTD; 23590075Sobrien 236132718Skan/* Hotwire a 0->4MB V==P mapping */ 237169689Skanextern pt_entry_t KPTphys; 238132718Skan 239132718Skan/* Virtual address of per-cpu common_tss */ 240132718Skanextern struct i386tss common_tss; 24190075Sobrien 24290075Sobrien/* 24390075Sobrien * Local data and functions. 24490075Sobrien */ 245169689Skan 24690075Sobrienstatic int mp_capable; 24790075Sobrienstatic u_int boot_address; 248132718Skanstatic u_int base_memory; 24990075Sobrien 250132718Skanstatic int picmode; /* 0: virtual wire mode, 1: PIC mode */ 25190075Sobrienstatic mpfps_t mpfps; 252132718Skanstatic int search_for_sig(u_int32_t target, int count); 253117395Skanstatic void mp_enable(u_int boot_addr); 254117395Skan 255117395Skanstatic int mptable_pass1(void); 256117395Skanstatic int mptable_pass2(void); 257117395Skanstatic void default_mp_table(int type); 258132718Skanstatic void fix_mp_table(void); 259117395Skanstatic void init_locks(void); 260169689Skanstatic int start_all_aps(u_int boot_addr); 261132718Skanstatic void install_ap_tramp(u_int boot_addr); 262132718Skanstatic int start_ap(int logicalCpu, u_int boot_addr); 263132718Skan 26490075Sobrien 26590075Sobrien/* 266169689Skan * Calculate usable address in base memory for AP trampoline code. 26790075Sobrien */ 268169689Skanu_int 26990075Sobrienmp_bootaddress(u_int basemem) 27090075Sobrien{ 271169689Skan POSTCODE(MP_BOOTADDRESS_POST); 272169689Skan 273169689Skan base_memory = basemem * 1024; /* convert to bytes */ 274169689Skan 275169689Skan boot_address = base_memory & ~0xfff; /* round down to 4k boundary */ 27690075Sobrien if ((base_memory - boot_address) < bootMP_size) 27790075Sobrien boot_address -= 4096; /* not enough, lower by 4k */ 278169689Skan 279169689Skan return boot_address; 28090075Sobrien} 28190075Sobrien 28290075Sobrien 28390075Sobrien/* 28490075Sobrien * Look for an Intel MP spec table (ie, SMP capable hardware). 28590075Sobrien */ 28690075Sobrienint 28790075Sobrienmp_probe(void) 288169689Skan{ 28990075Sobrien int x; 29090075Sobrien u_long segment; 29190075Sobrien u_int32_t target; 29290075Sobrien 293169689Skan POSTCODE(MP_PROBE_POST); 29490075Sobrien 295169689Skan /* see if EBDA exists */ 296169689Skan if (segment = (u_long) * (u_short *) (KERNBASE + 0x40e)) { 29790075Sobrien /* search first 1K of EBDA */ 29890075Sobrien target = (u_int32_t) (segment << 4); 29990075Sobrien if ((x = search_for_sig(target, 1024 / 4)) >= 0) 30090075Sobrien goto found; 301169689Skan } else { 302169689Skan /* last 1K of base memory, effective 'top of base' passed in */ 303169689Skan target = (u_int32_t) (base_memory - 0x400); 304169689Skan if ((x = search_for_sig(target, 1024 / 4)) >= 0) 305169689Skan goto found; 30690075Sobrien } 307169689Skan 30890075Sobrien /* search the BIOS */ 30990075Sobrien target = (u_int32_t) BIOS_BASE; 310169689Skan if ((x = search_for_sig(target, BIOS_COUNT)) >= 0) 311169689Skan goto found; 312169689Skan 313169689Skan /* nothing found */ 31490075Sobrien mpfps = (mpfps_t)0; 315169689Skan mp_capable = 0; 31690075Sobrien return 0; 317169689Skan 31890075Sobrienfound: 319169689Skan /* calculate needed resources */ 320132718Skan mpfps = (mpfps_t)x; 321169689Skan if (mptable_pass1()) 322132718Skan panic("you must reconfigure your kernel"); 323169689Skan 32490075Sobrien /* flag fact that we are running multiple processors */ 325169689Skan mp_capable = 1; 32690075Sobrien return 1; 32790075Sobrien} 32890075Sobrien 329132718Skan 330169689Skan/* 331169689Skan * Startup the SMP processors. 332132718Skan */ 333169689Skanvoid 334132718Skanmp_start(void) 335132718Skan{ 336169689Skan POSTCODE(MP_START_POST); 337132718Skan 338132718Skan /* look for MP capable motherboard */ 339132718Skan if (mp_capable) 340132718Skan mp_enable(boot_address); 341132718Skan else 342132718Skan panic("MP hardware not found!"); 34390075Sobrien} 34490075Sobrien 34590075Sobrien 34690075Sobrien/* 34790075Sobrien * Print various information about the SMP system hardware and setup. 34890075Sobrien */ 34990075Sobrienvoid 35090075Sobrienmp_announce(void) 35190075Sobrien{ 35290075Sobrien int x; 35390075Sobrien 35490075Sobrien POSTCODE(MP_ANNOUNCE_POST); 35590075Sobrien 35690075Sobrien printf("FreeBSD/SMP: Multiprocessor motherboard\n"); 35790075Sobrien printf(" cpu0 (BSP): apic id: %2d", CPU_TO_ID(0)); 35890075Sobrien printf(", version: 0x%08x", cpu_apic_versions[0]); 35990075Sobrien printf(", at 0x%08x\n", cpu_apic_address); 36090075Sobrien for (x = 1; x <= mp_naps; ++x) { 36190075Sobrien printf(" cpu%d (AP): apic id: %2d", x, CPU_TO_ID(x)); 36290075Sobrien printf(", version: 0x%08x", cpu_apic_versions[x]); 36390075Sobrien printf(", at 0x%08x\n", cpu_apic_address); 36490075Sobrien } 36590075Sobrien 36690075Sobrien#if defined(APIC_IO) 36790075Sobrien for (x = 0; x < mp_napics; ++x) { 36890075Sobrien printf(" io%d (APIC): apic id: %2d", x, IO_TO_ID(x)); 369169689Skan printf(", version: 0x%08x", io_apic_versions[x]); 37090075Sobrien printf(", at 0x%08x\n", io_apic_address[x]); 37190075Sobrien } 37290075Sobrien#else 37390075Sobrien printf(" Warning: APIC I/O disabled\n"); 37490075Sobrien#endif /* APIC_IO */ 37590075Sobrien} 37690075Sobrien 37790075Sobrien/* 37890075Sobrien * AP cpu's call this to sync up protected mode. 37990075Sobrien */ 38090075Sobrienvoid 38190075Sobrieninit_secondary(void) 38290075Sobrien{ 38390075Sobrien int gsel_tss, slot; 38490075Sobrien 38590075Sobrien r_gdt.rd_limit = sizeof(gdt[0]) * (NGDT + NCPU) - 1; 38690075Sobrien r_gdt.rd_base = (int) gdt; 38790075Sobrien lgdt(&r_gdt); /* does magic intra-segment return */ 38890075Sobrien lidt(&r_idt); 38990075Sobrien lldt(_default_ldt); 39090075Sobrien 39190075Sobrien slot = NGDT + cpuid; 39290075Sobrien gsel_tss = GSEL(slot, SEL_KPL); 39390075Sobrien gdt[slot].sd.sd_type = SDT_SYS386TSS; 39490075Sobrien common_tss.tss_esp0 = 0; /* not used until after switch */ 39590075Sobrien common_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL); 396169689Skan common_tss.tss_ioopt = (sizeof common_tss) << 16; 39790075Sobrien ltr(gsel_tss); 39890075Sobrien 39990075Sobrien load_cr0(0x8005003b); /* XXX! */ 40090075Sobrien 40190075Sobrien PTD[0] = 0; 40290075Sobrien pmap_set_opt((unsigned *)PTD); 40390075Sobrien 40490075Sobrien invltlb(); 40590075Sobrien} 40690075Sobrien 40790075Sobrien 40890075Sobrien#if defined(APIC_IO) 40990075Sobrien/* 41090075Sobrien * Final configuration of the BSP's local APIC: 41190075Sobrien * - disable 'pic mode'. 412169689Skan * - disable 'virtual wire mode'. 413169689Skan * - enable NMI. 414169689Skan */ 415169689Skanvoid 416169689Skanbsp_apic_configure(void) 417169689Skan{ 418169689Skan u_char byte; 419169689Skan u_int32_t temp; 420169689Skan 421169689Skan /* leave 'pic mode' if necessary */ 422169689Skan if (picmode) { 423169689Skan outb(0x22, 0x70); /* select IMCR */ 424169689Skan byte = inb(0x23); /* current contents */ 425169689Skan byte |= 0x01; /* mask external INTR */ 426169689Skan outb(0x23, byte); /* disconnect 8259s/NMI */ 427169689Skan } 428169689Skan 429169689Skan /* mask lint0 (the 8259 'virtual wire' connection) */ 430169689Skan temp = lapic.lvt_lint0; 431169689Skan temp |= APIC_LVT_M; /* set the mask */ 432169689Skan lapic.lvt_lint0 = temp; 433169689Skan 434169689Skan /* setup lint1 to handle NMI */ 435169689Skan temp = lapic.lvt_lint1; 436260919Spfg temp &= ~APIC_LVT_M; /* clear the mask */ 437260919Spfg lapic.lvt_lint1 = temp; 438260919Spfg 439169689Skan if (bootverbose) 440169689Skan apic_dump("bsp_apic_configure()"); 441169689Skan} 442169689Skan#endif /* APIC_IO */ 443169689Skan 444169689Skan 445169689Skan/******************************************************************* 446169689Skan * local functions and data 447260919Spfg */ 448260919Spfg 449260919Spfg/* 450169689Skan * start the SMP system 451169689Skan */ 452169689Skanstatic void 453169689Skanmp_enable(u_int boot_addr) 454169689Skan{ 455169689Skan int x; 456169689Skan#if defined(APIC_IO) 457169689Skan int apic; 458169689Skan u_int ux; 459169689Skan#endif /* APIC_IO */ 460169689Skan 461169689Skan POSTCODE(MP_ENABLE_POST); 462260919Spfg 463260919Spfg /* turn on 4MB of V == P addressing so we can get to MP table */ 464260919Spfg *(int *)PTD = PG_V | PG_RW | ((u_long)KPTphys & PG_FRAME); 465169689Skan invltlb(); 466169689Skan 467169689Skan /* examine the MP table for needed info, uses physical addresses */ 468169689Skan x = mptable_pass2(); 469169689Skan 470169689Skan *(int *)PTD = 0; 471169689Skan invltlb(); 472169689Skan 473169689Skan /* can't process default configs till the CPU APIC is pmapped */ 474169689Skan if (x) 475169689Skan default_mp_table(x); 47690075Sobrien 47790075Sobrien /* post scan cleanup */ 47890075Sobrien fix_mp_table(); 47990075Sobrien 480117395Skan#if defined(APIC_IO) 48190075Sobrien 482 /* fill the LOGICAL io_apic_versions table */ 483 for (apic = 0; apic < mp_napics; ++apic) { 484 ux = io_apic_read(apic, IOAPIC_VER); 485 io_apic_versions[apic] = ux; 486 } 487 488 /* program each IO APIC in the system */ 489 for (apic = 0; apic < mp_napics; ++apic) 490 if (io_apic_setup(apic) < 0) 491 panic("IO APIC setup failure"); 492 493 /* install a 'Spurious INTerrupt' vector */ 494 setidt(XSPURIOUSINT_OFFSET, Xspuriousint, 495 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 496 497 /* install an inter-CPU IPI for TLB invalidation */ 498 setidt(XINVLTLB_OFFSET, Xinvltlb, 499 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 500 501 /* install an inter-CPU IPI for CPU stop/restart */ 502 setidt(XCPUSTOP_OFFSET, Xcpustop, 503 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 504 505#if defined(TEST_TEST1) 506 /* install a "fake hardware INTerrupt" vector */ 507 setidt(XTEST1_OFFSET, Xtest1, 508 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 509#endif /** TEST_TEST1 */ 510 511#endif /* APIC_IO */ 512 513 /* initialize all SMP locks */ 514 init_locks(); 515 516 /* start each Application Processor */ 517 start_all_aps(boot_addr); 518 519 /* 520 * The init process might be started on a different CPU now, 521 * and the boot CPU might not call prepare_usermode to get 522 * cr0 correctly configured. Thus we initialize cr0 here. 523 */ 524 load_cr0(rcr0() | CR0_WP | CR0_AM); 525} 526 527 528/* 529 * look for the MP spec signature 530 */ 531 532/* string defined by the Intel MP Spec as identifying the MP table */ 533#define MP_SIG 0x5f504d5f /* _MP_ */ 534#define NEXT(X) ((X) += 4) 535static int 536search_for_sig(u_int32_t target, int count) 537{ 538 int x; 539 u_int32_t *addr = (u_int32_t *) (KERNBASE + target); 540 541 for (x = 0; x < count; NEXT(x)) 542 if (addr[x] == MP_SIG) 543 /* make array index a byte index */ 544 return (target + (x * sizeof(u_int32_t))); 545 546 return -1; 547} 548 549 550static basetable_entry basetable_entry_types[] = 551{ 552 {0, 20, "Processor"}, 553 {1, 8, "Bus"}, 554 {2, 8, "I/O APIC"}, 555 {3, 8, "I/O INT"}, 556 {4, 8, "Local INT"} 557}; 558 559typedef struct BUSDATA { 560 u_char bus_id; 561 enum busTypes bus_type; 562} bus_datum; 563 564typedef struct INTDATA { 565 u_char int_type; 566 u_short int_flags; 567 u_char src_bus_id; 568 u_char src_bus_irq; 569 u_char dst_apic_id; 570 u_char dst_apic_int; 571} io_int, local_int; 572 573typedef struct BUSTYPENAME { 574 u_char type; 575 char name[7]; 576} bus_type_name; 577 578static bus_type_name bus_type_table[] = 579{ 580 {CBUS, "CBUS"}, 581 {CBUSII, "CBUSII"}, 582 {EISA, "EISA"}, 583 {UNKNOWN_BUSTYPE, "---"}, 584 {UNKNOWN_BUSTYPE, "---"}, 585 {ISA, "ISA"}, 586 {UNKNOWN_BUSTYPE, "---"}, 587 {UNKNOWN_BUSTYPE, "---"}, 588 {UNKNOWN_BUSTYPE, "---"}, 589 {UNKNOWN_BUSTYPE, "---"}, 590 {UNKNOWN_BUSTYPE, "---"}, 591 {UNKNOWN_BUSTYPE, "---"}, 592 {PCI, "PCI"}, 593 {UNKNOWN_BUSTYPE, "---"}, 594 {UNKNOWN_BUSTYPE, "---"}, 595 {UNKNOWN_BUSTYPE, "---"}, 596 {UNKNOWN_BUSTYPE, "---"}, 597 {XPRESS, "XPRESS"}, 598 {UNKNOWN_BUSTYPE, "---"} 599}; 600/* from MP spec v1.4, table 5-1 */ 601static int default_data[7][5] = 602{ 603/* nbus, id0, type0, id1, type1 */ 604 {1, 0, ISA, 255, 255}, 605 {1, 0, EISA, 255, 255}, 606 {1, 0, EISA, 255, 255}, 607 {0, 255, 255, 255, 255},/* MCA not supported */ 608 {2, 0, ISA, 1, PCI}, 609 {2, 0, EISA, 1, PCI}, 610 {0, 255, 255, 255, 255} /* MCA not supported */ 611}; 612 613 614/* the bus data */ 615bus_datum bus_data[NBUS]; 616 617/* the IO INT data, one entry per possible APIC INTerrupt */ 618io_int io_apic_ints[NINTR]; 619 620static int nintrs; 621 622static int processor_entry __P((proc_entry_ptr entry, int cpu)); 623static int bus_entry __P((bus_entry_ptr entry, int bus)); 624static int io_apic_entry __P((io_apic_entry_ptr entry, int apic)); 625static int int_entry __P((int_entry_ptr entry, int intr)); 626static int lookup_bus_type __P((char *name)); 627 628 629/* 630 * 1st pass on motherboard's Intel MP specification table. 631 * 632 * initializes: 633 * mp_ncpus = 1 634 * 635 * determines: 636 * cpu_apic_address (common to all CPUs) 637 * io_apic_address[N] 638 * mp_naps 639 * mp_nbusses 640 * mp_napics 641 * nintrs 642 */ 643static int 644mptable_pass1(void) 645{ 646 int x; 647 mpcth_t cth; 648 int totalSize; 649 void* position; 650 int count; 651 int type; 652 int mustpanic; 653 654 POSTCODE(MPTABLE_PASS1_POST); 655 656 mustpanic = 0; 657 658 /* clear various tables */ 659 for (x = 0; x < NAPICID; ++x) { 660 io_apic_address[x] = ~0; /* IO APIC address table */ 661 } 662 663 /* init everything to empty */ 664 mp_naps = 0; 665 mp_nbusses = 0; 666 mp_napics = 0; 667 nintrs = 0; 668 669 /* check for use of 'default' configuration */ 670 if (MPFPS_MPFB1 != 0) { 671 /* use default addresses */ 672 cpu_apic_address = DEFAULT_APIC_BASE; 673 io_apic_address[0] = DEFAULT_IO_APIC_BASE; 674 675 /* fill in with defaults */ 676 mp_naps = 2; /* includes BSP */ 677 mp_nbusses = default_data[MPFPS_MPFB1 - 1][0]; 678#if defined(APIC_IO) 679 mp_napics = 1; 680 nintrs = 16; 681#endif /* APIC_IO */ 682 } 683 else { 684 if ((cth = mpfps->pap) == 0) 685 panic("MP Configuration Table Header MISSING!"); 686 687 cpu_apic_address = (vm_offset_t) cth->apic_address; 688 689 /* walk the table, recording info of interest */ 690 totalSize = cth->base_table_length - sizeof(struct MPCTH); 691 position = (u_char *) cth + sizeof(struct MPCTH); 692 count = cth->entry_count; 693 694 while (count--) { 695 switch (type = *(u_char *) position) { 696 case 0: /* processor_entry */ 697 if (((proc_entry_ptr)position)->cpu_flags 698 & PROCENTRY_FLAG_EN) 699 ++mp_naps; 700 break; 701 case 1: /* bus_entry */ 702 ++mp_nbusses; 703 break; 704 case 2: /* io_apic_entry */ 705 if (((io_apic_entry_ptr)position)->apic_flags 706 & IOAPICENTRY_FLAG_EN) 707 io_apic_address[mp_napics++] = 708 (vm_offset_t)((io_apic_entry_ptr) 709 position)->apic_address; 710 break; 711 case 3: /* int_entry */ 712 ++nintrs; 713 break; 714 case 4: /* int_entry */ 715 break; 716 default: 717 panic("mpfps Base Table HOSED!"); 718 /* NOTREACHED */ 719 } 720 721 totalSize -= basetable_entry_types[type].length; 722 (u_char*)position += basetable_entry_types[type].length; 723 } 724 } 725 726 /* qualify the numbers */ 727 if (mp_naps > NCPU) 728#if 0 /* XXX FIXME: kern/4255 */ 729 printf("Warning: only using %d of %d available CPUs!\n", 730 NCPU, mp_naps); 731#else 732 { 733 printf("NCPU cannot be different than actual CPU count.\n"); 734 printf(" add 'options NCPU=%d' to your kernel config file,\n", 735 mp_naps); 736 printf(" then rerun config & rebuild your SMP kernel\n"); 737 mustpanic = 1; 738 } 739#endif /* XXX FIXME: kern/4255 */ 740 if (mp_nbusses > NBUS) { 741 printf("found %d busses, increase NBUS\n", mp_nbusses); 742 mustpanic = 1; 743 } 744 if (mp_napics > NAPIC) { 745 printf("found %d apics, increase NAPIC\n", mp_napics); 746 mustpanic = 1; 747 } 748 if (nintrs > NINTR) { 749 printf("found %d intrs, increase NINTR\n", nintrs); 750 mustpanic = 1; 751 } 752 753 /* 754 * Count the BSP. 755 * This is also used as a counter while starting the APs. 756 */ 757 mp_ncpus = 1; 758 759 --mp_naps; /* subtract the BSP */ 760 761 return mustpanic; 762} 763 764 765/* 766 * 2nd pass on motherboard's Intel MP specification table. 767 * 768 * sets: 769 * boot_cpu_id 770 * ID_TO_IO(N), phy APIC ID to log CPU/IO table 771 * CPU_TO_ID(N), logical CPU to APIC ID table 772 * IO_TO_ID(N), logical IO to APIC ID table 773 * bus_data[N] 774 * io_apic_ints[N] 775 */ 776static int 777mptable_pass2(void) 778{ 779 int x; 780 mpcth_t cth; 781 int totalSize; 782 void* position; 783 int count; 784 int type; 785 int apic, bus, cpu, intr; 786 787 POSTCODE(MPTABLE_PASS2_POST); 788 789 /* clear various tables */ 790 for (x = 0; x < NAPICID; ++x) { 791 ID_TO_IO(x) = -1; /* phy APIC ID to log CPU/IO table */ 792 CPU_TO_ID(x) = -1; /* logical CPU to APIC ID table */ 793 IO_TO_ID(x) = -1; /* logical IO to APIC ID table */ 794 } 795 796 /* clear bus data table */ 797 for (x = 0; x < NBUS; ++x) 798 bus_data[x].bus_id = 0xff; 799 800 /* clear IO APIC INT table */ 801 for (x = 0; x < NINTR; ++x) 802 io_apic_ints[x].int_type = 0xff; 803 804 /* setup the cpu/apic mapping arrays */ 805 boot_cpu_id = -1; 806 807 /* record whether PIC or virtual-wire mode */ 808 picmode = (mpfps->mpfb2 & 0x80) ? 1 : 0; 809 810 /* check for use of 'default' configuration */ 811 if (MPFPS_MPFB1 != 0) 812 return MPFPS_MPFB1; /* return default configuration type */ 813 814 if ((cth = mpfps->pap) == 0) 815 panic("MP Configuration Table Header MISSING!"); 816 817 /* walk the table, recording info of interest */ 818 totalSize = cth->base_table_length - sizeof(struct MPCTH); 819 position = (u_char *) cth + sizeof(struct MPCTH); 820 count = cth->entry_count; 821 apic = bus = intr = 0; 822 cpu = 1; /* pre-count the BSP */ 823 824 while (count--) { 825 switch (type = *(u_char *) position) { 826 case 0: 827 if (processor_entry(position, cpu)) 828 ++cpu; 829 break; 830 case 1: 831 if (bus_entry(position, bus)) 832 ++bus; 833 break; 834 case 2: 835 if (io_apic_entry(position, apic)) 836 ++apic; 837 break; 838 case 3: 839 if (int_entry(position, intr)) 840 ++intr; 841 break; 842 case 4: 843 /* int_entry(position); */ 844 break; 845 default: 846 panic("mpfps Base Table HOSED!"); 847 /* NOTREACHED */ 848 } 849 850 totalSize -= basetable_entry_types[type].length; 851 (u_char *) position += basetable_entry_types[type].length; 852 } 853 854 if (boot_cpu_id == -1) 855 panic("NO BSP found!"); 856 857 /* report fact that its NOT a default configuration */ 858 return 0; 859} 860 861 862/* 863 * parse an Intel MP specification table 864 */ 865static void 866fix_mp_table(void) 867{ 868 int x; 869 int id; 870 int bus_0; 871 int bus_pci; 872 int num_pci_bus; 873 874 /* 875 * Fix mis-numbering of the PCI bus and its INT entries if the BIOS 876 * did it wrong. The MP spec says that when more than 1 PCI bus 877 * exists the BIOS must begin with bus entries for the PCI bus and use 878 * actual PCI bus numbering. This implies that when only 1 PCI bus 879 * exists the BIOS can choose to ignore this ordering, and indeed many 880 * MP motherboards do ignore it. This causes a problem when the PCI 881 * sub-system makes requests of the MP sub-system based on PCI bus 882 * numbers. So here we look for the situation and renumber the 883 * busses and associated INTs in an effort to "make it right". 884 */ 885 886 /* find bus 0, PCI bus, count the number of PCI busses */ 887 for (num_pci_bus = 0, x = 0; x < mp_nbusses; ++x) { 888 if (bus_data[x].bus_id == 0) { 889 bus_0 = x; 890 } 891 if (bus_data[x].bus_type == PCI) { 892 ++num_pci_bus; 893 bus_pci = x; 894 } 895 } 896 /* 897 * bus_0 == slot of bus with ID of 0 898 * bus_pci == slot of last PCI bus encountered 899 */ 900 901 /* check the 1 PCI bus case for sanity */ 902 if (num_pci_bus == 1) { 903 904 /* if it is number 0 all is well */ 905 if (bus_data[bus_pci].bus_id == 0) 906 return; 907 908 /* mis-numbered, swap with whichever bus uses slot 0 */ 909 910 /* swap the bus entry types */ 911 bus_data[bus_pci].bus_type = bus_data[bus_0].bus_type; 912 bus_data[bus_0].bus_type = PCI; 913 914 /* swap each relavant INTerrupt entry */ 915 id = bus_data[bus_pci].bus_id; 916 for (x = 0; x < nintrs; ++x) { 917 if (io_apic_ints[x].src_bus_id == id) { 918 io_apic_ints[x].src_bus_id = 0; 919 } 920 else if (io_apic_ints[x].src_bus_id == 0) { 921 io_apic_ints[x].src_bus_id = id; 922 } 923 } 924 } 925 /* sanity check if more than 1 PCI bus */ 926 else if (num_pci_bus > 1) { 927 for (x = 0; x < mp_nbusses; ++x) { 928 if (bus_data[x].bus_type != PCI) 929 continue; 930 if (bus_data[x].bus_id >= num_pci_bus) 931 panic("bad PCI bus numbering"); 932 } 933 } 934} 935 936 937static int 938processor_entry(proc_entry_ptr entry, int cpu) 939{ 940 /* check for usability */ 941 if ((cpu >= NCPU) || !(entry->cpu_flags & PROCENTRY_FLAG_EN)) 942 return 0; 943 944 /* check for BSP flag */ 945 if (entry->cpu_flags & PROCENTRY_FLAG_BP) { 946 boot_cpu_id = entry->apic_id; 947 CPU_TO_ID(0) = entry->apic_id; 948 ID_TO_CPU(entry->apic_id) = 0; 949 return 0; /* its already been counted */ 950 } 951 952 /* add another AP to list, if less than max number of CPUs */ 953 else { 954 CPU_TO_ID(cpu) = entry->apic_id; 955 ID_TO_CPU(entry->apic_id) = cpu; 956 return 1; 957 } 958} 959 960 961static int 962bus_entry(bus_entry_ptr entry, int bus) 963{ 964 int x; 965 char c, name[8]; 966 967 /* encode the name into an index */ 968 for (x = 0; x < 6; ++x) { 969 if ((c = entry->bus_type[x]) == ' ') 970 break; 971 name[x] = c; 972 } 973 name[x] = '\0'; 974 975 if ((x = lookup_bus_type(name)) == UNKNOWN_BUSTYPE) 976 panic("unknown bus type: '%s'", name); 977 978 bus_data[bus].bus_id = entry->bus_id; 979 bus_data[bus].bus_type = x; 980 981 return 1; 982} 983 984 985static int 986io_apic_entry(io_apic_entry_ptr entry, int apic) 987{ 988 if (!(entry->apic_flags & IOAPICENTRY_FLAG_EN)) 989 return 0; 990 991 IO_TO_ID(apic) = entry->apic_id; 992 ID_TO_IO(entry->apic_id) = apic; 993 994 return 1; 995} 996 997 998static int 999lookup_bus_type(char *name) 1000{ 1001 int x; 1002 1003 for (x = 0; x < MAX_BUSTYPE; ++x) 1004 if (strcmp(bus_type_table[x].name, name) == 0) 1005 return bus_type_table[x].type; 1006 1007 return UNKNOWN_BUSTYPE; 1008} 1009 1010 1011static int 1012int_entry(int_entry_ptr entry, int intr) 1013{ 1014 io_apic_ints[intr].int_type = entry->int_type; 1015 io_apic_ints[intr].int_flags = entry->int_flags; 1016 io_apic_ints[intr].src_bus_id = entry->src_bus_id; 1017 io_apic_ints[intr].src_bus_irq = entry->src_bus_irq; 1018 io_apic_ints[intr].dst_apic_id = entry->dst_apic_id; 1019 io_apic_ints[intr].dst_apic_int = entry->dst_apic_int; 1020 1021 return 1; 1022} 1023 1024 1025static int 1026apic_int_is_bus_type(int intr, int bus_type) 1027{ 1028 int bus; 1029 1030 for (bus = 0; bus < mp_nbusses; ++bus) 1031 if ((bus_data[bus].bus_id == io_apic_ints[intr].src_bus_id) 1032 && ((int) bus_data[bus].bus_type == bus_type)) 1033 return 1; 1034 1035 return 0; 1036} 1037 1038 1039/* 1040 * Given a traditional ISA INT mask, return an APIC mask. 1041 */ 1042u_int 1043isa_apic_mask(u_int isa_mask) 1044{ 1045 int isa_irq; 1046 int apic_pin; 1047 1048#if defined(SKIP_IRQ15_REDIRECT) 1049 if (isa_mask == (1 << 15)) { 1050 printf("skipping ISA IRQ15 redirect\n"); 1051 return isa_mask; 1052 } 1053#endif /* SKIP_IRQ15_REDIRECT */ 1054 1055 isa_irq = ffs(isa_mask); /* find its bit position */ 1056 if (isa_irq == 0) /* doesn't exist */ 1057 return 0; 1058 --isa_irq; /* make it zero based */ 1059 1060 apic_pin = isa_apic_pin(isa_irq); /* look for APIC connection */ 1061 if (apic_pin == -1) 1062 return 0; 1063 1064 return (1 << apic_pin); /* convert pin# to a mask */ 1065} 1066 1067 1068/* 1069 * Determine which APIC pin an ISA/EISA INT is attached to. 1070 */ 1071#define INTTYPE(I) (io_apic_ints[(I)].int_type) 1072#define INTPIN(I) (io_apic_ints[(I)].dst_apic_int) 1073 1074#define SRCBUSIRQ(I) (io_apic_ints[(I)].src_bus_irq) 1075int 1076isa_apic_pin(int isa_irq) 1077{ 1078 int intr; 1079 1080 for (intr = 0; intr < nintrs; ++intr) { /* check each record */ 1081 if (INTTYPE(intr) == 0) { /* standard INT */ 1082 if (SRCBUSIRQ(intr) == isa_irq) { 1083 if (apic_int_is_bus_type(intr, ISA) || 1084 apic_int_is_bus_type(intr, EISA)) 1085 return INTPIN(intr); /* found */ 1086 } 1087 } 1088 } 1089 return -1; /* NOT found */ 1090} 1091#undef SRCBUSIRQ 1092 1093 1094/* 1095 * Determine which APIC pin a PCI INT is attached to. 1096 */ 1097#define SRCBUSID(I) (io_apic_ints[(I)].src_bus_id) 1098#define SRCBUSDEVICE(I) ((io_apic_ints[(I)].src_bus_irq >> 2) & 0x1f) 1099#define SRCBUSLINE(I) (io_apic_ints[(I)].src_bus_irq & 0x03) 1100int 1101pci_apic_pin(int pciBus, int pciDevice, int pciInt) 1102{ 1103 int intr; 1104 1105 --pciInt; /* zero based */ 1106 1107 for (intr = 0; intr < nintrs; ++intr) /* check each record */ 1108 if ((INTTYPE(intr) == 0) /* standard INT */ 1109 && (SRCBUSID(intr) == pciBus) 1110 && (SRCBUSDEVICE(intr) == pciDevice) 1111 && (SRCBUSLINE(intr) == pciInt)) /* a candidate IRQ */ 1112 if (apic_int_is_bus_type(intr, PCI)) 1113 return INTPIN(intr); /* exact match */ 1114 1115 return -1; /* NOT found */ 1116} 1117#undef SRCBUSLINE 1118#undef SRCBUSDEVICE 1119#undef SRCBUSID 1120 1121#undef INTPIN 1122#undef INTTYPE 1123 1124 1125/* 1126 * Reprogram the MB chipset to NOT redirect an ISA INTerrupt. 1127 * 1128 * XXX FIXME: 1129 * Exactly what this means is unclear at this point. It is a solution 1130 * for motherboards that redirect the MBIRQ0 pin. Generically a motherboard 1131 * could route any of the ISA INTs to upper (>15) IRQ values. But most would 1132 * NOT be redirected via MBIRQ0, thus "undirect()ing" them would NOT be an 1133 * option. 1134 */ 1135int 1136undirect_isa_irq(int rirq) 1137{ 1138#if defined(READY) 1139 printf("Freeing redirected ISA irq %d.\n", rirq); 1140 /** FIXME: tickle the MB redirector chip */ 1141 return ???; 1142#else 1143 printf("Freeing (NOT implemented) redirected ISA irq %d.\n", rirq); 1144 return 0; 1145#endif /* READY */ 1146} 1147 1148 1149/* 1150 * Reprogram the MB chipset to NOT redirect a PCI INTerrupt 1151 */ 1152int 1153undirect_pci_irq(int rirq) 1154{ 1155#if defined(READY) 1156 if (bootverbose) 1157 printf("Freeing redirected PCI irq %d.\n", rirq); 1158 1159 /** FIXME: tickle the MB redirector chip */ 1160 return ???; 1161#else 1162 if (bootverbose) 1163 printf("Freeing (NOT implemented) redirected PCI irq %d.\n", 1164 rirq); 1165 return 0; 1166#endif /* READY */ 1167} 1168 1169 1170/* 1171 * given a bus ID, return: 1172 * the bus type if found 1173 * -1 if NOT found 1174 */ 1175int 1176apic_bus_type(int id) 1177{ 1178 int x; 1179 1180 for (x = 0; x < mp_nbusses; ++x) 1181 if (bus_data[x].bus_id == id) 1182 return bus_data[x].bus_type; 1183 1184 return -1; 1185} 1186 1187 1188/* 1189 * given a LOGICAL APIC# and pin#, return: 1190 * the associated src bus ID if found 1191 * -1 if NOT found 1192 */ 1193int 1194apic_src_bus_id(int apic, int pin) 1195{ 1196 int x; 1197 1198 /* search each of the possible INTerrupt sources */ 1199 for (x = 0; x < nintrs; ++x) 1200 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) && 1201 (pin == io_apic_ints[x].dst_apic_int)) 1202 return (io_apic_ints[x].src_bus_id); 1203 1204 return -1; /* NOT found */ 1205} 1206 1207 1208/* 1209 * given a LOGICAL APIC# and pin#, return: 1210 * the associated src bus IRQ if found 1211 * -1 if NOT found 1212 */ 1213int 1214apic_src_bus_irq(int apic, int pin) 1215{ 1216 int x; 1217 1218 for (x = 0; x < nintrs; x++) 1219 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) && 1220 (pin == io_apic_ints[x].dst_apic_int)) 1221 return (io_apic_ints[x].src_bus_irq); 1222 1223 return -1; /* NOT found */ 1224} 1225 1226 1227/* 1228 * given a LOGICAL APIC# and pin#, return: 1229 * the associated INTerrupt type if found 1230 * -1 if NOT found 1231 */ 1232int 1233apic_int_type(int apic, int pin) 1234{ 1235 int x; 1236 1237 /* search each of the possible INTerrupt sources */ 1238 for (x = 0; x < nintrs; ++x) 1239 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) && 1240 (pin == io_apic_ints[x].dst_apic_int)) 1241 return (io_apic_ints[x].int_type); 1242 1243 return -1; /* NOT found */ 1244} 1245 1246 1247/* 1248 * given a LOGICAL APIC# and pin#, return: 1249 * the associated trigger mode if found 1250 * -1 if NOT found 1251 */ 1252int 1253apic_trigger(int apic, int pin) 1254{ 1255 int x; 1256 1257 /* search each of the possible INTerrupt sources */ 1258 for (x = 0; x < nintrs; ++x) 1259 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) && 1260 (pin == io_apic_ints[x].dst_apic_int)) 1261 return ((io_apic_ints[x].int_flags >> 2) & 0x03); 1262 1263 return -1; /* NOT found */ 1264} 1265 1266 1267/* 1268 * given a LOGICAL APIC# and pin#, return: 1269 * the associated 'active' level if found 1270 * -1 if NOT found 1271 */ 1272int 1273apic_polarity(int apic, int pin) 1274{ 1275 int x; 1276 1277 /* search each of the possible INTerrupt sources */ 1278 for (x = 0; x < nintrs; ++x) 1279 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) && 1280 (pin == io_apic_ints[x].dst_apic_int)) 1281 return (io_apic_ints[x].int_flags & 0x03); 1282 1283 return -1; /* NOT found */ 1284} 1285 1286 1287/* 1288 * set data according to MP defaults 1289 * FIXME: probably not complete yet... 1290 */ 1291static void 1292default_mp_table(int type) 1293{ 1294 int ap_cpu_id; 1295#if defined(APIC_IO) 1296 u_int32_t ux; 1297 int io_apic_id; 1298 int pin; 1299#endif /* APIC_IO */ 1300 1301#if 0 1302 printf(" MP default config type: %d\n", type); 1303 switch (type) { 1304 case 1: 1305 printf(" bus: ISA, APIC: 82489DX\n"); 1306 break; 1307 case 2: 1308 printf(" bus: EISA, APIC: 82489DX\n"); 1309 break; 1310 case 3: 1311 printf(" bus: EISA, APIC: 82489DX\n"); 1312 break; 1313 case 4: 1314 printf(" bus: MCA, APIC: 82489DX\n"); 1315 break; 1316 case 5: 1317 printf(" bus: ISA+PCI, APIC: Integrated\n"); 1318 break; 1319 case 6: 1320 printf(" bus: EISA+PCI, APIC: Integrated\n"); 1321 break; 1322 case 7: 1323 printf(" bus: MCA+PCI, APIC: Integrated\n"); 1324 break; 1325 default: 1326 printf(" future type\n"); 1327 break; 1328 /* NOTREACHED */ 1329 } 1330#endif /* 0 */ 1331 1332 boot_cpu_id = (lapic.id & APIC_ID_MASK) >> 24; 1333 ap_cpu_id = (boot_cpu_id == 0) ? 1 : 0; 1334 1335 /* BSP */ 1336 CPU_TO_ID(0) = boot_cpu_id; 1337 ID_TO_CPU(boot_cpu_id) = 0; 1338 1339 /* one and only AP */ 1340 CPU_TO_ID(1) = ap_cpu_id; 1341 ID_TO_CPU(ap_cpu_id) = 1; 1342 1343#if defined(APIC_IO) 1344 /* one and only IO APIC */ 1345 io_apic_id = (io_apic_read(0, IOAPIC_ID) & APIC_ID_MASK) >> 24; 1346 1347 /* 1348 * sanity check, refer to MP spec section 3.6.6, last paragraph 1349 * necessary as some hardware isn't properly setting up the IO APIC 1350 */ 1351#if defined(REALLY_ANAL_IOAPICID_VALUE) 1352 if (io_apic_id != 2) { 1353#else 1354 if ((io_apic_id == 0) || (io_apic_id == 1) || (io_apic_id == 15)) { 1355#endif /* REALLY_ANAL_IOAPICID_VALUE */ 1356 ux = io_apic_read(0, IOAPIC_ID); /* get current contents */ 1357 ux &= ~APIC_ID_MASK; /* clear the ID field */ 1358 ux |= 0x02000000; /* set it to '2' */ 1359 io_apic_write(0, IOAPIC_ID, ux); /* write new value */ 1360 ux = io_apic_read(0, IOAPIC_ID); /* re-read && test */ 1361 if ((ux & APIC_ID_MASK) != 0x02000000) 1362 panic("can't control IO APIC ID, reg: 0x%08x", ux); 1363 io_apic_id = 2; 1364 } 1365 IO_TO_ID(0) = io_apic_id; 1366 ID_TO_IO(io_apic_id) = 0; 1367#endif /* APIC_IO */ 1368 1369 /* fill out bus entries */ 1370 switch (type) { 1371 case 1: 1372 case 2: 1373 case 3: 1374 case 5: 1375 case 6: 1376 bus_data[0].bus_id = default_data[type - 1][1]; 1377 bus_data[0].bus_type = default_data[type - 1][2]; 1378 bus_data[1].bus_id = default_data[type - 1][3]; 1379 bus_data[1].bus_type = default_data[type - 1][4]; 1380 break; 1381 1382 /* case 4: case 7: MCA NOT supported */ 1383 default: /* illegal/reserved */ 1384 panic("BAD default MP config: %d", type); 1385 /* NOTREACHED */ 1386 } 1387 1388#if defined(APIC_IO) 1389 /* general cases from MP v1.4, table 5-2 */ 1390 for (pin = 0; pin < 16; ++pin) { 1391 io_apic_ints[pin].int_type = 0; 1392 io_apic_ints[pin].int_flags = 0x05; /* edge/active-hi */ 1393 io_apic_ints[pin].src_bus_id = 0; 1394 io_apic_ints[pin].src_bus_irq = pin; /* IRQ2 caught below */ 1395 io_apic_ints[pin].dst_apic_id = io_apic_id; 1396 io_apic_ints[pin].dst_apic_int = pin; /* 1-to-1 */ 1397 } 1398 1399 /* special cases from MP v1.4, table 5-2 */ 1400 if (type == 2) { 1401 io_apic_ints[2].int_type = 0xff; /* N/C */ 1402 io_apic_ints[13].int_type = 0xff; /* N/C */ 1403#if !defined(APIC_MIXED_MODE) 1404 /** FIXME: ??? */ 1405 panic("sorry, can't support type 2 default yet"); 1406#endif /* APIC_MIXED_MODE */ 1407 } 1408 else 1409 io_apic_ints[2].src_bus_irq = 0; /* ISA IRQ0 is on APIC INT 2 */ 1410 1411 if (type == 7) 1412 io_apic_ints[0].int_type = 0xff; /* N/C */ 1413 else 1414 io_apic_ints[0].int_type = 3; /* vectored 8259 */ 1415#endif /* APIC_IO */ 1416} 1417 1418 1419/* 1420 * initialize all the SMP locks 1421 */ 1422 1423/* critical region around IO APIC, apic_imen */ 1424struct simplelock imen_lock; 1425 1426/* critical region around splxx(), cpl, cil, ipending */ 1427struct simplelock cpl_lock; 1428 1429/* Make FAST_INTR() routines sequential */ 1430struct simplelock fast_intr_lock; 1431 1432/* critical region around INTR() routines */ 1433struct simplelock intr_lock; 1434 1435/* lock the com (tty) data structures */ 1436struct simplelock com_lock; 1437 1438static void 1439init_locks(void) 1440{ 1441 /* 1442 * Get the initial mp_lock with a count of 1 for the BSP. 1443 * This uses a LOGICAL cpu ID, ie BSP == 0. 1444 */ 1445 mp_lock = 0x00000001; 1446 1447 /* ISR uses its own "giant lock" */ 1448 isr_lock = 0x00000000; 1449 1450 /* serializes FAST_INTR() accesses */ 1451 s_lock_init((struct simplelock*)&fast_intr_lock); 1452 1453 /* serializes INTR() accesses */ 1454 s_lock_init((struct simplelock*)&intr_lock); 1455 1456 /* locks the IO APIC and apic_imen accesses */ 1457 s_lock_init((struct simplelock*)&imen_lock); 1458 1459 /* locks cpl accesses */ 1460 s_lock_init((struct simplelock*)&cpl_lock); 1461 1462 /* locks com (tty) data/hardware accesses: a FASTINTR() */ 1463 s_lock_init((struct simplelock*)&com_lock); 1464} 1465 1466 1467/* 1468 * start each AP in our list 1469 */ 1470static int 1471start_all_aps(u_int boot_addr) 1472{ 1473 int x, i; 1474 u_char mpbiosreason; 1475 u_long mpbioswarmvec; 1476 pd_entry_t newptd; 1477 pt_entry_t newpt; 1478 int *newpp; 1479 1480 POSTCODE(START_ALL_APS_POST); 1481 1482 /* initialize BSP's local APIC */ 1483 apic_initialize(); 1484 bsp_apic_ready = 1; 1485 1486 /* install the AP 1st level boot code */ 1487 install_ap_tramp(boot_addr); 1488 1489 1490 /* save the current value of the warm-start vector */ 1491 mpbioswarmvec = *((u_long *) WARMBOOT_OFF); 1492 outb(CMOS_REG, BIOS_RESET); 1493 mpbiosreason = inb(CMOS_DATA); 1494 1495 /* record BSP in CPU map */ 1496 all_cpus = 1; 1497 1498 /* start each AP */ 1499 for (x = 1; x <= mp_naps; ++x) { 1500 1501 /* HACK HACK HACK !!! */ 1502 1503 /* alloc new page table directory */ 1504 newptd = (pd_entry_t)(kmem_alloc(kernel_map, PAGE_SIZE)); 1505 1506 /* clone currently active one (ie: IdlePTD) */ 1507 bcopy(PTD, newptd, PAGE_SIZE); /* inc prv page pde */ 1508 1509 /* set up 0 -> 4MB P==V mapping for AP boot */ 1510 newptd[0] = PG_V | PG_RW | ((u_long)KPTphys & PG_FRAME); 1511 1512 /* store PTD for this AP */ 1513 bootPTD = (pd_entry_t)vtophys(newptd); 1514 1515 /* alloc new page table page */ 1516 newpt = (pt_entry_t)(kmem_alloc(kernel_map, PAGE_SIZE)); 1517 1518 /* set the new PTD's private page to point there */ 1519 newptd[MPPTDI] = PG_V | PG_RW | vtophys(newpt); 1520 1521 /* install self referential entry */ 1522 newptd[PTDPTDI] = PG_V | PG_RW | vtophys(newptd); 1523 1524 /* get a new private data page */ 1525 newpp = (int *)kmem_alloc(kernel_map, PAGE_SIZE); 1526 1527 /* wire it into the private page table page */ 1528 newpt[0] = PG_V | PG_RW | vtophys(newpp); 1529 1530 /* wire the ptp into itself for access */ 1531 newpt[1] = PG_V | PG_RW | vtophys(newpt); 1532 1533 /* and the local apic */ 1534 newpt[2] = SMP_prvpt[2]; 1535 1536 /* and the IO apic mapping[s] */ 1537 for (i = 16; i < 32; i++) 1538 newpt[i] = SMP_prvpt[i]; 1539 1540 /* prime data page for it to use */ 1541 newpp[0] = x; /* cpuid */ 1542 newpp[1] = 0; /* curproc */ 1543 newpp[2] = 0; /* curpcb */ 1544 newpp[3] = 0; /* npxproc */ 1545 newpp[4] = 0; /* runtime.tv_sec */ 1546 newpp[5] = 0; /* runtime.tv_usec */ 1547 newpp[6] = x << 24; /* cpu_lockid */ 1548 1549 /* XXX NOTE: ABANDON bootPTD for now!!!! */ 1550 1551 /* END REVOLTING HACKERY */ 1552 1553 /* setup a vector to our boot code */ 1554 *((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET; 1555 *((volatile u_short *) WARMBOOT_SEG) = (boot_addr >> 4); 1556 outb(CMOS_REG, BIOS_RESET); 1557 outb(CMOS_DATA, BIOS_WARM); /* 'warm-start' */ 1558 1559 /* attempt to start the Application Processor */ 1560 CHECK_INIT(99); /* setup checkpoints */ 1561 if (!start_ap(x, boot_addr)) { 1562 printf("AP #%d (PHY# %d) failed!\n", x, CPU_TO_ID(x)); 1563 CHECK_PRINT("trace"); /* show checkpoints */ 1564 /* better panic as the AP may be running loose */ 1565 printf("panic y/n? [y] "); 1566 if (cngetc() != 'n') 1567 panic("bye-bye"); 1568 } 1569 CHECK_PRINT("trace"); /* show checkpoints */ 1570 1571 /* record its version info */ 1572 cpu_apic_versions[x] = cpu_apic_versions[0]; 1573 1574 all_cpus |= (1 << x); /* record AP in CPU map */ 1575 } 1576 1577 /* build our map of 'other' CPUs */ 1578 other_cpus = all_cpus & ~(1 << cpuid); 1579 1580 /* fill in our (BSP) APIC version */ 1581 cpu_apic_versions[0] = lapic.version; 1582 1583 /* restore the warmstart vector */ 1584 *(u_long *) WARMBOOT_OFF = mpbioswarmvec; 1585 outb(CMOS_REG, BIOS_RESET); 1586 outb(CMOS_DATA, mpbiosreason); 1587 1588 pmap_set_opt_bsp(); 1589 1590 /* number of APs actually started */ 1591 return mp_ncpus - 1; 1592} 1593 1594 1595/* 1596 * load the 1st level AP boot code into base memory. 1597 */ 1598 1599/* targets for relocation */ 1600extern void bigJump(void); 1601extern void bootCodeSeg(void); 1602extern void bootDataSeg(void); 1603extern void MPentry(void); 1604extern u_int MP_GDT; 1605extern u_int mp_gdtbase; 1606 1607static void 1608install_ap_tramp(u_int boot_addr) 1609{ 1610 int x; 1611 int size = *(int *) ((u_long) & bootMP_size); 1612 u_char *src = (u_char *) ((u_long) bootMP); 1613 u_char *dst = (u_char *) boot_addr + KERNBASE; 1614 u_int boot_base = (u_int) bootMP; 1615 u_int8_t *dst8; 1616 u_int16_t *dst16; 1617 u_int32_t *dst32; 1618 1619 POSTCODE(INSTALL_AP_TRAMP_POST); 1620 1621 for (x = 0; x < size; ++x) 1622 *dst++ = *src++; 1623 1624 /* 1625 * modify addresses in code we just moved to basemem. unfortunately we 1626 * need fairly detailed info about mpboot.s for this to work. changes 1627 * to mpboot.s might require changes here. 1628 */ 1629 1630 /* boot code is located in KERNEL space */ 1631 dst = (u_char *) boot_addr + KERNBASE; 1632 1633 /* modify the lgdt arg */ 1634 dst32 = (u_int32_t *) (dst + ((u_int) & mp_gdtbase - boot_base)); 1635 *dst32 = boot_addr + ((u_int) & MP_GDT - boot_base); 1636 1637 /* modify the ljmp target for MPentry() */ 1638 dst32 = (u_int32_t *) (dst + ((u_int) bigJump - boot_base) + 1); 1639 *dst32 = ((u_int) MPentry - KERNBASE); 1640 1641 /* modify the target for boot code segment */ 1642 dst16 = (u_int16_t *) (dst + ((u_int) bootCodeSeg - boot_base)); 1643 dst8 = (u_int8_t *) (dst16 + 1); 1644 *dst16 = (u_int) boot_addr & 0xffff; 1645 *dst8 = ((u_int) boot_addr >> 16) & 0xff; 1646 1647 /* modify the target for boot data segment */ 1648 dst16 = (u_int16_t *) (dst + ((u_int) bootDataSeg - boot_base)); 1649 dst8 = (u_int8_t *) (dst16 + 1); 1650 *dst16 = (u_int) boot_addr & 0xffff; 1651 *dst8 = ((u_int) boot_addr >> 16) & 0xff; 1652} 1653 1654 1655/* 1656 * this function starts the AP (application processor) identified 1657 * by the APIC ID 'physicalCpu'. It does quite a "song and dance" 1658 * to accomplish this. This is necessary because of the nuances 1659 * of the different hardware we might encounter. It ain't pretty, 1660 * but it seems to work. 1661 */ 1662static int 1663start_ap(int logical_cpu, u_int boot_addr) 1664{ 1665 int physical_cpu; 1666 int vector; 1667 int cpus; 1668 u_long icr_lo, icr_hi; 1669 1670 POSTCODE(START_AP_POST); 1671 1672 /* get the PHYSICAL APIC ID# */ 1673 physical_cpu = CPU_TO_ID(logical_cpu); 1674 1675 /* calculate the vector */ 1676 vector = (boot_addr >> 12) & 0xff; 1677 1678 /* used as a watchpoint to signal AP startup */ 1679 cpus = mp_ncpus; 1680 1681 /* 1682 * first we do an INIT/RESET IPI this INIT IPI might be run, reseting 1683 * and running the target CPU. OR this INIT IPI might be latched (P5 1684 * bug), CPU waiting for STARTUP IPI. OR this INIT IPI might be 1685 * ignored. 1686 */ 1687 1688 /* setup the address for the target AP */ 1689 icr_hi = lapic.icr_hi & ~APIC_ID_MASK; 1690 icr_hi |= (physical_cpu << 24); 1691 lapic.icr_hi = icr_hi; 1692 1693 /* do an INIT IPI: assert RESET */ 1694 icr_lo = lapic.icr_lo & 0xfff00000; 1695 lapic.icr_lo = icr_lo | 0x0000c500; 1696 1697 /* wait for pending status end */ 1698 while (lapic.icr_lo & APIC_DELSTAT_MASK) 1699 /* spin */ ; 1700 1701 /* do an INIT IPI: deassert RESET */ 1702 lapic.icr_lo = icr_lo | 0x00008500; 1703 1704 /* wait for pending status end */ 1705 u_sleep(10000); /* wait ~10mS */ 1706 while (lapic.icr_lo & APIC_DELSTAT_MASK) 1707 /* spin */ ; 1708 1709 /* 1710 * next we do a STARTUP IPI: the previous INIT IPI might still be 1711 * latched, (P5 bug) this 1st STARTUP would then terminate 1712 * immediately, and the previously started INIT IPI would continue. OR 1713 * the previous INIT IPI has already run. and this STARTUP IPI will 1714 * run. OR the previous INIT IPI was ignored. and this STARTUP IPI 1715 * will run. 1716 */ 1717 1718 /* do a STARTUP IPI */ 1719 lapic.icr_lo = icr_lo | 0x00000600 | vector; 1720 while (lapic.icr_lo & APIC_DELSTAT_MASK) 1721 /* spin */ ; 1722 u_sleep(200); /* wait ~200uS */ 1723 1724 /* 1725 * finally we do a 2nd STARTUP IPI: this 2nd STARTUP IPI should run IF 1726 * the previous STARTUP IPI was cancelled by a latched INIT IPI. OR 1727 * this STARTUP IPI will be ignored, as only ONE STARTUP IPI is 1728 * recognized after hardware RESET or INIT IPI. 1729 */ 1730 1731 lapic.icr_lo = icr_lo | 0x00000600 | vector; 1732 while (lapic.icr_lo & APIC_DELSTAT_MASK) 1733 /* spin */ ; 1734 u_sleep(200); /* wait ~200uS */ 1735 1736 /* wait for it to start */ 1737 set_apic_timer(5000000);/* == 5 seconds */ 1738 while (read_apic_timer()) 1739 if (mp_ncpus > cpus) 1740 return 1; /* return SUCCESS */ 1741 1742 return 0; /* return FAILURE */ 1743} 1744 1745 1746/* 1747 * Flush the TLB on all other CPU's 1748 * 1749 * XXX: Needs to handshake and wait for completion before proceding. 1750 */ 1751void 1752smp_invltlb(void) 1753{ 1754#if defined(APIC_IO) 1755 if (smp_active && invltlb_ok) 1756 all_but_self_ipi(XINVLTLB_OFFSET); 1757#endif /* APIC_IO */ 1758} 1759 1760void 1761invlpg(u_int addr) 1762{ 1763 __asm __volatile("invlpg (%0)"::"r"(addr):"memory"); 1764 1765 /* send a message to the other CPUs */ 1766 smp_invltlb(); 1767} 1768 1769void 1770invltlb(void) 1771{ 1772 u_long temp; 1773 1774 /* 1775 * This should be implemented as load_cr3(rcr3()) when load_cr3() is 1776 * inlined. 1777 */ 1778 __asm __volatile("movl %%cr3, %0; movl %0, %%cr3":"=r"(temp) :: "memory"); 1779 1780 /* send a message to the other CPUs */ 1781 smp_invltlb(); 1782} 1783 1784 1785/* 1786 * When called the executing CPU will send an IPI to all other CPUs 1787 * requesting that they halt execution. 1788 * 1789 * Usually (but not necessarily) called with 'other_cpus' as its arg. 1790 * 1791 * - Signals all CPUs in map to stop. 1792 * - Waits for each to stop. 1793 * 1794 * Returns: 1795 * -1: error 1796 * 0: NA 1797 * 1: ok 1798 * 1799 * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs 1800 * from executing at same time. 1801 */ 1802int 1803stop_cpus( u_int map ) 1804{ 1805 if (!smp_active) 1806 return 0; 1807 1808 /* send IPI to all CPUs in map */ 1809 stopped_cpus = 0; 1810 1811 /* send the Xcpustop IPI to all CPUs in map */ 1812 selected_apic_ipi(map, XCPUSTOP_OFFSET, APIC_DELMODE_FIXED); 1813 1814 while (stopped_cpus != map) 1815 /* spin */ ; 1816 1817 return 1; 1818} 1819 1820 1821/* 1822 * Called by a CPU to restart stopped CPUs. 1823 * 1824 * Usually (but not necessarily) called with 'stopped_cpus' as its arg. 1825 * 1826 * - Signals all CPUs in map to restart. 1827 * - Waits for each to restart. 1828 * 1829 * Returns: 1830 * -1: error 1831 * 0: NA 1832 * 1: ok 1833 */ 1834int 1835restart_cpus( u_int map ) 1836{ 1837 if (!smp_active) 1838 return 0; 1839 1840 started_cpus = map; /* signal other cpus to restart */ 1841 1842 while (started_cpus) /* wait for each to clear its bit */ 1843 /* spin */ ; 1844 1845 return 1; 1846} 1847