update for HEAD-2003091401
[reactos.git] / hal / halx86 / mp.c
index 01e026e..2f37c12 100644 (file)
@@ -82,7 +82,8 @@ extern VOID MpsSpuriousInterrupt(VOID);
    WRITE_PORT_UCHAR((PUCHAR)0x71, value); \
 })
 
-BOOLEAN MPSInitialized = FALSE;  /* Is the MP system initialized? */
+static BOOLEAN MPSInitialized = FALSE;  /* Is the MP system initialized? */
+static KDPC RescheduleDpc;
 
 VOID APICDisable(VOID);
 static VOID APICSyncArbIDs(VOID);
@@ -96,8 +97,7 @@ ULONG lastvalw = 0;
 #endif /* MP */
 
 
-BOOLEAN BSPInitialized = FALSE;  /* Is the BSP initialized? */
-
+static BOOLEAN BSPInitialized = FALSE;  /* Is the BSP initialized? */
 
 /* FUNCTIONS *****************************************************************/
 
@@ -148,12 +148,10 @@ volatile ULONG IOAPICRead(
   PULONG Base;
 
   Base = (PULONG)IOAPICMap[Apic].ApicAddress;
-
-       *Base = Offset;
-       return *((PULONG)((ULONG)Base + IOAPIC_IOWIN));
+  *Base = Offset;
+  return *((PULONG)((ULONG)Base + IOAPIC_IOWIN));
 }
 
-
 VOID IOAPICWrite(
    ULONG Apic,
    ULONG Offset,
@@ -162,9 +160,8 @@ VOID IOAPICWrite(
   PULONG Base;
 
   Base = (PULONG)IOAPICMap[Apic].ApicAddress;
-
   *Base = Offset;
-       *((PULONG)((ULONG)Base + IOAPIC_IOWIN)) = Value;
+  *((PULONG)((ULONG)Base + IOAPIC_IOWIN)) = Value;
 }
 
 
@@ -179,6 +176,7 @@ VOID IOAPICClearPin(
    */
        memset(&Entry, 0, sizeof(Entry));
        Entry.mask = 1;
+
        IOAPICWrite(Apic, IOAPIC_REDTBL + 2 * Pin, *(((PULONG)&Entry) + 0));
        IOAPICWrite(Apic, IOAPIC_REDTBL + 1 + 2 * Pin, *(((PULONG)&Entry) + 1));
 }
@@ -210,6 +208,7 @@ VOID IOAPICMaskIrq(
 
        *((PULONG)&Entry) = IOAPICRead(Apic, IOAPIC_REDTBL+2*Irq);
   Entry.mask = 1;
+
        IOAPICWrite(Apic, IOAPIC_REDTBL+2*Irq, *((PULONG)&Entry));
 }
 
@@ -223,6 +222,7 @@ VOID IOAPICUnmaskIrq(
 
   *((PULONG)&Entry) = IOAPICRead(Apic, IOAPIC_REDTBL+2*Irq);
   Entry.mask = 0;
+
   IOAPICWrite(Apic, IOAPIC_REDTBL+2*Irq, *((PULONG)&Entry));
 }
 
@@ -268,7 +268,7 @@ IOAPICSetupIds(VOID)
     
     tmp &= ~IOAPIC_ID_MASK;
     tmp |= SET_IOAPIC_ID(IOAPICMap[apic].ApicId);
-    
+
     IOAPICWrite(apic, IOAPIC_ID, tmp);
     
     /*
@@ -277,7 +277,7 @@ IOAPICSetupIds(VOID)
     tmp = IOAPICRead(apic, 0);
     if (GET_IOAPIC_ID(tmp) != IOAPICMap[apic].ApicId) {
       DPRINT1("Could not set I/O APIC ID!\n");
-      KeBugCheck(0);
+      KEBUGCHECK(0);
     }
   }
 }
@@ -550,7 +550,7 @@ static VOID AddPinToIrq(
                entry = irq_2_pin + entry->next;
                if (++first_free_entry >= PIN_MAP_SIZE) {
       DPRINT1("Ohh no!");
-                       KeBugCheck(0);
+                       KEBUGCHECK(0);
      }
        }
        entry->apic = apic;
@@ -582,25 +582,26 @@ static ULONG IOAPICGetIrqEntry(
 static ULONG AssignIrqVector(
   ULONG irq)
 {
-       static ULONG current_vector = FIRST_DEVICE_VECTOR, vector_offset = 0;
+  static ULONG current_vector = FIRST_DEVICE_VECTOR, vector_offset = 0;
   ULONG vector;
 
   /* There may already have been assigned a vector for this IRQ */
   vector = IRQVectorMap[irq];
-       if (vector > 0)
-               return vector;
+  if (vector > 0)
+    return vector;
 
-  current_vector += 8;
   if (current_vector > FIRST_SYSTEM_VECTOR) {
-               vector_offset++;
+      vector_offset++;
          current_vector = FIRST_DEVICE_VECTOR + vector_offset;
   } else if (current_vector == FIRST_SYSTEM_VECTOR) {
      DPRINT1("Ran out of interrupt sources!");
-     KeBugCheck(0);
+     KEBUGCHECK(0);
   }
 
-       IRQVectorMap[irq] = current_vector;
-       return current_vector;
+  vector = current_vector;
+  IRQVectorMap[irq] = vector;
+  current_vector += 8;
+  return vector;
 }
 
 
