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 char *maploc = (void*)MM_MapHWPages(PhysicalAddress, npages);
227 LOG("Mapping %P+0x%x failed", PhysicalAddress, Length);
230 // MM_DumpTables(0, -1);
231 void *rv = maploc + ofs;
232 LOG("Map (%P+%i pg) to %p", PhysicalAddress, npages, rv);
236 void AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
238 if( (tVAddr)LogicalAddress - KERNEL_BASE < ONEMEG )
241 LOG("%p", LogicalAddress);
243 Uint ofs = (tVAddr)LogicalAddress & (PAGE_SIZE-1);
244 int npages = (ofs + Length + (PAGE_SIZE-1)) / PAGE_SIZE;
245 // TODO: Validate `Length` is the same as was passed to AcpiOsMapMemory
246 MM_UnmapHWPages( (tVAddr)LogicalAddress, npages);
249 ACPI_STATUS AcpiOsGetPhysicalAddress(void *LogicalAddress, ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
251 if( LogicalAddress == NULL || PhysicalAddress == NULL )
252 return AE_BAD_PARAMETER;
254 tPAddr rv = MM_GetPhysAddr(LogicalAddress);
257 *PhysicalAddress = rv;
261 void *AcpiOsAllocate(ACPI_SIZE Size)
266 void AcpiOsFree(void *Memory)
271 BOOLEAN AcpiOsReadable(void *Memory, ACPI_SIZE Length)
273 return CheckMem(Memory, Length);
276 BOOLEAN AcpiOsWritable(void *Memory, ACPI_SIZE Length)
278 // TODO: Actually check if it's writable
279 return CheckMem(Memory, Length);
284 ACPI_THREAD_ID AcpiOsGetThreadId(void)
286 return Threads_GetTID() + 1;
289 ACPI_STATUS AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function, void *Context)
291 // TODO: Need to store currently executing functions
292 if( Function == NULL )
293 return AE_BAD_PARAMETER;
294 Proc_SpawnWorker(Function, Context);
298 void AcpiOsSleep(UINT64 Milliseconds)
300 Time_Delay(Milliseconds);
303 void AcpiOsStall(UINT32 Microseconds)
305 // TODO: need a microsleep function
306 Microseconds += (1000-1);
307 Microseconds /= 1000;
308 Time_Delay(Microseconds);
311 void AcpiOsWaitEventsComplete(void)
316 // --- Mutexes etc ---
317 ACPI_STATUS AcpiOsCreateMutex(ACPI_MUTEX *OutHandle)
321 return AE_BAD_PARAMETER;
322 tMutex *ret = calloc( sizeof(tMutex), 1 );
324 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
327 ret->Name = "AcpiOsCreateMutex";
333 void AcpiOsDeleteMutex(ACPI_MUTEX Handle)
335 Mutex_Acquire(Handle);
339 ACPI_STATUS AcpiOsAcquireMutex(ACPI_MUTEX Handle, UINT16 Timeout)
342 return AE_BAD_PARAMETER;
344 Mutex_Acquire(Handle);
349 void AcpiOsReleaseMutex(ACPI_MUTEX Handle)
351 Mutex_Release(Handle);
354 ACPI_STATUS AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, ACPI_SEMAPHORE *OutHandle)
356 LOG("(MaxUnits=%i,InitialUnits=%i)", MaxUnits, InitialUnits);
358 return AE_BAD_PARAMETER;
359 tSemaphore *ret = calloc( sizeof(tSemaphore), 1 );
361 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
365 Semaphore_Init(ret, InitialUnits, MaxUnits, "AcpiOsCreateSemaphore", "");
370 ACPI_STATUS AcpiOsDeleteSemaphore(ACPI_SEMAPHORE Handle)
373 return AE_BAD_PARAMETER;
380 ACPI_STATUS AcpiOsWaitSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units, UINT16 Timeout)
383 return AE_BAD_PARAMETER;
388 // NOTE: Possible race condition
389 if( Semaphore_GetValue(Handle) >= Units ) {
390 Semaphore_Wait(Handle, Units);
398 while(rem && now() - start < Timeout)
400 rem -= Semaphore_Wait(Handle, rem);
404 Semaphore_Signal(Handle, Units - rem);
411 ACPI_STATUS AcpiOsSignalSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units)
414 return AE_BAD_PARAMETER;
416 // TODO: Support AE_LIMIT detection early (to avoid blocks)
419 int rv = Semaphore_Signal(Handle, Units);
426 ACPI_STATUS AcpiOsCreateLock(ACPI_SPINLOCK *OutHandle)
430 return AE_BAD_PARAMETER;
431 tShortSpinlock *lock = calloc(sizeof(tShortSpinlock), 1);
439 void AcpiOsDeleteLock(ACPI_SPINLOCK Handle)
444 ACPI_CPU_FLAGS AcpiOsAcquireLock(ACPI_SPINLOCK Handle)
450 void AcpiOsReleaseLock(ACPI_SPINLOCK Handle, ACPI_CPU_FLAGS Flags)
455 // --- Interrupt handling ---
456 #define N_INT_LEVELS 16
457 ACPI_OSD_HANDLER gaACPI_InterruptHandlers[N_INT_LEVELS];
458 void *gaACPI_InterruptData[N_INT_LEVELS];
459 int gaACPI_InterruptHandles[N_INT_LEVELS];
461 void ACPI_int_InterruptProxy(int IRQ, void *data)
463 if( !gaACPI_InterruptHandlers[IRQ] )
465 gaACPI_InterruptHandlers[IRQ](gaACPI_InterruptData[IRQ]);
468 ACPI_STATUS AcpiOsInstallInterruptHandler(UINT32 InterruptLevel, ACPI_OSD_HANDLER Handler, void *Context)
470 if( InterruptLevel >= N_INT_LEVELS || Handler == NULL )
471 return AE_BAD_PARAMETER;
472 if( gaACPI_InterruptHandlers[InterruptLevel] )
473 return AE_ALREADY_EXISTS;
475 gaACPI_InterruptHandlers[InterruptLevel] = Handler;
476 gaACPI_InterruptData[InterruptLevel] = Context;
478 gaACPI_InterruptHandles[InterruptLevel] = IRQ_AddHandler(InterruptLevel, ACPI_int_InterruptProxy, NULL);
482 ACPI_STATUS AcpiOsRemoveInterruptHandler(UINT32 InterruptLevel, ACPI_OSD_HANDLER Handler)
484 if( InterruptLevel >= N_INT_LEVELS || Handler == NULL )
485 return AE_BAD_PARAMETER;
486 if( gaACPI_InterruptHandlers[InterruptLevel] != Handler )
488 gaACPI_InterruptHandlers[InterruptLevel] = NULL;
489 IRQ_RemHandler(gaACPI_InterruptHandles[InterruptLevel]);
493 // --- Memory Access ---
494 ACPI_STATUS AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 *Value, UINT32 Width)
497 if( Address < ONEMEG ) {
498 ptr = (void*)(KERNEL_BASE | Address);
501 ptr = (char*)MM_MapTemp(Address) + (Address & 0xFFF);
506 case 8: *Value = *(Uint8 *)ptr; break;
507 case 16: *Value = *(Uint16*)ptr; break;
508 case 32: *Value = *(Uint32*)ptr; break;
509 case 64: *Value = *(Uint64*)ptr; break;
512 if( Address >= ONEMEG ) {
519 ACPI_STATUS AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 Value, UINT32 Width)
522 if( Address < ONEMEG ) {
523 ptr = (void*)(KERNEL_BASE | Address);
526 ptr = (char*)MM_MapTemp(Address) + (Address & 0xFFF);
531 case 8: *(Uint8 *)ptr = Value; break;
532 case 16: *(Uint16*)ptr = Value; break;
533 case 32: *(Uint32*)ptr = Value; break;
534 case 64: *(Uint64*)ptr = Value; break;
536 return AE_BAD_PARAMETER;
539 if( Address >= 1024*1024 ) {
546 // --- Port Input / Output ---
547 ACPI_STATUS AcpiOsReadPort(ACPI_IO_ADDRESS Address, UINT32 *Value, UINT32 Width)
551 case 8: *Value = inb(Address); break;
552 case 16: *Value = inw(Address); break;
553 case 32: *Value = ind(Address); break;
555 return AE_BAD_PARAMETER;
560 ACPI_STATUS AcpiOsWritePort(ACPI_IO_ADDRESS Address, UINT32 Value, UINT32 Width)
564 case 8: outb(Address, Value); break;
565 case 16: outw(Address, Value); break;
566 case 32: outd(Address, Value); break;
568 return AE_BAD_PARAMETER;
573 // --- PCI Configuration Space Access ---
574 ACPI_STATUS AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 *Value, UINT32 Width)
577 return AE_NOT_IMPLEMENTED;
580 ACPI_STATUS AcpiOsWritePciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 Value, UINT32 Width)
583 return AE_NOT_IMPLEMENTED;
586 // --- Formatted Output ---
587 void AcpiOsPrintf(const char *Format, ...)
590 va_start(args, Format);
597 void AcpiOsVprintf(const char *Format, va_list Args)
602 void AcpiOsRedirectOutput(void *Destination)
604 // TODO: Do I even need to impliment this?
607 // --- Miscellaneous ---
608 UINT64 AcpiOsGetTimer(void)
610 return now() * 10 * 1000;
613 ACPI_STATUS AcpiOsSignal(UINT32 Function, void *Info)
617 case ACPI_SIGNAL_FATAL: {
618 ACPI_SIGNAL_FATAL_INFO *finfo = Info;
619 Log_Error("ACPI AML", "Fatal %x %x %x", finfo->Type, finfo->Code, finfo->Argument);
621 case ACPI_SIGNAL_BREAKPOINT: {
622 Log_Notice("ACPI AML", "Breakpoint %s", Info);
628 ACPI_STATUS AcpiOsGetLine(char *Buffer, UINT32 BufferLength, UINT32 *BytesRead)
631 return AE_NOT_IMPLEMENTED;