Changeset da1bafb in mainline for kernel/arch/ia32/src/smp/apic.c


Ignore:
Timestamp:
2010-05-24T18:57:31Z (14 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0095368
Parents:
666f492
Message:

major code revision

  • replace spinlocks taken with interrupts disabled with irq_spinlocks
  • change spacing (not indendation) to be tab-size independent
  • use unsigned integer types where appropriate (especially bit flags)
  • visual separation
  • remove argument names in function prototypes
  • string changes
  • correct some formating directives
  • replace various cryptic single-character variables (t, a, m, c, b, etc.) with proper identifiers (thread, task, timeout, as, itm, itc, etc.)
  • unify some assembler constructs
  • unused page table levels are now optimized out in compile time
  • replace several ints (with boolean semantics) with bools
  • use specifically sized types instead of generic types where appropriate (size_t, uint32_t, btree_key_t)
  • improve comments
  • split asserts with conjuction into multiple independent asserts
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/arch/ia32/src/smp/apic.c

    r666f492 rda1bafb  
    5353 * Advanced Programmable Interrupt Controller for SMP systems.
    5454 * Tested on:
    55  *      Bochs 2.0.2 - Bochs 2.2.6 with 2-8 CPUs
    56  *      Simics 2.0.28 - Simics 2.2.19 2-15 CPUs
    57  *      VMware Workstation 5.5 with 2 CPUs
    58  *      QEMU 0.8.0 with 2-15 CPUs
    59  *      ASUS P/I-P65UP5 + ASUS C-P55T2D REV. 1.41 with 2x 200Mhz Pentium CPUs
    60  *      ASUS PCH-DL with 2x 3000Mhz Pentium 4 Xeon (HT) CPUs
    61  *      MSI K7D Master-L with 2x 2100MHz Athlon MP CPUs
     55 *    Bochs 2.0.2 - Bochs 2.2.6 with 2-8 CPUs
     56 *    Simics 2.0.28 - Simics 2.2.19 2-15 CPUs
     57 *    VMware Workstation 5.5 with 2 CPUs
     58 *    QEMU 0.8.0 with 2-15 CPUs
     59 *    ASUS P/I-P65UP5 + ASUS C-P55T2D REV. 1.41 with 2x 200Mhz Pentium CPUs
     60 *    ASUS PCH-DL with 2x 3000Mhz Pentium 4 Xeon (HT) CPUs
     61 *    MSI K7D Master-L with 2x 2100MHz Athlon MP CPUs
     62 *
    6263 */
    6364
     
    6970 * optimize the code too much and accesses to l_apic and io_apic, that must
    7071 * always be 32-bit, would use byte oriented instructions.
     72 *
    7173 */
    7274volatile uint32_t *l_apic = (uint32_t *) 0xfee00000;
     
    7981
    8082#ifdef LAPIC_VERBOSE
    81 static char *delmod_str[] = {
     83static const char *delmod_str[] = {
    8284        "Fixed",
    8385        "Lowest Priority",
     
    9092};
    9193
    92 static char *destmod_str[] = {
     94static const char *destmod_str[] = {
    9395        "Physical",
    9496        "Logical"
    9597};
    9698
    97 static char *trigmod_str[] = {
     99static const char *trigmod_str[] = {
    98100        "Edge",
    99101        "Level"
    100102};
    101103
    102 static char *mask_str[] = {
     104static const char *mask_str[] = {
    103105        "Unmasked",
    104106        "Masked"
    105107};
    106108
    107 static char *delivs_str[] = {
     109static const char *delivs_str[] = {
    108110        "Idle",
    109111        "Send Pending"
    110112};
    111113
    112 static char *tm_mode_str[] = {
     114static const char *tm_mode_str[] = {
    113115        "One-shot",
    114116        "Periodic"
    115117};
    116118
    117 static char *intpol_str[] = {
     119static const char *intpol_str[] = {
    118120        "Polarity High",
    119121        "Polarity Low"
     
    123125/** APIC spurious interrupt handler.
    124126 *
    125  * @param n Interrupt vector.
     127 * @param n      Interrupt vector.
    126128 * @param istate Interrupted state.
    127  */
    128 static void apic_spurious(int n __attribute__((unused)), istate_t *istate __attribute__((unused)))
     129 *
     130 */
     131static void apic_spurious(int n __attribute__((unused)),
     132    istate_t *istate __attribute__((unused)))
    129133{
    130134#ifdef CONFIG_DEBUG
     
    145149         * irq->lock so we just unlock it and then lock it again.
    146150         */
    147         spinlock_unlock(&irq->lock);
     151        irq_spinlock_unlock(&irq->lock, false);
    148152        clock();
    149         spinlock_lock(&irq->lock);
     153        irq_spinlock_lock(&irq->lock, false);
    150154}
    151155
     
    153157void apic_init(void)
    154158{
    155         io_apic_id_t idreg;
    156        
    157159        exc_register(VECTOR_APIC_SPUR, "apic_spurious", (iroutine) apic_spurious);
    158 
     160       
    159161        enable_irqs_function = io_apic_enable_irqs;
    160162        disable_irqs_function = io_apic_disable_irqs;
     
    179181        for (i = 0; i < IRQ_COUNT; i++) {
    180182                int pin;
    181        
     183               
    182184                if ((pin = smp_irq_to_pin(i)) != -1)
    183185                        io_apic_change_ioredtbl((uint8_t) pin, DEST_ALL, (uint8_t) (IVT_IRQBASE + i), LOPRI);
     
    187189         * Ensure that io_apic has unique ID.
    188190         */
     191        io_apic_id_t idreg;
     192       
    189193        idreg.value = io_apic_read(IOAPICID);
    190         if ((1 << idreg.apic_id) & apic_id_mask) {      /* see if IO APIC ID is used already */
     194        if ((1 << idreg.apic_id) & apic_id_mask) {  /* See if IO APIC ID is used already */
    191195                for (i = 0; i < APIC_ID_COUNT; i++) {
    192196                        if (!((1 << i) & apic_id_mask)) {
     
    197201                }
    198202        }
    199 
     203       
    200204        /*
    201205         * Configure the BSP's lapic.
    202206         */
    203207        l_apic_init();
    204 
    205         l_apic_debug();
     208        l_apic_debug();
    206209}
    207210
     
    211214 *
    212215 * @return 0 on error, 1 on success.
     216 *
    213217 */
    214218int apic_poll_errors(void)
     
    232236        if (esr.illegal_register_address)
    233237                printf("Illegal Register Address\n");
    234 
     238       
    235239        return !esr.err_bitmap;
    236240}
     
    241245 *
    242246 * @return 0 on failure, 1 on success.
     247 *
    243248 */
    244249int l_apic_broadcast_custom_ipi(uint8_t vector)
    245250{
    246251        icr_t icr;
    247 
     252       
    248253        icr.lo = l_apic[ICRlo];
    249254        icr.delmod = DELMOD_FIXED;
     
    253258        icr.trigger_mode = TRIGMOD_LEVEL;
    254259        icr.vector = vector;
    255 
     260       
    256261        l_apic[ICRlo] = icr.lo;
    257 
     262       
    258263        icr.lo = l_apic[ICRlo];
    259264        if (icr.delivs == DELIVS_PENDING) {
     
    262267#endif
    263268        }
    264 
     269       
    265270        return apic_poll_errors();
    266271}
     
    271276 *
    272277 * @return 0 on failure, 1 on success.
     278 *
    273279 */
    274280int l_apic_send_init_ipi(uint8_t apicid)
    275281{
     282        /*
     283         * Read the ICR register in and zero all non-reserved fields.
     284         */
    276285        icr_t icr;
    277         int i;
    278 
    279         /*
    280          * Read the ICR register in and zero all non-reserved fields.
    281          */
     286       
    282287        icr.lo = l_apic[ICRlo];
    283288        icr.hi = l_apic[ICRhi];
     
    293298        l_apic[ICRhi] = icr.hi;
    294299        l_apic[ICRlo] = icr.lo;
    295 
     300       
    296301        /*
    297302         * According to MP Specification, 20us should be enough to
     
    299304         */
    300305        delay(20);
    301 
     306       
    302307        if (!apic_poll_errors())
    303308                return 0;
    304 
     309       
    305310        icr.lo = l_apic[ICRlo];
    306311        if (icr.delivs == DELIVS_PENDING) {
     
    309314#endif
    310315        }
    311 
     316       
    312317        icr.delmod = DELMOD_INIT;
    313318        icr.destmod = DESTMOD_PHYS;
     
    317322        icr.vector = 0;
    318323        l_apic[ICRlo] = icr.lo;
    319 
     324       
    320325        /*
    321326         * Wait 10ms as MP Specification specifies.
    322327         */
    323328        delay(10000);
    324 
     329       
    325330        if (!is_82489DX_apic(l_apic[LAVR])) {
    326331                /*
    327332                 * If this is not 82489DX-based l_apic we must send two STARTUP IPI's.
    328333                 */
    329                 for (i = 0; i<2; i++) {
     334                unsigned int i;
     335                for (i = 0; i < 2; i++) {
    330336                        icr.lo = l_apic[ICRlo];
    331337                        icr.vector = (uint8_t) (((uintptr_t) ap_boot) >> 12); /* calculate the reset vector */
     
    346352void l_apic_init(void)
    347353{
     354        /* Initialize LVT Error register. */
    348355        lvt_error_t error;
    349         lvt_lint_t lint;
    350         tpr_t tpr;
    351         svr_t svr;
    352         icr_t icr;
    353         tdcr_t tdcr;
    354         lvt_tm_t tm;
    355         ldr_t ldr;
    356         dfr_t dfr;
    357         uint32_t t1, t2;
    358 
    359         /* Initialize LVT Error register. */
     356       
    360357        error.value = l_apic[LVT_Err];
    361358        error.masked = true;
    362359        l_apic[LVT_Err] = error.value;
    363 
     360       
    364361        /* Initialize LVT LINT0 register. */
     362        lvt_lint_t lint;
     363       
    365364        lint.value = l_apic[LVT_LINT0];
    366365        lint.masked = true;
    367366        l_apic[LVT_LINT0] = lint.value;
    368 
     367       
    369368        /* Initialize LVT LINT1 register. */
    370369        lint.value = l_apic[LVT_LINT1];
    371370        lint.masked = true;
    372371        l_apic[LVT_LINT1] = lint.value;
    373 
     372       
    374373        /* Task Priority Register initialization. */
     374        tpr_t tpr;
     375       
    375376        tpr.value = l_apic[TPR];
    376377        tpr.pri_sc = 0;
     
    379380       
    380381        /* Spurious-Interrupt Vector Register initialization. */
     382        svr_t svr;
     383       
    381384        svr.value = l_apic[SVR];
    382385        svr.vector = VECTOR_APIC_SPUR;
     
    384387        svr.focus_checking = true;
    385388        l_apic[SVR] = svr.value;
    386 
     389       
    387390        if (CPU->arch.family >= 6)
    388391                enable_l_apic_in_msr();
    389392       
    390393        /* Interrupt Command Register initialization. */
     394        icr_t icr;
     395       
    391396        icr.lo = l_apic[ICRlo];
    392397        icr.delmod = DELMOD_INIT;
     
    398403       
    399404        /* Timer Divide Configuration Register initialization. */
     405        tdcr_t tdcr;
     406       
    400407        tdcr.value = l_apic[TDCR];
    401408        tdcr.div_value = DIVIDE_1;
    402409        l_apic[TDCR] = tdcr.value;
    403 
     410       
    404411        /* Program local timer. */
     412        lvt_tm_t tm;
     413       
    405414        tm.value = l_apic[LVT_Tm];
    406415        tm.vector = VECTOR_CLK;
     
    408417        tm.masked = false;
    409418        l_apic[LVT_Tm] = tm.value;
    410 
     419       
    411420        /*
    412421         * Measure and configure the timer to generate timer
    413422         * interrupt with period 1s/HZ seconds.
    414423         */
     424        uint32_t t1 = l_apic[CCRT];
     425        l_apic[ICRT] = 0xffffffff;
     426       
     427        while (l_apic[CCRT] == t1);
     428       
    415429        t1 = l_apic[CCRT];
    416         l_apic[ICRT] = 0xffffffff;
    417 
    418         while (l_apic[CCRT] == t1)
    419                 ;
    420                
    421         t1 = l_apic[CCRT];
    422         delay(1000000/HZ);
    423         t2 = l_apic[CCRT];
    424        
    425         l_apic[ICRT] = t1-t2;
     430        delay(1000000 / HZ);
     431        uint32_t t2 = l_apic[CCRT];
     432       
     433        l_apic[ICRT] = t1 - t2;
    426434       
    427435        /* Program Logical Destination Register. */
    428436        ASSERT(CPU->id < 8);
     437        ldr_t ldr;
     438       
    429439        ldr.value = l_apic[LDR];
    430440        ldr.id = (uint8_t) (1 << CPU->id);
     
    432442       
    433443        /* Program Destination Format Register for Flat mode. */
     444        dfr_t dfr;
     445       
    434446        dfr.value = l_apic[DFR];
    435447        dfr.model = MODEL_FLAT;
     
    447459{
    448460#ifdef LAPIC_VERBOSE
     461        printf("LVT on cpu%" PRIs ", LAPIC ID: %" PRIu8 "\n", CPU->id, l_apic_id());
     462       
    449463        lvt_tm_t tm;
    450         lvt_lint_t lint;
    451         lvt_error_t error;     
    452        
    453         printf("LVT on cpu%d, LAPIC ID: %d\n", CPU->id, l_apic_id());
    454 
    455464        tm.value = l_apic[LVT_Tm];
    456465        printf("LVT Tm: vector=%hhd, %s, %s, %s\n", tm.vector, delivs_str[tm.delivs], mask_str[tm.masked], tm_mode_str[tm.mode]);
     466       
     467        lvt_lint_t lint;
    457468        lint.value = l_apic[LVT_LINT0];
    458469        printf("LVT LINT0: vector=%hhd, %s, %s, %s, irr=%d, %s, %s\n", tm.vector, delmod_str[lint.delmod], delivs_str[lint.delivs], intpol_str[lint.intpol], lint.irr, trigmod_str[lint.trigger_mode], mask_str[lint.masked]);
    459470        lint.value = l_apic[LVT_LINT1];
    460471        printf("LVT LINT1: vector=%hhd, %s, %s, %s, irr=%d, %s, %s\n", tm.vector, delmod_str[lint.delmod], delivs_str[lint.delivs], intpol_str[lint.intpol], lint.irr, trigmod_str[lint.trigger_mode], mask_str[lint.masked]); 
     472       
     473        lvt_error_t error;
    461474        error.value = l_apic[LVT_Err];
    462475        printf("LVT Err: vector=%hhd, %s, %s\n", error.vector, delivs_str[error.delivs], mask_str[error.masked]);
     
    467480 *
    468481 * @return Local APIC ID.
     482 *
    469483 */
    470484uint8_t l_apic_id(void)
     
    481495 *
    482496 * @return Content of the addressed IO APIC register.
     497 *
    483498 */
    484499uint32_t io_apic_read(uint8_t address)
     
    495510 *
    496511 * @param address IO APIC register address.
    497  * @param x Content to be written to the addressed IO APIC register.
    498  */
    499 void io_apic_write(uint8_t address, uint32_t x)
     512 * @param val     Content to be written to the addressed IO APIC register.
     513 *
     514 */
     515void io_apic_write(uint8_t address, uint32_t val)
    500516{
    501517        io_regsel_t regsel;
     
    504520        regsel.reg_addr = address;
    505521        io_apic[IOREGSEL] = regsel.value;
    506         io_apic[IOWIN] = x;
     522        io_apic[IOWIN] = val;
    507523}
    508524
    509525/** Change some attributes of one item in I/O Redirection Table.
    510526 *
    511  * @param pin IO APIC pin number.
    512  * @param dest Interrupt destination address.
    513  * @param v Interrupt vector to trigger.
     527 * @param pin   IO APIC pin number.
     528 * @param dest  Interrupt destination address.
     529 * @param vec  Interrupt vector to trigger.
    514530 * @param flags Flags.
    515  */
    516 void io_apic_change_ioredtbl(uint8_t pin, uint8_t dest, uint8_t v, int flags)
    517 {
    518         io_redirection_reg_t reg;
    519         int dlvr = DELMOD_FIXED;
     531 *
     532 */
     533void io_apic_change_ioredtbl(uint8_t pin, uint8_t dest, uint8_t vec,
     534    unsigned int flags)
     535{
     536        unsigned int dlvr;
    520537       
    521538        if (flags & LOPRI)
    522539                dlvr = DELMOD_LOWPRI;
    523 
     540        else
     541                dlvr = DELMOD_FIXED;
     542       
     543        io_redirection_reg_t reg;
    524544        reg.lo = io_apic_read((uint8_t) (IOREDTBL + pin * 2));
    525545        reg.hi = io_apic_read((uint8_t) (IOREDTBL + pin * 2 + 1));
     
    530550        reg.intpol = POLARITY_HIGH;
    531551        reg.delmod = dlvr;
    532         reg.intvec = v;
    533 
     552        reg.intvec = vec;
     553       
    534554        io_apic_write((uint8_t) (IOREDTBL + pin * 2), reg.lo);
    535555        io_apic_write((uint8_t) (IOREDTBL + pin * 2 + 1), reg.hi);
     
    539559 *
    540560 * @param irqmask Bitmask of IRQs to be masked (0 = do not mask, 1 = mask).
     561 *
    541562 */
    542563void io_apic_disable_irqs(uint16_t irqmask)
    543564{
    544         io_redirection_reg_t reg;
    545565        unsigned int i;
    546         int pin;
    547        
    548566        for (i = 0; i < 16; i++) {
    549567                if (irqmask & (1 << i)) {
     
    552570                         * mapping for the respective IRQ number.
    553571                         */
    554                         pin = smp_irq_to_pin(i);
     572                        int pin = smp_irq_to_pin(i);
    555573                        if (pin != -1) {
     574                                io_redirection_reg_t reg;
     575                               
    556576                                reg.lo = io_apic_read((uint8_t) (IOREDTBL + pin * 2));
    557577                                reg.masked = true;
     
    566586 *
    567587 * @param irqmask Bitmask of IRQs to be unmasked (0 = do not unmask, 1 = unmask).
     588 *
    568589 */
    569590void io_apic_enable_irqs(uint16_t irqmask)
    570591{
    571592        unsigned int i;
    572         int pin;
    573         io_redirection_reg_t reg;       
    574        
    575593        for (i = 0; i < 16; i++) {
    576594                if (irqmask & (1 << i)) {
     
    579597                         * mapping for the respective IRQ number.
    580598                         */
    581                         pin = smp_irq_to_pin(i);
     599                        int pin = smp_irq_to_pin(i);
    582600                        if (pin != -1) {
     601                                io_redirection_reg_t reg;
     602                               
    583603                                reg.lo = io_apic_read((uint8_t) (IOREDTBL + pin * 2));
    584604                                reg.masked = false;
Note: See TracChangeset for help on using the changeset viewer.