iso9660 now mostly works.
[planlOS.git] / system / kernel / ke / apic.c
blobdb0bf0d478fc2b6a5e6d25b3db7808b21cb32583
1 /*
2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include "ke/apic.h"
23 #include "ke/cpu.h"
24 #include "ke/debug.h"
25 #include "mm/memory.h"
27 static uintptr_t apic_base_phys = 0;
28 static uintptr_t apic_base = 0;
30 int keAPICInit(void)
32 // TODO: Check whether APIC is available
33 // Get physical address
34 //keWriteMSR(0x1B, 0xFEE00000);
35 apic_base_phys = keReadMSR(0x1B) & ~0xFFF;
36 kePrint("APIC: Address: %X\n", apic_base_phys);
38 // Map mmio space into virtual address space
39 /*apic_base = halFindContinuousPages(kernel_directory, 1, 0xE0000000, 0xFFFFFFFF);
40 halMapPage(kernel_directory, apic_base, apic_base_phys & ~0xFFF, 0x3);
41 apic_base = apic_base + (apic_base_phys & 0xFFF);*/
43 apic_base = mmFindFreeKernelPages(MM_MAX_KERNEL_PAGE,
44 MM_MIN_KERNEL_PAGE, 0, 0x1000);
45 mmMapKernelMemory(apic_base_phys, apic_base,
46 MM_MAP_READ | MM_MAP_WRITE | MM_MAP_UNCACHEABLE);
48 /*kePrint("ID: %X, version: %X\n", keAPICRead(0x20), keAPICRead(0x30));
50 keAPICWrite(0x80, 0x20);
51 //halAPICWrite(0x320, 0x10000);
52 //halAPICWrite(0x340, 0x10000);
53 keAPICWrite(0x350, 0x08700);
54 keAPICWrite(0x360, 0x400);
55 keAPICWrite(0x370, 0x10000);
56 keAPICWrite(0xF0, 0x10F);
57 keAPICWrite(0x350, 0x08700);
58 keAPICWrite(0x360, 0x400);*/
59 // Activate APIC
60 keAPICWrite(0xF0, 0x10F);
62 // Timer
63 /*keAPICWrite(0x3e0, 0xb);
64 keAPICWrite(0x320, 0x20040);
65 keAPICWrite(0x380, 0x100);
67 int i;
68 for (i = 0; i < 100000; i++)
70 keAPICRead(0x380);
72 kePrint("APIC Timer: 0x%X\n", keAPICRead(0x390));*/
75 return 1;
78 uint32_t keAPICGetCPU(void)
80 uint32_t cpuinfo[4];
81 keCPUID(1, cpuinfo);
82 return cpuinfo[1] >> 24;
85 void keAPICWrite(uint16_t offset, uint32_t value)
87 *((volatile uint32_t*)(apic_base + offset)) = value;
89 uint32_t keAPICRead(uint16_t offset)
91 return *((volatile uint32_t*)(apic_base + offset));
94 void keAPICWriteInit(void)
96 if (!apic_base) return;
97 keAPICWrite(0x310, 0);
98 keAPICWrite(0x300, 0xC4500);
100 void keAPICWriteStartup(uintptr_t entry)
102 if (!apic_base) return;
103 keAPICWrite(0x310, 0);
104 keAPICWrite(0x300, 0xC4600 | ((entry / 0x1000) && 0xFF));
106 void keAPICBroadcast(uint8_t intno, int self)
108 if (!apic_base) return;
109 keAPICWrite(0x310, 0);
110 if (self)
111 keAPICWrite(0x300, intno | 0x4000 | (2 << 18));
112 else
113 keAPICWrite(0x300, intno | 0x4000 | (3 << 18));