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 ---
130 ACPI_STATUS AcpiOsCreateCache(char *CacheName, UINT16 ObjectSize, UINT16 MaxDepth, ACPI_CACHE_T **ReturnCache)
133 int namelen = (CacheName ? strlen(CacheName) : 0) + 1;
134 LOG("CacheName=%s, ObjSize=%x, MaxDepth=%x", CacheName, ObjectSize, MaxDepth);
136 if( ReturnCache == NULL || ObjectSize < 16) {
137 return AE_BAD_PARAMETER;
140 namelen = (namelen + 3) & ~3;
142 ret = malloc(sizeof(*ret) + MaxDepth*sizeof(char) + namelen + MaxDepth*ObjectSize);
144 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
148 ret->nObj = MaxDepth;
149 ret->ObjectSize = ObjectSize;
150 ret->Name = (char*)(ret->ObjectStates + MaxDepth);
151 ret->First = ret->Name + namelen;
153 strcpy(ret->Name, CacheName);
156 memset(ret->ObjectStates, 0, sizeof(char)*MaxDepth);
158 LOG("Allocated cache %p '%s' (%i x 0x%x)", ret, CacheName, MaxDepth, ObjectSize);
165 ACPI_STATUS AcpiOsDeleteCache(ACPI_CACHE_T *Cache)
168 return AE_BAD_PARAMETER;
174 ACPI_STATUS AcpiOsPurgeCache(ACPI_CACHE_T *Cache)
176 ENTER("pCache", Cache);
177 if( Cache == NULL ) {
178 LEAVE('i', AE_BAD_PARAMETER);
179 return AE_BAD_PARAMETER;
182 memset(Cache->ObjectStates, 0, sizeof(char)*Cache->nObj);
188 void *AcpiOsAcquireObject(ACPI_CACHE_T *Cache)
190 ENTER("pCache", Cache);
191 LOG("Called by %p", __builtin_return_address(0));
192 for(int i = 0; i < Cache->nObj; i ++ )
194 if( !Cache->ObjectStates[i] ) {
195 Cache->ObjectStates[i] = 1;
196 void *rv = (char*)Cache->First + i*Cache->ObjectSize;
201 memset(rv, 0, Cache->ObjectSize);
207 Log_Debug("ACPICA", "AcpiOsAcquireObject: All %i objects used in '%s'",
208 Cache->nObj, Cache->Name);
214 ACPI_STATUS AcpiOsReleaseObject(ACPI_CACHE_T *Cache, void *Object)
216 if( Cache == NULL || Object == NULL )
217 return AE_BAD_PARAMETER;
218 ENTER("pCache pObject", Cache, Object);
220 tVAddr delta = (tVAddr)Object - (tVAddr)Cache->First;
221 delta /= Cache->ObjectSize;
222 LOG("Cache=%p, delta = %i, (limit %i)", Cache, delta, Cache->nObj);
224 if( delta >= Cache->nObj ) {
225 LEAVE('i', AE_BAD_PARAMETER);
226 return AE_BAD_PARAMETER;
229 Cache->ObjectStates[delta] = 0;
235 void *AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_SIZE Length)
237 if( PhysicalAddress < ONEMEG )
238 return (void*)(KERNEL_BASE | PhysicalAddress);
240 Uint ofs = PhysicalAddress & (PAGE_SIZE-1);
241 int npages = (ofs + Length + (PAGE_SIZE-1)) / PAGE_SIZE;
242 char *maploc = (void*)MM_MapHWPages(PhysicalAddress, npages);
244 LOG("Mapping %P+0x%x failed", PhysicalAddress, Length);
247 // MM_DumpTables(0, -1);
248 void *rv = maploc + ofs;
249 LOG("Map (%P+%i pg) to %p", PhysicalAddress, npages, rv);
253 void AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
255 if( (tVAddr)LogicalAddress - KERNEL_BASE < ONEMEG )
258 LOG("%p", LogicalAddress);
260 Uint ofs = (tVAddr)LogicalAddress & (PAGE_SIZE-1);
261 int npages = (ofs + Length + (PAGE_SIZE-1)) / PAGE_SIZE;
262 // TODO: Validate `Length` is the same as was passed to AcpiOsMapMemory
263 MM_UnmapHWPages( (tVAddr)LogicalAddress, npages);
266 ACPI_STATUS AcpiOsGetPhysicalAddress(void *LogicalAddress, ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
268 if( LogicalAddress == NULL || PhysicalAddress == NULL )
269 return AE_BAD_PARAMETER;
271 tPAddr rv = MM_GetPhysAddr(LogicalAddress);
274 *PhysicalAddress = rv;
278 void *AcpiOsAllocate(ACPI_SIZE Size)
283 void AcpiOsFree(void *Memory)
288 BOOLEAN AcpiOsReadable(void *Memory, ACPI_SIZE Length)
290 return CheckMem(Memory, Length);
293 BOOLEAN AcpiOsWritable(void *Memory, ACPI_SIZE Length)
295 // TODO: Actually check if it's writable
296 return CheckMem(Memory, Length);
301 ACPI_THREAD_ID AcpiOsGetThreadId(void)
303 return Threads_GetTID() + 1;
306 ACPI_STATUS AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function, void *Context)
308 // TODO: Need to store currently executing functions
309 if( Function == NULL )
310 return AE_BAD_PARAMETER;
311 Proc_SpawnWorker(Function, Context);
315 void AcpiOsSleep(UINT64 Milliseconds)
317 Time_Delay(Milliseconds);
320 void AcpiOsStall(UINT32 Microseconds)
322 // TODO: need a microsleep function
323 Microseconds += (1000-1);
324 Microseconds /= 1000;
325 Time_Delay(Microseconds);
328 void AcpiOsWaitEventsComplete(void)
333 // --- Mutexes etc ---
334 ACPI_STATUS AcpiOsCreateMutex(ACPI_MUTEX *OutHandle)
338 return AE_BAD_PARAMETER;
339 tMutex *ret = calloc( sizeof(tMutex), 1 );
341 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
344 ret->Name = "AcpiOsCreateMutex";
350 void AcpiOsDeleteMutex(ACPI_MUTEX Handle)
352 // TODO: Need `Mutex_Destroy`
353 Mutex_Acquire(Handle);
357 ACPI_STATUS AcpiOsAcquireMutex(ACPI_MUTEX Handle, UINT16 Timeout)
360 return AE_BAD_PARAMETER;
362 Mutex_Acquire(Handle);
367 void AcpiOsReleaseMutex(ACPI_MUTEX Handle)
369 Mutex_Release(Handle);
372 ACPI_STATUS AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, ACPI_SEMAPHORE *OutHandle)
374 LOG("(MaxUnits=%i,InitialUnits=%i)", MaxUnits, InitialUnits);
376 return AE_BAD_PARAMETER;
377 tSemaphore *ret = calloc( sizeof(tSemaphore), 1 );
379 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
383 Semaphore_Init(ret, InitialUnits, MaxUnits, "AcpiOsCreateSemaphore", "");
388 ACPI_STATUS AcpiOsDeleteSemaphore(ACPI_SEMAPHORE Handle)
391 return AE_BAD_PARAMETER;
398 ACPI_STATUS AcpiOsWaitSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units, UINT16 Timeout)
401 return AE_BAD_PARAMETER;
406 // NOTE: Possible race condition
407 if( Semaphore_GetValue(Handle) >= Units ) {
408 Semaphore_Wait(Handle, Units);
416 while(rem && now() - start < Timeout)
418 rem -= Semaphore_Wait(Handle, rem);
422 Semaphore_Signal(Handle, Units - rem);
429 ACPI_STATUS AcpiOsSignalSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units)
432 return AE_BAD_PARAMETER;
434 // TODO: Support AE_LIMIT detection early (to avoid blocks)
437 int rv = Semaphore_Signal(Handle, Units);
444 ACPI_STATUS AcpiOsCreateLock(ACPI_SPINLOCK *OutHandle)
448 return AE_BAD_PARAMETER;
449 tShortSpinlock *lock = calloc(sizeof(tShortSpinlock), 1);
457 void AcpiOsDeleteLock(ACPI_SPINLOCK Handle)
462 ACPI_CPU_FLAGS AcpiOsAcquireLock(ACPI_SPINLOCK Handle)
468 void AcpiOsReleaseLock(ACPI_SPINLOCK Handle, ACPI_CPU_FLAGS Flags)
473 // --- Interrupt handling ---
474 #define N_INT_LEVELS 16
475 ACPI_OSD_HANDLER gaACPI_InterruptHandlers[N_INT_LEVELS];
476 void *gaACPI_InterruptData[N_INT_LEVELS];
477 int gaACPI_InterruptHandles[N_INT_LEVELS];
479 void ACPI_int_InterruptProxy(int IRQ, void *data)
481 if( !gaACPI_InterruptHandlers[IRQ] )
483 gaACPI_InterruptHandlers[IRQ](gaACPI_InterruptData[IRQ]);
486 ACPI_STATUS AcpiOsInstallInterruptHandler(UINT32 InterruptLevel, ACPI_OSD_HANDLER Handler, void *Context)
488 if( InterruptLevel >= N_INT_LEVELS || Handler == NULL )
489 return AE_BAD_PARAMETER;
490 if( gaACPI_InterruptHandlers[InterruptLevel] )
491 return AE_ALREADY_EXISTS;
493 gaACPI_InterruptHandlers[InterruptLevel] = Handler;
494 gaACPI_InterruptData[InterruptLevel] = Context;
496 gaACPI_InterruptHandles[InterruptLevel] = IRQ_AddHandler(InterruptLevel, ACPI_int_InterruptProxy, NULL);
500 ACPI_STATUS AcpiOsRemoveInterruptHandler(UINT32 InterruptLevel, ACPI_OSD_HANDLER Handler)
502 if( InterruptLevel >= N_INT_LEVELS || Handler == NULL )
503 return AE_BAD_PARAMETER;
504 if( gaACPI_InterruptHandlers[InterruptLevel] != Handler )
506 gaACPI_InterruptHandlers[InterruptLevel] = NULL;
507 IRQ_RemHandler(gaACPI_InterruptHandles[InterruptLevel]);
511 // --- Memory Access ---
512 ACPI_STATUS AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 *Value, UINT32 Width)
515 if( Address < ONEMEG ) {
516 ptr = (void*)(KERNEL_BASE | Address);
519 ptr = (char*)MM_MapTemp(Address) + (Address & 0xFFF);
524 case 8: *Value = *(Uint8 *)ptr; break;
525 case 16: *Value = *(Uint16*)ptr; break;
526 case 32: *Value = *(Uint32*)ptr; break;
527 case 64: *Value = *(Uint64*)ptr; break;
530 if( Address >= ONEMEG ) {
537 ACPI_STATUS AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 Value, UINT32 Width)
540 if( Address < ONEMEG ) {
541 ptr = (void*)(KERNEL_BASE | Address);
544 ptr = (char*)MM_MapTemp(Address) + (Address & 0xFFF);
549 case 8: *(Uint8 *)ptr = Value; break;
550 case 16: *(Uint16*)ptr = Value; break;
551 case 32: *(Uint32*)ptr = Value; break;
552 case 64: *(Uint64*)ptr = Value; break;
554 return AE_BAD_PARAMETER;
557 if( Address >= 1024*1024 ) {
564 // --- Port Input / Output ---
565 ACPI_STATUS AcpiOsReadPort(ACPI_IO_ADDRESS Address, UINT32 *Value, UINT32 Width)
569 case 8: *Value = inb(Address); break;
570 case 16: *Value = inw(Address); break;
571 case 32: *Value = ind(Address); break;
573 return AE_BAD_PARAMETER;
578 ACPI_STATUS AcpiOsWritePort(ACPI_IO_ADDRESS Address, UINT32 Value, UINT32 Width)
582 case 8: outb(Address, Value); break;
583 case 16: outw(Address, Value); break;
584 case 32: outd(Address, Value); break;
586 return AE_BAD_PARAMETER;
591 // --- PCI Configuration Space Access ---
592 ACPI_STATUS AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 *Value, UINT32 Width)
595 return AE_NOT_IMPLEMENTED;
598 ACPI_STATUS AcpiOsWritePciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 Value, UINT32 Width)
601 return AE_NOT_IMPLEMENTED;
604 // --- Formatted Output ---
605 void AcpiOsPrintf(const char *Format, ...)
608 va_start(args, Format);
615 void AcpiOsVprintf(const char *Format, va_list Args)
620 void AcpiOsRedirectOutput(void *Destination)
622 // TODO: Do I even need to impliment this?
625 // --- Miscellaneous ---
626 UINT64 AcpiOsGetTimer(void)
628 return now() * 10 * 1000;
631 ACPI_STATUS AcpiOsSignal(UINT32 Function, void *Info)
635 case ACPI_SIGNAL_FATAL: {
636 ACPI_SIGNAL_FATAL_INFO *finfo = Info;
637 Log_Error("ACPI AML", "Fatal %x %x %x", finfo->Type, finfo->Code, finfo->Argument);
639 case ACPI_SIGNAL_BREAKPOINT: {
640 Log_Notice("ACPI AML", "Breakpoint %s", Info);
646 ACPI_STATUS AcpiOsGetLine(char *Buffer, UINT32 BufferLength, UINT32 *BytesRead)
649 return AE_NOT_IMPLEMENTED;