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 rv = AcpiLoadTables();
53 if( ACPI_FAILURE(rv) )
55 Log_Error("ACPI", "AcpiLoadTables: %i", rv);
60 rv = AcpiEnableSubsystem(ACPI_FULL_INITIALIZATION);
61 if( ACPI_FAILURE(rv) )
63 Log_Error("ACPI", "AcpiEnableSubsystem: %i", rv);
74 ACPI_STATUS AcpiOsInitialize(void)
79 ACPI_STATUS AcpiOsTerminate(void)
84 ACPI_PHYSICAL_ADDRESS AcpiOsGetRootPointer(void)
89 if( gACPI_RSDPOverride )
90 return gACPI_RSDPOverride;
92 rv = AcpiFindRootPointer(&val);
93 if( ACPI_FAILURE(rv) )
102 ACPI_STATUS AcpiOsPredefinedOverride(const ACPI_PREDEFINED_NAMES *PredefinedObject, ACPI_STRING *NewValue)
108 ACPI_STATUS AcpiOsTableOverride(ACPI_TABLE_HEADER *ExisitingTable, ACPI_TABLE_HEADER **NewTable)
114 ACPI_STATUS AcpiOsPhysicalTableOverride(ACPI_TABLE_HEADER *ExisitingTable, ACPI_PHYSICAL_ADDRESS *NewAddress, UINT32 *NewTableLength)
120 // -- Memory Management ---
121 #if USE_ACESS_ACPI_CACHE
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 if( ReturnCache == NULL || ObjectSize < 16) {
138 return AE_BAD_PARAMETER;
141 namelen = (namelen + 3) & ~3;
143 ret = malloc(sizeof(*ret) + MaxDepth*sizeof(char) + namelen + MaxDepth*ObjectSize);
145 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
149 ret->nObj = MaxDepth;
150 ret->ObjectSize = ObjectSize;
151 ret->Name = (char*)(ret->ObjectStates + MaxDepth);
152 ret->First = ret->Name + namelen;
154 strcpy(ret->Name, CacheName);
157 memset(ret->ObjectStates, 0, sizeof(char)*MaxDepth);
159 LOG("Allocated cache %p '%s' (%i x 0x%x)", ret, CacheName, MaxDepth, ObjectSize);
166 ACPI_STATUS AcpiOsDeleteCache(ACPI_CACHE_T *Cache)
169 return AE_BAD_PARAMETER;
175 ACPI_STATUS AcpiOsPurgeCache(ACPI_CACHE_T *Cache)
177 ENTER("pCache", Cache);
178 if( Cache == NULL ) {
179 LEAVE('i', AE_BAD_PARAMETER);
180 return AE_BAD_PARAMETER;
183 memset(Cache->ObjectStates, 0, sizeof(char)*Cache->nObj);
189 void *AcpiOsAcquireObject(ACPI_CACHE_T *Cache)
191 ENTER("pCache", Cache);
192 LOG("Called by %p", __builtin_return_address(0));
193 for(int i = 0; i < Cache->nObj; i ++ )
195 if( !Cache->ObjectStates[i] ) {
196 Cache->ObjectStates[i] = 1;
197 void *rv = (char*)Cache->First + i*Cache->ObjectSize;
202 memset(rv, 0, Cache->ObjectSize);
208 Log_Debug("ACPICA", "AcpiOsAcquireObject: All %i objects used in '%s'",
209 Cache->nObj, Cache->Name);
215 ACPI_STATUS AcpiOsReleaseObject(ACPI_CACHE_T *Cache, void *Object)
217 if( Cache == NULL || Object == NULL )
218 return AE_BAD_PARAMETER;
219 ENTER("pCache pObject", Cache, Object);
221 tVAddr delta = (tVAddr)Object - (tVAddr)Cache->First;
222 delta /= Cache->ObjectSize;
223 LOG("Cache=%p, delta = %i, (limit %i)", Cache, delta, Cache->nObj);
225 if( delta >= Cache->nObj ) {
226 LEAVE('i', AE_BAD_PARAMETER);
227 return AE_BAD_PARAMETER;
230 Cache->ObjectStates[delta] = 0;
237 void *AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_SIZE Length)
239 if( PhysicalAddress < ONEMEG ) {
240 ASSERTCR(Length, <=, ONEMEG-PhysicalAddress, NULL);
241 return (void*)(KERNEL_BASE | PhysicalAddress);
244 Uint ofs = PhysicalAddress & (PAGE_SIZE-1);
245 int npages = (ofs + Length + (PAGE_SIZE-1)) / PAGE_SIZE;
246 char *maploc = MM_MapHWPages(PhysicalAddress, npages);
248 LOG("Mapping %P+0x%x failed", PhysicalAddress, Length);
251 // MM_DumpTables(0, -1);
252 void *rv = maploc + ofs;
253 LOG("Map (%P+%i pg) to %p", PhysicalAddress, npages, rv);
257 void AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
259 if( (tVAddr)LogicalAddress - KERNEL_BASE < ONEMEG ) {
263 LOG("%p", LogicalAddress);
265 Uint ofs = (tVAddr)LogicalAddress & (PAGE_SIZE-1);
266 int npages = (ofs + Length + (PAGE_SIZE-1)) / PAGE_SIZE;
267 // TODO: Validate `Length` is the same as was passed to AcpiOsMapMemory
268 MM_UnmapHWPages( LogicalAddress, npages);
271 ACPI_STATUS AcpiOsGetPhysicalAddress(void *LogicalAddress, ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
273 if( LogicalAddress == NULL || PhysicalAddress == NULL )
274 return AE_BAD_PARAMETER;
276 tPAddr rv = MM_GetPhysAddr(LogicalAddress);
279 *PhysicalAddress = rv;
283 void *AcpiOsAllocate(ACPI_SIZE Size)
288 void AcpiOsFree(void *Memory)
293 BOOLEAN AcpiOsReadable(void *Memory, ACPI_SIZE Length)
295 return CheckMem(Memory, Length);
298 BOOLEAN AcpiOsWritable(void *Memory, ACPI_SIZE Length)
300 // TODO: Actually check if it's writable
301 return CheckMem(Memory, Length);
306 ACPI_THREAD_ID AcpiOsGetThreadId(void)
308 return Threads_GetTID() + 1;
311 ACPI_STATUS AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function, void *Context)
313 // TODO: Need to store currently executing functions
314 if( Function == NULL )
315 return AE_BAD_PARAMETER;
316 Proc_SpawnWorker(Function, Context);
320 void AcpiOsSleep(UINT64 Milliseconds)
322 Time_Delay(Milliseconds);
325 void AcpiOsStall(UINT32 Microseconds)
327 // TODO: need a microsleep function
328 Microseconds += (1000-1);
329 Microseconds /= 1000;
330 Time_Delay(Microseconds);
333 void AcpiOsWaitEventsComplete(void)
338 // --- Mutexes etc ---
339 ACPI_STATUS AcpiOsCreateMutex(ACPI_MUTEX *OutHandle)
343 return AE_BAD_PARAMETER;
344 tMutex *ret = calloc( sizeof(tMutex), 1 );
346 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
349 ret->Name = "AcpiOsCreateMutex";
355 void AcpiOsDeleteMutex(ACPI_MUTEX Handle)
357 // TODO: Need `Mutex_Destroy`
358 Mutex_Acquire(Handle);
362 ACPI_STATUS AcpiOsAcquireMutex(ACPI_MUTEX Handle, UINT16 Timeout)
365 return AE_BAD_PARAMETER;
367 Mutex_Acquire(Handle);
372 void AcpiOsReleaseMutex(ACPI_MUTEX Handle)
374 Mutex_Release(Handle);
377 ACPI_STATUS AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, ACPI_SEMAPHORE *OutHandle)
379 LOG("(MaxUnits=%i,InitialUnits=%i)", MaxUnits, InitialUnits);
381 return AE_BAD_PARAMETER;
382 tSemaphore *ret = calloc( sizeof(tSemaphore), 1 );
384 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
388 Semaphore_Init(ret, InitialUnits, MaxUnits, "AcpiOsCreateSemaphore", "");
393 ACPI_STATUS AcpiOsDeleteSemaphore(ACPI_SEMAPHORE Handle)
396 return AE_BAD_PARAMETER;
403 ACPI_STATUS AcpiOsWaitSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units, UINT16 Timeout)
406 return AE_BAD_PARAMETER;
411 // NOTE: Possible race condition
412 if( Semaphore_GetValue(Handle) >= Units ) {
413 Semaphore_Wait(Handle, Units);
421 while(rem && now() - start < Timeout)
423 rem -= Semaphore_Wait(Handle, rem);
427 Semaphore_Signal(Handle, Units - rem);
434 ACPI_STATUS AcpiOsSignalSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units)
437 return AE_BAD_PARAMETER;
439 // TODO: Support AE_LIMIT detection early (to avoid blocks)
442 int rv = Semaphore_Signal(Handle, Units);
449 ACPI_STATUS AcpiOsCreateLock(ACPI_SPINLOCK *OutHandle)
453 return AE_BAD_PARAMETER;
454 tShortSpinlock *lock = calloc(sizeof(tShortSpinlock), 1);
462 void AcpiOsDeleteLock(ACPI_SPINLOCK Handle)
467 ACPI_CPU_FLAGS AcpiOsAcquireLock(ACPI_SPINLOCK Handle)
473 void AcpiOsReleaseLock(ACPI_SPINLOCK Handle, ACPI_CPU_FLAGS Flags)
478 // --- Interrupt handling ---
479 #define N_INT_LEVELS 16
480 ACPI_OSD_HANDLER gaACPI_InterruptHandlers[N_INT_LEVELS];
481 void *gaACPI_InterruptData[N_INT_LEVELS];
482 int gaACPI_InterruptHandles[N_INT_LEVELS];
484 void ACPI_int_InterruptProxy(int IRQ, void *data)
486 if( !gaACPI_InterruptHandlers[IRQ] )
488 gaACPI_InterruptHandlers[IRQ](gaACPI_InterruptData[IRQ]);
491 ACPI_STATUS AcpiOsInstallInterruptHandler(UINT32 InterruptLevel, ACPI_OSD_HANDLER Handler, void *Context)
493 if( InterruptLevel >= N_INT_LEVELS || Handler == NULL )
494 return AE_BAD_PARAMETER;
495 if( gaACPI_InterruptHandlers[InterruptLevel] )
496 return AE_ALREADY_EXISTS;
498 gaACPI_InterruptHandlers[InterruptLevel] = Handler;
499 gaACPI_InterruptData[InterruptLevel] = Context;
501 gaACPI_InterruptHandles[InterruptLevel] = IRQ_AddHandler(InterruptLevel, ACPI_int_InterruptProxy, NULL);
505 ACPI_STATUS AcpiOsRemoveInterruptHandler(UINT32 InterruptLevel, ACPI_OSD_HANDLER Handler)
507 if( InterruptLevel >= N_INT_LEVELS || Handler == NULL )
508 return AE_BAD_PARAMETER;
509 if( gaACPI_InterruptHandlers[InterruptLevel] != Handler )
511 gaACPI_InterruptHandlers[InterruptLevel] = NULL;
512 IRQ_RemHandler(gaACPI_InterruptHandles[InterruptLevel]);
516 // --- Memory Access ---
517 ACPI_STATUS AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 *Value, UINT32 Width)
520 if( Address < ONEMEG ) {
521 ptr = (void*)(KERNEL_BASE | Address);
524 ptr = (char*)MM_MapTemp(Address) + (Address & 0xFFF);
529 case 8: *Value = *(Uint8 *)ptr; break;
530 case 16: *Value = *(Uint16*)ptr; break;
531 case 32: *Value = *(Uint32*)ptr; break;
532 case 64: *Value = *(Uint64*)ptr; break;
535 if( Address >= ONEMEG ) {
539 LOG("*%P = [%i]%X", Address, Width, *Value);
544 ACPI_STATUS AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 Value, UINT32 Width)
547 if( Address < ONEMEG ) {
548 ptr = (void*)(KERNEL_BASE | Address);
551 ptr = (char*)MM_MapTemp(Address) + (Address & 0xFFF);
556 case 8: *(Uint8 *)ptr = Value; break;
557 case 16: *(Uint16*)ptr = Value; break;
558 case 32: *(Uint32*)ptr = Value; break;
559 case 64: *(Uint64*)ptr = Value; break;
561 return AE_BAD_PARAMETER;
564 if( Address >= ONEMEG ) {
571 // --- Port Input / Output ---
572 ACPI_STATUS AcpiOsReadPort(ACPI_IO_ADDRESS Address, UINT32 *Value, UINT32 Width)
576 case 8: *Value = inb(Address); break;
577 case 16: *Value = inw(Address); break;
578 case 32: *Value = ind(Address); break;
580 return AE_BAD_PARAMETER;
585 ACPI_STATUS AcpiOsWritePort(ACPI_IO_ADDRESS Address, UINT32 Value, UINT32 Width)
589 case 8: outb(Address, Value); break;
590 case 16: outw(Address, Value); break;
591 case 32: outd(Address, Value); break;
593 return AE_BAD_PARAMETER;
598 // --- PCI Configuration Space Access ---
599 ACPI_STATUS AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 *Value, UINT32 Width)
602 return AE_NOT_IMPLEMENTED;
605 ACPI_STATUS AcpiOsWritePciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 Value, UINT32 Width)
608 return AE_NOT_IMPLEMENTED;
611 // --- Formatted Output ---
612 void AcpiOsPrintf(const char *Format, ...)
615 va_start(args, Format);
622 void AcpiOsVprintf(const char *Format, va_list Args)
627 void AcpiOsRedirectOutput(void *Destination)
629 // TODO: Do I even need to impliment this?
632 // --- Miscellaneous ---
633 UINT64 AcpiOsGetTimer(void)
635 return now() * 10 * 1000;
638 ACPI_STATUS AcpiOsSignal(UINT32 Function, void *Info)
642 case ACPI_SIGNAL_FATAL: {
643 ACPI_SIGNAL_FATAL_INFO *finfo = Info;
644 Log_Error("ACPI AML", "Fatal %x %x %x", finfo->Type, finfo->Code, finfo->Argument);
646 case ACPI_SIGNAL_BREAKPOINT: {
647 Log_Notice("ACPI AML", "Breakpoint %s", Info);
653 ACPI_STATUS AcpiOsGetLine(char *Buffer, UINT32 BufferLength, UINT32 *BytesRead)
656 return AE_NOT_IMPLEMENTED;