2 * Acess2 Kernel (x86 Core)
3 * - By John Hodge (thePowersGang)
8 #define ACPI_DEBUG_OUTPUT 0
10 #define _AcpiModuleName "Shim"
11 #define _COMPONENT "Acess"
15 #include <semaphore.h>
17 #define ONEMEG (1024*1024)
20 // - RSDP Address from uEFI
21 tPAddr gACPI_RSDPOverride = 0;
24 int ACPICA_Initialise(void);
25 void ACPI_int_InterruptProxy(int IRQ, void *data);
29 int ACPICA_Initialise(void)
33 #ifdef ACPI_DEBUG_OUTPUT
34 AcpiDbgLevel = ACPI_DB_ALL;
37 rv = AcpiInitializeSubsystem();
38 if( ACPI_FAILURE(rv) )
40 Log_Error("ACPI", "AcpiInitializeSubsystem: %i", rv);
44 rv = AcpiInitializeTables(NULL, 16, FALSE);
45 if( ACPI_FAILURE(rv) )
47 Log_Error("ACPI", "AcpiInitializeTables: %i", rv);
52 // AcpiInitializeTables?
53 rv = AcpiLoadTables();
54 if( ACPI_FAILURE(rv) )
56 Log_Error("ACPI", "AcpiLoadTables: %i", rv);
61 rv = AcpiEnableSubsystem(ACPI_FULL_INITIALIZATION);
62 if( ACPI_FAILURE(rv) )
64 Log_Error("ACPI", "AcpiEnableSubsystem: %i", rv);
75 ACPI_STATUS AcpiOsInitialize(void)
80 ACPI_STATUS AcpiOsTerminate(void)
85 ACPI_PHYSICAL_ADDRESS AcpiOsGetRootPointer(void)
90 if( gACPI_RSDPOverride )
91 return gACPI_RSDPOverride;
93 rv = AcpiFindRootPointer(&val);
94 if( ACPI_FAILURE(rv) )
103 ACPI_STATUS AcpiOsPredefinedOverride(const ACPI_PREDEFINED_NAMES *PredefinedObject, ACPI_STRING *NewValue)
109 ACPI_STATUS AcpiOsTableOverride(ACPI_TABLE_HEADER *ExisitingTable, ACPI_TABLE_HEADER **NewTable)
115 ACPI_STATUS AcpiOsPhysicalTableOverride(ACPI_TABLE_HEADER *ExisitingTable, ACPI_PHYSICAL_ADDRESS *NewAddress, UINT32 *NewTableLength)
121 // -- Memory Management ---
131 ACPI_STATUS AcpiOsCreateCache(char *CacheName, UINT16 ObjectSize, UINT16 MaxDepth, ACPI_CACHE_T **ReturnCache)
134 int namelen = (CacheName ? strlen(CacheName) : 0) + 1;
135 LOG("CacheName=%s, ObjSize=%x, MaxDepth=%x", CacheName, ObjectSize, MaxDepth);
137 namelen = (namelen + 3) & ~3;
139 ret = malloc(sizeof(*ret) + MaxDepth*sizeof(char) + namelen + MaxDepth*ObjectSize);
141 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
145 ret->nObj = MaxDepth;
146 ret->ObjectSize = ObjectSize;
147 ret->Name = (char*)(ret->ObjectStates + MaxDepth);
148 ret->First = ret->Name + namelen;
150 strcpy(ret->Name, CacheName);
153 memset(ret->ObjectStates, 0, sizeof(char)*MaxDepth);
155 LOG("Allocated cache %p '%s' (%i x 0x%x)", ret, CacheName, MaxDepth, ObjectSize);
162 ACPI_STATUS AcpiOsDeleteCache(ACPI_CACHE_T *Cache)
165 return AE_BAD_PARAMETER;
171 ACPI_STATUS AcpiOsPurgeCache(ACPI_CACHE_T *Cache)
174 return AE_BAD_PARAMETER;
176 memset(Cache->ObjectStates, 0, sizeof(char)*Cache->nObj);
181 void *AcpiOsAcquireObject(ACPI_CACHE_T *Cache)
183 ENTER("pCache", Cache);
184 for(int i = 0; i < Cache->nObj; i ++ )
186 if( !Cache->ObjectStates[i] ) {
187 Cache->ObjectStates[i] = 1;
188 void *rv = (char*)Cache->First + i*Cache->ObjectSize;
189 memset(rv, 0, Cache->ObjectSize);
195 Log_Debug("ACPICA", "AcpiOsAcquireObject: %i objects used", Cache->nObj);
201 ACPI_STATUS AcpiOsReleaseObject(ACPI_CACHE_T *Cache, void *Object)
203 if( Cache == NULL || Object == NULL )
204 return AE_BAD_PARAMETER;
206 tVAddr delta = (tVAddr)Object - (tVAddr)Cache->First;
207 delta /= Cache->ObjectSize;
208 LOG("Cache=%p, delta = %i, (limit %i)", Cache, delta, Cache->nObj);
210 if( delta >= Cache->nObj )
211 return AE_BAD_PARAMETER;
213 Cache->ObjectStates[delta] = 0;
218 void *AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_SIZE Length)
220 if( PhysicalAddress < ONEMEG )
221 return (void*)(KERNEL_BASE | PhysicalAddress);
223 Uint ofs = PhysicalAddress & (PAGE_SIZE-1);
224 int npages = (ofs + Length + (PAGE_SIZE-1)) / PAGE_SIZE;
225 void *rv = ((char*)MM_MapHWPages(PhysicalAddress, npages)) + ofs;
226 // MM_DumpTables(0, -1);
227 LOG("Map (%P+%i pg) to %p", PhysicalAddress, npages, rv);
231 void AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
233 if( (tVAddr)LogicalAddress - KERNEL_BASE < ONEMEG )
236 LOG("%p", LogicalAddress);
238 Uint ofs = (tVAddr)LogicalAddress & (PAGE_SIZE-1);
239 int npages = (ofs + Length + (PAGE_SIZE-1)) / PAGE_SIZE;
240 // TODO: Validate `Length` is the same as was passed to AcpiOsMapMemory
241 MM_UnmapHWPages( (tVAddr)LogicalAddress, npages);
244 ACPI_STATUS AcpiOsGetPhysicalAddress(void *LogicalAddress, ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
246 if( LogicalAddress == NULL || PhysicalAddress == NULL )
247 return AE_BAD_PARAMETER;
249 tPAddr rv = MM_GetPhysAddr(LogicalAddress);
252 *PhysicalAddress = rv;
256 void *AcpiOsAllocate(ACPI_SIZE Size)
261 void AcpiOsFree(void *Memory)
266 BOOLEAN AcpiOsReadable(void *Memory, ACPI_SIZE Length)
268 return CheckMem(Memory, Length);
271 BOOLEAN AcpiOsWritable(void *Memory, ACPI_SIZE Length)
273 // TODO: Actually check if it's writable
274 return CheckMem(Memory, Length);
279 ACPI_THREAD_ID AcpiOsGetThreadId(void)
281 return Threads_GetTID() + 1;
284 ACPI_STATUS AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function, void *Context)
286 // TODO: Need to store currently executing functions
287 if( Function == NULL )
288 return AE_BAD_PARAMETER;
289 Proc_SpawnWorker(Function, Context);
293 void AcpiOsSleep(UINT64 Milliseconds)
295 Time_Delay(Milliseconds);
298 void AcpiOsStall(UINT32 Microseconds)
300 // TODO: need a microsleep function
301 Microseconds += (1000-1);
302 Microseconds /= 1000;
303 Time_Delay(Microseconds);
306 void AcpiOsWaitEventsComplete(void)
311 // --- Mutexes etc ---
312 ACPI_STATUS AcpiOsCreateMutex(ACPI_MUTEX *OutHandle)
316 return AE_BAD_PARAMETER;
317 tMutex *ret = calloc( sizeof(tMutex), 1 );
319 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
322 ret->Name = "AcpiOsCreateMutex";
328 void AcpiOsDeleteMutex(ACPI_MUTEX Handle)
330 Mutex_Acquire(Handle);
334 ACPI_STATUS AcpiOsAcquireMutex(ACPI_MUTEX Handle, UINT16 Timeout)
337 return AE_BAD_PARAMETER;
339 Mutex_Acquire(Handle);
344 void AcpiOsReleaseMutex(ACPI_MUTEX Handle)
346 Mutex_Release(Handle);
349 ACPI_STATUS AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, ACPI_SEMAPHORE *OutHandle)
351 LOG("(MaxUnits=%i,InitialUnits=%i)", MaxUnits, InitialUnits);
353 return AE_BAD_PARAMETER;
354 tSemaphore *ret = calloc( sizeof(tSemaphore), 1 );
356 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
360 Semaphore_Init(ret, InitialUnits, MaxUnits, "AcpiOsCreateSemaphore", "");
365 ACPI_STATUS AcpiOsDeleteSemaphore(ACPI_SEMAPHORE Handle)
368 return AE_BAD_PARAMETER;
375 ACPI_STATUS AcpiOsWaitSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units, UINT16 Timeout)
378 return AE_BAD_PARAMETER;
383 // NOTE: Possible race condition
384 if( Semaphore_GetValue(Handle) >= Units ) {
385 Semaphore_Wait(Handle, Units);
393 while(rem && now() - start < Timeout)
395 rem -= Semaphore_Wait(Handle, rem);
399 Semaphore_Signal(Handle, Units - rem);
406 ACPI_STATUS AcpiOsSignalSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units)
409 return AE_BAD_PARAMETER;
411 // TODO: Support AE_LIMIT detection early (to avoid blocks)
414 int rv = Semaphore_Signal(Handle, Units);
421 ACPI_STATUS AcpiOsCreateLock(ACPI_SPINLOCK *OutHandle)
425 return AE_BAD_PARAMETER;
426 tShortSpinlock *lock = calloc(sizeof(tShortSpinlock), 1);
434 void AcpiOsDeleteLock(ACPI_SPINLOCK Handle)
439 ACPI_CPU_FLAGS AcpiOsAcquireLock(ACPI_SPINLOCK Handle)
445 void AcpiOsReleaseLock(ACPI_SPINLOCK Handle, ACPI_CPU_FLAGS Flags)
450 // --- Interrupt handling ---
451 #define N_INT_LEVELS 16
452 ACPI_OSD_HANDLER gaACPI_InterruptHandlers[N_INT_LEVELS];
453 void *gaACPI_InterruptData[N_INT_LEVELS];
454 int gaACPI_InterruptHandles[N_INT_LEVELS];
456 void ACPI_int_InterruptProxy(int IRQ, void *data)
458 if( !gaACPI_InterruptHandlers[IRQ] )
460 gaACPI_InterruptHandlers[IRQ](gaACPI_InterruptData[IRQ]);
463 ACPI_STATUS AcpiOsInstallInterruptHandler(UINT32 InterruptLevel, ACPI_OSD_HANDLER Handler, void *Context)
465 if( InterruptLevel >= N_INT_LEVELS || Handler == NULL )
466 return AE_BAD_PARAMETER;
467 if( gaACPI_InterruptHandlers[InterruptLevel] )
468 return AE_ALREADY_EXISTS;
470 gaACPI_InterruptHandlers[InterruptLevel] = Handler;
471 gaACPI_InterruptData[InterruptLevel] = Context;
473 gaACPI_InterruptHandles[InterruptLevel] = IRQ_AddHandler(InterruptLevel, ACPI_int_InterruptProxy, NULL);
477 ACPI_STATUS AcpiOsRemoveInterruptHandler(UINT32 InterruptLevel, ACPI_OSD_HANDLER Handler)
479 if( InterruptLevel >= N_INT_LEVELS || Handler == NULL )
480 return AE_BAD_PARAMETER;
481 if( gaACPI_InterruptHandlers[InterruptLevel] != Handler )
483 gaACPI_InterruptHandlers[InterruptLevel] = NULL;
484 IRQ_RemHandler(gaACPI_InterruptHandles[InterruptLevel]);
488 // --- Memory Access ---
489 ACPI_STATUS AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 *Value, UINT32 Width)
492 if( Address < ONEMEG ) {
493 ptr = (void*)(KERNEL_BASE | Address);
496 ptr = (char*)MM_MapTemp(Address) + (Address & 0xFFF);
501 case 8: *Value = *(Uint8 *)ptr; break;
502 case 16: *Value = *(Uint16*)ptr; break;
503 case 32: *Value = *(Uint32*)ptr; break;
504 case 64: *Value = *(Uint64*)ptr; break;
507 if( Address >= ONEMEG ) {
514 ACPI_STATUS AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 Value, UINT32 Width)
517 if( Address < ONEMEG ) {
518 ptr = (void*)(KERNEL_BASE | Address);
521 ptr = (char*)MM_MapTemp(Address) + (Address & 0xFFF);
526 case 8: *(Uint8 *)ptr = Value; break;
527 case 16: *(Uint16*)ptr = Value; break;
528 case 32: *(Uint32*)ptr = Value; break;
529 case 64: *(Uint64*)ptr = Value; break;
531 return AE_BAD_PARAMETER;
534 if( Address >= 1024*1024 ) {
541 // --- Port Input / Output ---
542 ACPI_STATUS AcpiOsReadPort(ACPI_IO_ADDRESS Address, UINT32 *Value, UINT32 Width)
546 case 8: *Value = inb(Address); break;
547 case 16: *Value = inw(Address); break;
548 case 32: *Value = ind(Address); break;
550 return AE_BAD_PARAMETER;
555 ACPI_STATUS AcpiOsWritePort(ACPI_IO_ADDRESS Address, UINT32 Value, UINT32 Width)
559 case 8: outb(Address, Value); break;
560 case 16: outw(Address, Value); break;
561 case 32: outd(Address, Value); break;
563 return AE_BAD_PARAMETER;
568 // --- PCI Configuration Space Access ---
569 ACPI_STATUS AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 *Value, UINT32 Width)
572 return AE_NOT_IMPLEMENTED;
575 ACPI_STATUS AcpiOsWritePciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 Value, UINT32 Width)
578 return AE_NOT_IMPLEMENTED;
581 // --- Formatted Output ---
582 void AcpiOsPrintf(const char *Format, ...)
585 va_start(args, Format);
592 void AcpiOsVprintf(const char *Format, va_list Args)
597 void AcpiOsRedirectOutput(void *Destination)
599 // TODO: Do I even need to impliment this?
602 // --- Miscellaneous ---
603 UINT64 AcpiOsGetTimer(void)
605 return now() * 10 * 1000;
608 ACPI_STATUS AcpiOsSignal(UINT32 Function, void *Info)
612 case ACPI_SIGNAL_FATAL: {
613 ACPI_SIGNAL_FATAL_INFO *finfo = Info;
614 Log_Error("ACPI AML", "Fatal %x %x %x", finfo->Type, finfo->Code, finfo->Argument);
616 case ACPI_SIGNAL_BREAKPOINT: {
617 Log_Notice("ACPI AML", "Breakpoint %s", Info);
623 ACPI_STATUS AcpiOsGetLine(char *Buffer, UINT32 BufferLength, UINT32 *BytesRead)
626 return AE_NOT_IMPLEMENTED;