Guest User

Untitled

a guest
Jun 10th, 2012
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.93 KB | None | 0 0
  1.  
  2. /*
  3.  *  xapic.c
  4.  *  Advanced Programmable Interrupt Controller
  5.  */
  6.  
  7. #include <os/driver.h>
  8. #include <cpu/xapic.h>
  9. #include <cpu/cpu.h>
  10. #include <misc/mp.h>
  11.  
  12. #include <stdio.h>
  13.  
  14. #define DESTMODE_FIXED          0x00
  15. #define DESTMODE_LOW_PRIORITY   0x01
  16. #define DESTMODE_SMI            0x02
  17. #define DESTMODE_NMI            0x04
  18. #define DESTMODE_INIT           0x05
  19. #define DESTMODE_EXTERNAL       0x07
  20.  
  21. #define BuildRedirectionEntry(DEST, DESTMODE, DISABLED, ISPCI, VECT)        \
  22.         ((uint64)(                                                          \
  23.             (((uint64)DEST) << 56) |                                        \
  24.             (((uint64)DISABLED) << 16) |                                    \
  25.             (((ISPCI == 1) ? (uint64)((1 << 15) | (1 << 13)) : 0)) |        \
  26.             (((uint64)DESTMODE) << 11) |                                    \
  27.             (((uint64)VECT))                                                \
  28.         ))
  29.  
  30. // For visibility in apic.asm
  31. void* volatile lapicBase = NULL;
  32. void* volatile ioapicBase = NULL;
  33.  
  34. static uint16 ioapicMaxRedirectTableEntry = 0;
  35.  
  36. // volatile keyword here SHOULD keep GCC from optimizing out the access order
  37. static inline void XIOAPICWriteReg(const volatile void* base, uint8 offset, uint32 value) {
  38.     *(uint32*)base = offset;
  39.     *(uint32*)((paddr)base + 0x10) = value;
  40. }
  41.  
  42. static inline uint32 XIOAPICReadReg(const volatile void* base, uint8 offset) {
  43.     *(uint32*)base = offset;
  44.    
  45.     return *(uint32*)((paddr)base + 0x10);
  46. }
  47.  
  48. static inline void XLAPICWriteReg(const volatile void* base, uint16 offset, uint32 value) {
  49.     *(uint32*)((paddr)base + offset) = value;
  50. }
  51.  
  52. static inline uint32 XLAPICReadReg(const volatile void* base, uint16 offset) {
  53.     return *(uint32*)((paddr)base + offset);
  54. }
  55.  
  56. static void XLAPICInitialize(void* lapicAddress) {
  57.     uint32 sivr, lvttr, pcr;
  58.    
  59.     // Sanity check
  60.     if (lapicAddress == NULL)
  61.         return;
  62.    
  63.     // Configure local APIC
  64.     XLAPICWriteReg(lapicAddress, LAPIC_MM_REG_TASK_PRIORITY, 0x20);                 // Clear task priority
  65.     //XLAPICWriteReg(lapicAddress, LAPIC_MM_REG_LOGICAL_DESTINATION, 0x01000000);       // Logical Destination
  66.     //XLAPICWriteReg(lapicAddress, LAPIC_MM_REG_DESTINATION_FORMAT, 0xFFFFFFFF);        // Destination Format Register
  67.    
  68.     // Disable timer
  69.     lvttr = XLAPICReadReg(lapicAddress, LAPIC_MM_REG_LVT_TIMER);
  70.     lvttr |= 0x00010000;
  71.     XLAPICWriteReg(lapicAddress, LAPIC_MM_REG_LVT_TIMER, lvttr);
  72.    
  73.     // Disable performance counter
  74.     pcr = XLAPICReadReg(lapicAddress, 0x340);
  75.     pcr |= 0x00010000;
  76.     XLAPICWriteReg(lapicAddress, 0x340, pcr);
  77.    
  78.     XLAPICWriteReg(lapicAddress, 0x350, XLAPICReadReg(lapicAddress, 0x350) | 0x00008700);
  79.     XLAPICWriteReg(lapicAddress, 0x360, XLAPICReadReg(lapicAddress, 0x360) | 0x00000400);
  80.     XLAPICWriteReg(lapicAddress, 0x370, XLAPICReadReg(lapicAddress, 0x370) | 0x00010000);
  81.    
  82.     // Enable local APIC
  83.     sivr = XLAPICReadReg(lapicAddress, LAPIC_MM_REG_SPURIOUS_INTERRUPT);
  84.     sivr = (sivr & 0xFFFFFF00) | 0x0000000F;
  85.     sivr |= 0x00000100;
  86.     XLAPICWriteReg(lapicAddress, LAPIC_MM_REG_SPURIOUS_INTERRUPT, sivr);
  87. }
  88.  
  89. void XAPICInstallRedirectionEntry(uint16 index, uint64 value) {
  90.     // Sanity check
  91.     if (ioapicBase == NULL || index >= ioapicMaxRedirectTableEntry)
  92.         return;
  93.        
  94.     kprintf("VALUE = %016X\n", value);
  95.    
  96.     // Safe gaurd entry
  97.     XIOAPICWriteReg((const void*)ioapicBase, (index << 1) + 0x10 + 1, 0x00010000);
  98.    
  99.     // Write
  100.     XIOAPICWriteReg((const void*)ioapicBase, (index << 1) + 0x10, value & 0x00000000FFFFFFFF);
  101.     XIOAPICWriteReg((const void*)ioapicBase, (index << 1) + 0x10 + 1, (value & 0xFFFFFFFF00000000) >> 32);
  102. }
  103.  
  104. /*
  105.  *  int APicInitializeWithMP(uint16* coreCount);
  106.  *  Arguments:
  107.  *      coreCount   Core Count
  108.  *  Returns:
  109.  *       0          Success
  110.  *      -1          No APIC detected
  111.  *      -2          No MP support
  112.  */
  113. static int XAPICInitializeWithMP(uint16* coreCount) {
  114.     size_t i, j, ioapicInitCount = 0;
  115.    
  116.     // Get configuration table
  117.     MPConfigurationTable* configurationTable = MpGetConfigurationTable();
  118.     if (configurationTable == NULL)
  119.         return -2;
  120.    
  121.     if (!configurationTable->lapicAddress)
  122.         return -1;
  123.        
  124.     // Disable PIC before we do anything
  125.     outb(0x21, 0xFF);
  126.     outb(0xA1, 0xFF);
  127.    
  128.     // Enable each IOAPIC -- Verify every IOAPIC has a unique id beforehand?
  129.     j = MpGetEntryCount(configurationTable);
  130.    
  131.     // This for loop is used instead of a while() because it's more likely that an IOAPIC entry is lower in the list (saves time)
  132.     for(i = 0; i < j; ++i) {
  133.         MPEntry* entry;
  134.        
  135.         // Get entry
  136.         if (!(entry = MpGetEntry(configurationTable, i)))
  137.             continue;
  138.        
  139.         if (entry->type == MP_ET_PROCESSOR)         // Processor entry
  140.             ++*coreCount;
  141.         else if (entry->type == MP_ET_IOAPIC) {     // IOAPIC entry
  142.             uint32 entries;
  143.            
  144.             // Only initialize a single IOAPIC and IOAPICs marked as usable
  145.             if (ioapicInitCount > 0 || !(entry->ioapic.flags & MP_IOAPIC_FLAG_UNUSABLE))
  146.                 continue;
  147.            
  148.             kprintf("Initializing IOAPIC %i (flags = 0x%04X)\n", entry->ioapic.id, entry->ioapic.flags);
  149.            
  150.             ioapicBase = (void*)(paddr)entry->ioapic.address;
  151.            
  152.             // Grab IOAPIC redirection entries
  153.             entries = ((XIOAPICReadReg((const void*)ioapicBase, IOAPIC_REG_VERSION) >> 16) & 0xFF) + 1;
  154.             ioapicMaxRedirectTableEntry = entries;
  155.            
  156.             kprintf("ENTRIES = %i\n\n", entries);
  157.            
  158.             // Program first 16 entries
  159.             if (entries >= 16) {
  160.                 XAPICInstallRedirectionEntry(INT_PIT, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 0, 0, IRQ_PIT));
  161.                 XAPICInstallRedirectionEntry(INT_KEYBOARD, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 0, 0, IRQ_KEYBOARD));
  162.                 XAPICInstallRedirectionEntry(INT_CASCADE, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 1, 0, IRQ_CASCADE));
  163.                 XAPICInstallRedirectionEntry(INT_COM1, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 0, 0, IRQ_COM1));
  164.                 XAPICInstallRedirectionEntry(INT_COM2, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 0, 0, IRQ_COM2));
  165.                 XAPICInstallRedirectionEntry(INT_LPT2, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 0, 0, IRQ_LPT2));
  166.                 XAPICInstallRedirectionEntry(INT_FLOPPY_DISK, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 0, 0, IRQ_FLOPPY_DISK));
  167.                 XAPICInstallRedirectionEntry(INT_LPT1, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 0, 0, IRQ_LPT1));
  168.                 XAPICInstallRedirectionEntry(INT_CMOS_RTC, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 0, 0, IRQ_CMOS_RTC));
  169.                 XAPICInstallRedirectionEntry(INT_MOUSE, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 0, 0, IRQ_MOUSE));
  170.                 XAPICInstallRedirectionEntry(INT_FPU, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 0, 0, IRQ_FPU));
  171.                 XAPICInstallRedirectionEntry(INT_HDD_PRIMARY, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 0, 0, IRQ_HDD_PRIMARY));
  172.                 XAPICInstallRedirectionEntry(INT_HDD_SECONDARY, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 0, 0, IRQ_HDD_SECONDARY));
  173.             }
  174.            
  175.             // Program PCI entries
  176.             if (entries >= 0x13) {
  177.                 // Depends on BIOS implementation, for now just disable them (Until I figure out how to detect these values safely)
  178.                 XAPICInstallRedirectionEntry(INT_PCI_A, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 1, 1, 0x00));
  179.                 XAPICInstallRedirectionEntry(INT_PCI_B, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 1, 1, 0x00));
  180.                 XAPICInstallRedirectionEntry(INT_PCI_C, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 1, 1, 0x00));
  181.                 XAPICInstallRedirectionEntry(INT_PCI_D, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 1, 1, 0x00));
  182.             }
  183.            
  184.             // Disable last entries
  185.             if (entries >= 0x17) {
  186.                 XAPICInstallRedirectionEntry(20, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 1, 0, 0x00));
  187.                 XAPICInstallRedirectionEntry(21, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 1, 0, 0x00));
  188.                 XAPICInstallRedirectionEntry(22, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 1, 0, 0x00));
  189.                 XAPICInstallRedirectionEntry(23, BuildRedirectionEntry(entry->ioapic.id, DESTMODE_FIXED, 1, 0, 0x00));
  190.             }
  191.            
  192.             // Increase the number of times we've initialized an IOAPIC
  193.             ++ioapicInitCount;
  194.         }
  195.     }
  196.    
  197.     // Enable Symetrical IO mode if we're not in it already
  198.     outb(0x22, 0x70);
  199.     outb(0x23, 0x01);
  200.    
  201.     // Configure & enable local APIC
  202.     lapicBase = (void*)(paddr)configurationTable->lapicAddress;
  203.     XLAPICInitialize(lapicBase);
  204.    
  205.     return (ioapicInitCount > 0) ? 0 : -1;
  206. }
  207.  
  208. /*
  209.  *  int APicInitializeWithACPI(uint16* coreCount);
  210.  *  Arguments:
  211.  *      coreCount   Core Count
  212.  *  Returns:
  213.  *       0          Success
  214.  *      -1          No APIC detected
  215.  *      -2          No ACPI support
  216.  */
  217. static int XAPICInitializeWithACPI(uint16* coreCount) {
  218.     UNUSED_PARAMETER(coreCount);
  219.    
  220.     return -1;
  221. }
  222.  
  223. /*
  224.  *  int APicInitialize(uint16* coreCount);
  225.  *  Arguments:
  226.  *      coreCount   Core Count
  227.  *  Returns:
  228.  *       0          Success
  229.  *      -1          No APIC detected
  230.  *      -2          MP/ACPI not supported on this system
  231.  */
  232. int XAPICInitialize(uint16* coreCount) {
  233.     size_t a, d;
  234.     int i;
  235.    
  236.     // Check if APIC is on this machine
  237.     CpuGetId(CPUID_GETFEATURES, &a, &d);
  238.     if (!(d & CPUID_FEAT_EDX_APIC))
  239.         return -1;
  240.  
  241.     // Attempt to initialize with MP
  242.     i = XAPICInitializeWithMP(coreCount);
  243.    
  244.     // If MP initialization fails try to initialize with ACPI
  245.     if (i != 0) {
  246.         *coreCount = 0;
  247.         i = XAPICInitializeWithACPI(coreCount);
  248.     }
  249.  
  250.     return i;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment