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 return (void*)(KERNEL_BASE | PhysicalAddress);
242 Uint ofs = PhysicalAddress & (PAGE_SIZE-1);
243 int npages = (ofs + Length + (PAGE_SIZE-1)) / PAGE_SIZE;
244 char *maploc = (void*)MM_MapHWPages(PhysicalAddress, npages);
246 LOG("Mapping %P+0x%x failed", PhysicalAddress, Length);
249 // MM_DumpTables(0, -1);
250 void *rv = maploc + ofs;
251 LOG("Map (%P+%i pg) to %p", PhysicalAddress, npages, rv);
255 void AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
257 if( (tVAddr)LogicalAddress - KERNEL_BASE < ONEMEG )
260 LOG("%p", LogicalAddress);
262 Uint ofs = (tVAddr)LogicalAddress & (PAGE_SIZE-1);
263 int npages = (ofs + Length + (PAGE_SIZE-1)) / PAGE_SIZE;
264 // TODO: Validate `Length` is the same as was passed to AcpiOsMapMemory
265 MM_UnmapHWPages( (tVAddr)LogicalAddress, npages);
268 ACPI_STATUS AcpiOsGetPhysicalAddress(void *LogicalAddress, ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
270 if( LogicalAddress == NULL || PhysicalAddress == NULL )
271 return AE_BAD_PARAMETER;
273 tPAddr rv = MM_GetPhysAddr(LogicalAddress);
276 *PhysicalAddress = rv;
280 void *AcpiOsAllocate(ACPI_SIZE Size)
285 void AcpiOsFree(void *Memory)
290 BOOLEAN AcpiOsReadable(void *Memory, ACPI_SIZE Length)
292 return CheckMem(Memory, Length);
295 BOOLEAN AcpiOsWritable(void *Memory, ACPI_SIZE Length)
297 // TODO: Actually check if it's writable
298 return CheckMem(Memory, Length);
303 ACPI_THREAD_ID AcpiOsGetThreadId(void)
305 return Threads_GetTID() + 1;
308 ACPI_STATUS AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function, void *Context)
310 // TODO: Need to store currently executing functions
311 if( Function == NULL )
312 return AE_BAD_PARAMETER;
313 Proc_SpawnWorker(Function, Context);
317 void AcpiOsSleep(UINT64 Milliseconds)
319 Time_Delay(Milliseconds);
322 void AcpiOsStall(UINT32 Microseconds)
324 // TODO: need a microsleep function
325 Microseconds += (1000-1);
326 Microseconds /= 1000;
327 Time_Delay(Microseconds);
330 void AcpiOsWaitEventsComplete(void)
335 // --- Mutexes etc ---
336 ACPI_STATUS AcpiOsCreateMutex(ACPI_MUTEX *OutHandle)
340 return AE_BAD_PARAMETER;
341 tMutex *ret = calloc( sizeof(tMutex), 1 );
343 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
346 ret->Name = "AcpiOsCreateMutex";
352 void AcpiOsDeleteMutex(ACPI_MUTEX Handle)
354 // TODO: Need `Mutex_Destroy`
355 Mutex_Acquire(Handle);
359 ACPI_STATUS AcpiOsAcquireMutex(ACPI_MUTEX Handle, UINT16 Timeout)
362 return AE_BAD_PARAMETER;
364 Mutex_Acquire(Handle);
369 void AcpiOsReleaseMutex(ACPI_MUTEX Handle)
371 Mutex_Release(Handle);
374 ACPI_STATUS AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, ACPI_SEMAPHORE *OutHandle)
376 LOG("(MaxUnits=%i,InitialUnits=%i)", MaxUnits, InitialUnits);
378 return AE_BAD_PARAMETER;
379 tSemaphore *ret = calloc( sizeof(tSemaphore), 1 );
381 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
385 Semaphore_Init(ret, InitialUnits, MaxUnits, "AcpiOsCreateSemaphore", "");
390 ACPI_STATUS AcpiOsDeleteSemaphore(ACPI_SEMAPHORE Handle)
393 return AE_BAD_PARAMETER;
400 ACPI_STATUS AcpiOsWaitSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units, UINT16 Timeout)
403 return AE_BAD_PARAMETER;
408 // NOTE: Possible race condition
409 if( Semaphore_GetValue(Handle) >= Units ) {
410 Semaphore_Wait(Handle, Units);
418 while(rem && now() - start < Timeout)
420 rem -= Semaphore_Wait(Handle, rem);
424 Semaphore_Signal(Handle, Units - rem);
431 ACPI_STATUS AcpiOsSignalSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units)
434 return AE_BAD_PARAMETER;
436 // TODO: Support AE_LIMIT detection early (to avoid blocks)
439 int rv = Semaphore_Signal(Handle, Units);
446 ACPI_STATUS AcpiOsCreateLock(ACPI_SPINLOCK *OutHandle)
450 return AE_BAD_PARAMETER;
451 tShortSpinlock *lock = calloc(sizeof(tShortSpinlock), 1);
459 void AcpiOsDeleteLock(ACPI_SPINLOCK Handle)
464 ACPI_CPU_FLAGS AcpiOsAcquireLock(ACPI_SPINLOCK Handle)
470 void AcpiOsReleaseLock(ACPI_SPINLOCK Handle, ACPI_CPU_FLAGS Flags)
475 // --- Interrupt handling ---
476 #define N_INT_LEVELS 16
477 ACPI_OSD_HANDLER gaACPI_InterruptHandlers[N_INT_LEVELS];
478 void *gaACPI_InterruptData[N_INT_LEVELS];
479 int gaACPI_InterruptHandles[N_INT_LEVELS];
481 void ACPI_int_InterruptProxy(int IRQ, void *data)
483 if( !gaACPI_InterruptHandlers[IRQ] )
485 gaACPI_InterruptHandlers[IRQ](gaACPI_InterruptData[IRQ]);
488 ACPI_STATUS AcpiOsInstallInterruptHandler(UINT32 InterruptLevel, ACPI_OSD_HANDLER Handler, void *Context)
490 if( InterruptLevel >= N_INT_LEVELS || Handler == NULL )
491 return AE_BAD_PARAMETER;
492 if( gaACPI_InterruptHandlers[InterruptLevel] )
493 return AE_ALREADY_EXISTS;
495 gaACPI_InterruptHandlers[InterruptLevel] = Handler;
496 gaACPI_InterruptData[InterruptLevel] = Context;
498 gaACPI_InterruptHandles[InterruptLevel] = IRQ_AddHandler(InterruptLevel, ACPI_int_InterruptProxy, NULL);
502 ACPI_STATUS AcpiOsRemoveInterruptHandler(UINT32 InterruptLevel, ACPI_OSD_HANDLER Handler)
504 if( InterruptLevel >= N_INT_LEVELS || Handler == NULL )
505 return AE_BAD_PARAMETER;
506 if( gaACPI_InterruptHandlers[InterruptLevel] != Handler )
508 gaACPI_InterruptHandlers[InterruptLevel] = NULL;
509 IRQ_RemHandler(gaACPI_InterruptHandles[InterruptLevel]);
513 // --- Memory Access ---
514 ACPI_STATUS AcpiOsReadMemory(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: *Value = *(Uint8 *)ptr; break;
527 case 16: *Value = *(Uint16*)ptr; break;
528 case 32: *Value = *(Uint32*)ptr; break;
529 case 64: *Value = *(Uint64*)ptr; break;
532 if( Address >= ONEMEG ) {
536 LOG("*%P = [%i]%X", Address, Width, *Value);
541 ACPI_STATUS AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 Value, UINT32 Width)
544 if( Address < ONEMEG ) {
545 ptr = (void*)(KERNEL_BASE | Address);
548 ptr = (char*)MM_MapTemp(Address) + (Address & 0xFFF);
553 case 8: *(Uint8 *)ptr = Value; break;
554 case 16: *(Uint16*)ptr = Value; break;
555 case 32: *(Uint32*)ptr = Value; break;
556 case 64: *(Uint64*)ptr = Value; break;
558 return AE_BAD_PARAMETER;
561 if( Address >= ONEMEG ) {
568 // --- Port Input / Output ---
569 ACPI_STATUS AcpiOsReadPort(ACPI_IO_ADDRESS Address, UINT32 *Value, UINT32 Width)
573 case 8: *Value = inb(Address); break;
574 case 16: *Value = inw(Address); break;
575 case 32: *Value = ind(Address); break;
577 return AE_BAD_PARAMETER;
582 ACPI_STATUS AcpiOsWritePort(ACPI_IO_ADDRESS Address, UINT32 Value, UINT32 Width)
586 case 8: outb(Address, Value); break;
587 case 16: outw(Address, Value); break;
588 case 32: outd(Address, Value); break;
590 return AE_BAD_PARAMETER;
595 // --- PCI Configuration Space Access ---
596 ACPI_STATUS AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 *Value, UINT32 Width)
599 return AE_NOT_IMPLEMENTED;
602 ACPI_STATUS AcpiOsWritePciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 Value, UINT32 Width)
605 return AE_NOT_IMPLEMENTED;
608 // --- Formatted Output ---
609 void AcpiOsPrintf(const char *Format, ...)
612 va_start(args, Format);
619 void AcpiOsVprintf(const char *Format, va_list Args)
624 void AcpiOsRedirectOutput(void *Destination)
626 // TODO: Do I even need to impliment this?
629 // --- Miscellaneous ---
630 UINT64 AcpiOsGetTimer(void)
632 return now() * 10 * 1000;
635 ACPI_STATUS AcpiOsSignal(UINT32 Function, void *Info)
639 case ACPI_SIGNAL_FATAL: {
640 ACPI_SIGNAL_FATAL_INFO *finfo = Info;
641 Log_Error("ACPI AML", "Fatal %x %x %x", finfo->Type, finfo->Code, finfo->Argument);
643 case ACPI_SIGNAL_BREAKPOINT: {
644 Log_Notice("ACPI AML", "Breakpoint %s", Info);
650 ACPI_STATUS AcpiOsGetLine(char *Buffer, UINT32 BufferLength, UINT32 *BytesRead)
653 return AE_NOT_IMPLEMENTED;