2 * Acess2 Kernel (x86 Core)
3 * - By John Hodge (thePowersGang)
8 #define ACPI_DEBUG_OUTPUT 1
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);
140 if( !ret ) return AE_NO_MEMORY;
142 ret->nObj = MaxDepth;
143 ret->ObjectSize = ObjectSize;
144 ret->Name = (char*)(ret->ObjectStates + MaxDepth);
145 ret->First = ret->Name + namelen;
147 strcpy(ret->Name, CacheName);
150 memset(ret->ObjectStates, 0, sizeof(char)*MaxDepth);
152 LOG("Allocated cache '%s' (%i x 0x%x)", CacheName, MaxDepth, ObjectSize);
159 ACPI_STATUS AcpiOsDeleteCache(ACPI_CACHE_T *Cache)
162 return AE_BAD_PARAMETER;
168 ACPI_STATUS AcpiOsPurgeCache(ACPI_CACHE_T *Cache)
171 return AE_BAD_PARAMETER;
173 memset(Cache->ObjectStates, 0, sizeof(char)*Cache->nObj);
178 void *AcpiOsAcquireObject(ACPI_CACHE_T *Cache)
180 ENTER("pCache", Cache);
181 for(int i = 0; i < Cache->nObj; i ++ )
183 if( !Cache->ObjectStates[i] ) {
184 Cache->ObjectStates[i] = 1;
185 void *rv = (char*)Cache->First + i*Cache->ObjectSize;
195 ACPI_STATUS AcpiOsReleaseObject(ACPI_CACHE_T *Cache, void *Object)
197 if( Cache == NULL || Object == NULL )
198 return AE_BAD_PARAMETER;
200 tVAddr delta = (tVAddr)Object - (tVAddr)Cache->First;
201 delta /= Cache->ObjectSize;
202 LOG("delta = %i, (limit %i)", delta, Cache->nObj);
204 if( delta > Cache->nObj )
205 return AE_BAD_PARAMETER;
207 Cache->ObjectStates[delta] = 0;
212 void *AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_SIZE Length)
214 if( PhysicalAddress < ONEMEG )
215 return (void*)(KERNEL_BASE | PhysicalAddress);
217 Uint ofs = PhysicalAddress & (PAGE_SIZE-1);
218 int npages = (ofs + Length + (PAGE_SIZE-1)) / PAGE_SIZE;
219 void *rv = ((char*)MM_MapHWPages(PhysicalAddress, npages)) + ofs;
220 // MM_DumpTables(0, -1);
221 LOG("Map (%P+%i pg) to %p", PhysicalAddress, npages, rv);
225 void AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
227 if( (tVAddr)LogicalAddress - KERNEL_BASE < ONEMEG )
230 LOG("%p", LogicalAddress);
232 Uint ofs = (tVAddr)LogicalAddress & (PAGE_SIZE-1);
233 int npages = (ofs + Length + (PAGE_SIZE-1)) / PAGE_SIZE;
234 // TODO: Validate `Length` is the same as was passed to AcpiOsMapMemory
235 MM_UnmapHWPages( (tVAddr)LogicalAddress, npages);
238 ACPI_STATUS AcpiOsGetPhysicalAddress(void *LogicalAddress, ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
240 if( LogicalAddress == NULL || PhysicalAddress == NULL )
241 return AE_BAD_PARAMETER;
243 tPAddr rv = MM_GetPhysAddr(LogicalAddress);
246 *PhysicalAddress = rv;
250 void *AcpiOsAllocate(ACPI_SIZE Size)
255 void AcpiOsFree(void *Memory)
260 BOOLEAN AcpiOsReadable(void *Memory, ACPI_SIZE Length)
262 return CheckMem(Memory, Length);
265 BOOLEAN AcpiOsWritable(void *Memory, ACPI_SIZE Length)
267 // TODO: Actually check if it's writable
268 return CheckMem(Memory, Length);
273 ACPI_THREAD_ID AcpiOsGetThreadId(void)
275 return Threads_GetTID() + 1;
278 ACPI_STATUS AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function, void *Context)
280 // TODO: Need to store currently executing functions
281 if( Function == NULL )
282 return AE_BAD_PARAMETER;
283 Proc_SpawnWorker(Function, Context);
287 void AcpiOsSleep(UINT64 Milliseconds)
289 Time_Delay(Milliseconds);
292 void AcpiOsStall(UINT32 Microseconds)
294 // TODO: need a microsleep function
295 Microseconds += (1000-1);
296 Microseconds /= 1000;
297 Time_Delay(Microseconds);
300 void AcpiOsWaitEventsComplete(void)
305 // --- Mutexes etc ---
306 ACPI_STATUS AcpiOsCreateMutex(ACPI_MUTEX *OutHandle)
310 return AE_BAD_PARAMETER;
311 tMutex *ret = calloc( sizeof(tMutex), 1 );
314 ret->Name = "AcpiOsCreateMutex";
320 void AcpiOsDeleteMutex(ACPI_MUTEX Handle)
322 Mutex_Acquire(Handle);
326 ACPI_STATUS AcpiOsAcquireMutex(ACPI_MUTEX Handle, UINT16 Timeout)
329 return AE_BAD_PARAMETER;
331 Mutex_Acquire(Handle);
336 void AcpiOsReleaseMutex(ACPI_MUTEX Handle)
338 Mutex_Release(Handle);
341 ACPI_STATUS AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, ACPI_SEMAPHORE *OutHandle)
343 LOG("(MaxUnits=%i,InitialUnits=%i)", MaxUnits, InitialUnits);
345 return AE_BAD_PARAMETER;
346 tSemaphore *ret = calloc( sizeof(tSemaphore), 1 );
350 Semaphore_Init(ret, InitialUnits, MaxUnits, "AcpiOsCreateSemaphore", "");
355 ACPI_STATUS AcpiOsDeleteSemaphore(ACPI_SEMAPHORE Handle)
358 return AE_BAD_PARAMETER;
365 ACPI_STATUS AcpiOsWaitSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units, UINT16 Timeout)
368 return AE_BAD_PARAMETER;
373 // NOTE: Possible race condition
374 if( Semaphore_GetValue(Handle) >= Units ) {
375 Semaphore_Wait(Handle, Units);
383 while(rem && now() - start < Timeout)
385 rem -= Semaphore_Wait(Handle, rem);
389 Semaphore_Signal(Handle, Units - rem);
396 ACPI_STATUS AcpiOsSignalSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units)
399 return AE_BAD_PARAMETER;
401 // TODO: Support AE_LIMIT detection early (to avoid blocks)
404 int rv = Semaphore_Signal(Handle, Units);
411 ACPI_STATUS AcpiOsCreateLock(ACPI_SPINLOCK *OutHandle)
415 return AE_BAD_PARAMETER;
416 tShortSpinlock *lock = calloc(sizeof(tShortSpinlock), 1);
424 void AcpiOsDeleteLock(ACPI_SPINLOCK Handle)
429 ACPI_CPU_FLAGS AcpiOsAcquireLock(ACPI_SPINLOCK Handle)
435 void AcpiOsReleaseLock(ACPI_SPINLOCK Handle, ACPI_CPU_FLAGS Flags)
440 // --- Interrupt handling ---
441 #define N_INT_LEVELS 16
442 ACPI_OSD_HANDLER gaACPI_InterruptHandlers[N_INT_LEVELS];
443 void *gaACPI_InterruptData[N_INT_LEVELS];
444 int gaACPI_InterruptHandles[N_INT_LEVELS];
446 void ACPI_int_InterruptProxy(int IRQ, void *data)
448 if( !gaACPI_InterruptHandlers[IRQ] )
450 gaACPI_InterruptHandlers[IRQ](gaACPI_InterruptData[IRQ]);
453 ACPI_STATUS AcpiOsInstallInterruptHandler(UINT32 InterruptLevel, ACPI_OSD_HANDLER Handler, void *Context)
455 if( InterruptLevel >= N_INT_LEVELS || Handler == NULL )
456 return AE_BAD_PARAMETER;
457 if( gaACPI_InterruptHandlers[InterruptLevel] )
458 return AE_ALREADY_EXISTS;
460 gaACPI_InterruptHandlers[InterruptLevel] = Handler;
461 gaACPI_InterruptData[InterruptLevel] = Context;
463 gaACPI_InterruptHandles[InterruptLevel] = IRQ_AddHandler(InterruptLevel, ACPI_int_InterruptProxy, NULL);
467 ACPI_STATUS AcpiOsRemoveInterruptHandler(UINT32 InterruptLevel, ACPI_OSD_HANDLER Handler)
469 if( InterruptLevel >= N_INT_LEVELS || Handler == NULL )
470 return AE_BAD_PARAMETER;
471 if( gaACPI_InterruptHandlers[InterruptLevel] != Handler )
473 gaACPI_InterruptHandlers[InterruptLevel] = NULL;
474 IRQ_RemHandler(gaACPI_InterruptHandles[InterruptLevel]);
478 // --- Memory Access ---
479 ACPI_STATUS AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 *Value, UINT32 Width)
482 if( Address < ONEMEG ) {
483 ptr = (void*)(KERNEL_BASE | Address);
486 ptr = (char*)MM_MapTemp(Address) + (Address & 0xFFF);
491 case 8: *Value = *(Uint8 *)ptr; break;
492 case 16: *Value = *(Uint16*)ptr; break;
493 case 32: *Value = *(Uint32*)ptr; break;
494 case 64: *Value = *(Uint64*)ptr; break;
497 if( Address >= ONEMEG ) {
504 ACPI_STATUS AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 Value, UINT32 Width)
507 if( Address < ONEMEG ) {
508 ptr = (void*)(KERNEL_BASE | Address);
511 ptr = (char*)MM_MapTemp(Address) + (Address & 0xFFF);
516 case 8: *(Uint8 *)ptr = Value; break;
517 case 16: *(Uint16*)ptr = Value; break;
518 case 32: *(Uint32*)ptr = Value; break;
519 case 64: *(Uint64*)ptr = Value; break;
521 return AE_BAD_PARAMETER;
524 if( Address >= 1024*1024 ) {
531 // --- Port Input / Output ---
532 ACPI_STATUS AcpiOsReadPort(ACPI_IO_ADDRESS Address, UINT32 *Value, UINT32 Width)
536 case 8: *Value = inb(Address); break;
537 case 16: *Value = inw(Address); break;
538 case 32: *Value = ind(Address); break;
540 return AE_BAD_PARAMETER;
545 ACPI_STATUS AcpiOsWritePort(ACPI_IO_ADDRESS Address, UINT32 Value, UINT32 Width)
549 case 8: outb(Address, Value); break;
550 case 16: outw(Address, Value); break;
551 case 32: outd(Address, Value); break;
553 return AE_BAD_PARAMETER;
558 // --- PCI Configuration Space Access ---
559 ACPI_STATUS AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 *Value, UINT32 Width)
562 return AE_NOT_IMPLEMENTED;
565 ACPI_STATUS AcpiOsWritePciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 Value, UINT32 Width)
568 return AE_NOT_IMPLEMENTED;
571 // --- Formatted Output ---
572 void AcpiOsPrintf(const char *Format, ...)
575 va_start(args, Format);
582 void AcpiOsVprintf(const char *Format, va_list Args)
587 void AcpiOsRedirectOutput(void *Destination)
589 // TODO: Do I even need to impliment this?
592 // --- Miscellaneous ---
593 UINT64 AcpiOsGetTimer(void)
595 return now() * 10 * 1000;
598 ACPI_STATUS AcpiOsSignal(UINT32 Function, void *Info)
602 case ACPI_SIGNAL_FATAL: {
603 ACPI_SIGNAL_FATAL_INFO *finfo = Info;
604 Log_Error("ACPI AML", "Fatal %x %x %x", finfo->Type, finfo->Code, finfo->Argument);
606 case ACPI_SIGNAL_BREAKPOINT: {
607 Log_Notice("ACPI AML", "Breakpoint %s", Info);
613 ACPI_STATUS AcpiOsGetLine(char *Buffer, UINT32 BufferLength, UINT32 *BytesRead)
616 return AE_NOT_IMPLEMENTED;