@@ -651,6 +652,8 @@ VOID IOAPICSetupIrqs(
        vector = AssignIrqVector(irq);
                entry.vector = vector;
 
+       DPRINT("vector 0x%.08x assigned to irq 0x%.02x\n", vector, irq);
+
     if (irq == 0)
     {
       /* Mask timer IRQ */
@@ -1167,7 +1170,6 @@ VOID WaitFor8254Wraparound(VOID)
        LONG Delta;
 
        CurCount = Read8254Timer();
-
        do {
                PrevCount = CurCount;
                CurCount = Read8254Timer();
@@ -1235,8 +1237,8 @@ VOID APICCalibrateTimer(
 
        /* Setup timer for normal operation */
   //APICSetupLVTT((CPUMap[CPU].BusSpeed / 1000000) * 100); // 100ns
-  APICSetupLVTT((CPUMap[CPU].BusSpeed / 1000000) * 15000); // 15ms
-  //APICSetupLVTT((CPUMap[CPU].BusSpeed / 1000000) * 100000); // 100ms
+//  APICSetupLVTT((CPUMap[CPU].BusSpeed / 1000000) * 15000); // 15ms
+  APICSetupLVTT((CPUMap[CPU].BusSpeed / 1000000) * 100000); // 100ms
 
   DPRINT("CPU clock speed is %ld.%04ld MHz.\n",
          CPUMap[CPU].CoreSpeed/1000000,
@@ -1374,37 +1376,52 @@ VOID MpsTimerHandler(
 {
 #if 0
   KIRQL OldIrql;
-#endif
 
-  DPRINT("T1");
+  DPRINT("T:\n");
 
   /*
-   * Acknowledge the interrupt
+   * Notify the rest of the kernel of the raised irq level
    */
-  APICSendEOI();
-#if 0
+  //OldIrql = KeRaiseIrqlToSynchLevel();
+  KeRaiseIrql(PROFILE_LEVEL, &OldIrql);
+
   /*
-   * Notify the rest of the kernel of the raised irq level
+   * Enable interrupts
+   * NOTE: Only higher priority interrupts will get through
    */
-  OldIrql = KeRaiseIrqlToSynchLevel();
-#endif
   __asm__("sti\n\t");
 
+  if (KeGetCurrentProcessorNumber() == 0)
+    {
+      //KIRQL OldIrql2;
+      //KeLowerIrql(PROFILE_LEVEL);
+      KiInterruptDispatch2(OldIrql, 0);
+      //KeRaiseIrql(CLOCK2_LEVEL, &OldIrql2);
+    }
+
+  DbgPrint("MpsTimerHandler() called at IRQL 0x%.08x\n", OldIrql);
+  //(BOOLEAN) KeInsertQueueDpc(&RescheduleDpc, NULL, NULL);
+
+       DbgPrint("MpsTimerHandler() -1 IRQL 0x%.08x\n", OldIrql);
+
   /*
-   * Call the dispatcher
+   * Disable interrupts
    */
-  // TODO FIXME - What happened to definition for PsDispatchThread ???
-  //PsDispatchThread(THREAD_STATE_READY);
+  __asm__("cli\n\t");
 
-  // KeGetCurrentThread is linked into hal from ntoskrnl, so can 
-  //    PsDispatchThread be exported from ntoskrnl also ???
+  DbgPrint("MpsTimerHandler() 0 IRQL 0x%.08x\n", OldIrql);
 
+  /*
+   * Acknowledge the interrupt
+   */
+  APICSendEOI();
 
-#if 0
   /*
    * Lower irq level
    */
+  DbgPrint("MpsTimerHandler() 1 IRQL 0x%.08x\n", OldIrql);
   KeLowerIrql(OldIrql);
+  DbgPrint("MpsTimerHandler() 2 IRQL 0x%.08x\n", OldIrql);
 #endif
 }
 
@@ -1445,10 +1462,8 @@ VOID MpsSpuriousHandler(
 {
   DPRINT1("Spurious interrupt on CPU(%d)\n", ThisCPU());
 
-       /*
-   * Acknowledge the interrupt
-   */
-  APICSendEOI();
+  /* No need to send EOI here */
+
   APICDump();
   for (;;);
 }
@@ -1494,7 +1509,7 @@ VOID APICSetup(
        APICWrite(APIC_SIVR, tmp);
 
   /*
-   * Only the BP should see the LINT1 NMI signal, obviously.
+   * Only the BSP should see the LINT1 NMI signal, obviously.
    */
   if (CPU == 0)
                tmp = APIC_DM_NMI;
@@ -1544,7 +1559,7 @@ HaliInitBSP(
                DPRINT("APIC found\n");
        } else {
                DPRINT1("No APIC found\n");
-               KeBugCheck(0);
+               KEBUGCHECK(0);
        }
 
   CPUMap[BootCPU].MaxLVT = APICGetMaxLVT();
@@ -1744,7 +1759,8 @@ HalAllProcessorsStarted (
 
 #ifdef MP
 
-  return (NextCPU >= CPUCount);
+  //return (NextCPU >= CPUCount);
+  return (NextCPU >= 1);
 
 #else /* MP */
 
@@ -1954,7 +1970,7 @@ static VOID HaliMPIOApicInfo(PMP_CONFIGURATION_IOAPIC m)
     DPRINT("Max # of I/O APICs (%d) exceeded (found %d).\n",
       MAX_IOAPIC, IOAPICCount);
     DPRINT1("Recompile with bigger MAX_IOAPIC!.\n");
-    KeBugCheck(0);
+    KEBUGCHECK(0);
   }
   IOAPICMap[IOAPICCount].ApicId = m->ApicId;
   IOAPICMap[IOAPICCount].ApicVersion = m->ApicVersion;
@@ -1971,7 +1987,7 @@ static VOID HaliMPIntSrcInfo(PMP_CONFIGURATION_INTSRC m)
     m->SrcBusIrq, m->DstApicId, m->DstApicInt);
   if (IRQCount > MAX_IRQ_SOURCE) {
     DPRINT1("Max # of irq sources exceeded!!\n");
-    KeBugCheck(0);
+    KEBUGCHECK(0);
   }
 
   IRQMap[IRQCount] = *m;
@@ -1994,11 +2010,11 @@ static VOID HaliMPIntLocalInfo(PMP_CONFIGURATION_INTLOCAL m)
    */
   if ((m->IrqType == INT_EXTINT) && (m->DstApicLInt != 0)) {
     DPRINT1("Invalid MP table!\n");
-    KeBugCheck(0);
+    KEBUGCHECK(0);
   }
   if ((m->IrqType == INT_NMI) && (m->DstApicLInt != 1)) {
     DPRINT1("Invalid MP table!\n");
-    KeBugCheck(0);
+    KEBUGCHECK(0);
   }
 }
 
@@ -2020,14 +2036,14 @@ HaliReadMPConfigTable(
        
        DbgPrint("Bad MP configuration block signature: %c%c%c%c\n", 
                pc[0], pc[1], pc[2], pc[3]);
-       KeBugCheck(0);
+       KEBUGCHECK(0);
        return;
      }
 
    if (MPChecksum((PUCHAR)Table, Table->Length))
      {
        DbgPrint("Bad MP configuration block checksum\n");
-       KeBugCheck(0);
+       KEBUGCHECK(0);
        return;
      }
 
@@ -2035,7 +2051,7 @@ HaliReadMPConfigTable(
      {
        DbgPrint("Bad MP configuration table version (%d)\n",
                Table->Specification);
-       KeBugCheck(0);
+       KEBUGCHECK(0);
        return;
      }
 
@@ -2044,7 +2060,7 @@ HaliReadMPConfigTable(
      {
        DbgPrint("APIC base address is at 0x%X. " \
                "I cannot handle non-standard adresses\n", APICBase);
-       KeBugCheck(0);
+       KEBUGCHECK(0);
      }
 
    Entry = (PUCHAR)((PVOID)Table + sizeof(MP_CONFIGURATION_TABLE));
@@ -2091,7 +2107,7 @@ HaliReadMPConfigTable(
         }
        default:
         DbgPrint("Unknown entry in MPC table\n");
-        KeBugCheck(0);
+        KEBUGCHECK(0);
        }
    }
 }
@@ -2214,6 +2230,7 @@ static VOID HaliConstructDefaultISAMPTable(
   }
 }
 
+
 BOOLEAN
 HaliScanForMPConfigTable(
    ULONG Base,
@@ -2301,6 +2318,22 @@ HaliScanForMPConfigTable(
 }
 
 
+static VOID STDCALL
+RescheduleDpcRoutine(PKDPC Dpc, PVOID DeferredContext,
+  PVOID SystemArgument1, PVOID SystemArgument2)
+{
+  KIRQL OldIrql;
+  KIRQL NewIrql;
+
+  DbgPrint("RDR()");
+  NewIrql = KeGetCurrentIrql();
+  KeLowerIrql(APC_LEVEL);
+  KeRescheduleThread();
+  KeRaiseIrql(NewIrql, &OldIrql);
+  DbgPrint("...\n");
+}
+
+
 VOID
 HalpInitMPS(
    VOID)
@@ -2328,6 +2361,8 @@ HalpInitMPS(
 
   MPSInitialized = TRUE;
 
+  KeInitializeDpc(&RescheduleDpc, RescheduleDpcRoutine, NULL);
+
   /*
      Scan the system memory for an MP configuration table
        1) Scan the first KB of system base memory
@@ -2343,14 +2378,14 @@ HalpInitMPS(
         EBDA <<= 4;
         if (!HaliScanForMPConfigTable((ULONG)EBDA, 0x1000)) {
           DbgPrint("No multiprocessor compliant system found.\n");
-          KeBugCheck(0);
+          KEBUGCHECK(0);
         }
       }
     }
   }
 
   /* Setup IRQ to vector translation map */
-  memset(&IRQVectorMap, sizeof(IRQVectorMap), 0);
+  memset(&IRQVectorMap, 0, sizeof(IRQVectorMap));
 
   /* Initialize the bootstrap processor */
   HaliInitBSP();