Merge branch 'master' of git://cadel.mutabah.net/acess2
[tpg/acess2.git] / KernelLand / Kernel / arch / x86 / acpica.c
1 /*
2  * Acess2 Kernel (x86 Core)
3  * - By John Hodge (thePowersGang)
4  *
5  * acpica.c
6  * - ACPICA Interface
7  */
8 #define ACPI_DEBUG_OUTPUT       0
9 #define DEBUG   0
10 #define _AcpiModuleName "Shim"
11 #define _COMPONENT      "Acess"
12 #include <acpi.h>
13 #include <timers.h>
14 #include <mutex.h>
15 #include <semaphore.h>
16
17 #define ONEMEG  (1024*1024)
18
19 // === GLOBALS ===
20 // - RSDP Address from uEFI
21 tPAddr  gACPI_RSDPOverride = 0;
22
23 // === PROTOTYPES ===
24 int     ACPICA_Initialise(void);
25 void    ACPI_int_InterruptProxy(int IRQ, void *data);
26
27
28 // === CODE ===
29 int ACPICA_Initialise(void)
30 {
31         ACPI_STATUS     rv;
32
33         #ifdef ACPI_DEBUG_OUTPUT
34         AcpiDbgLevel = ACPI_DB_ALL;
35         #endif
36
37         rv = AcpiInitializeSubsystem();
38         if( ACPI_FAILURE(rv) )
39         {
40                 Log_Error("ACPI", "AcpiInitializeSubsystem: %i", rv);
41                 return -1;
42         }
43         
44         rv = AcpiInitializeTables(NULL, 16, FALSE);
45         if( ACPI_FAILURE(rv) )
46         {
47                 Log_Error("ACPI", "AcpiInitializeTables: %i", rv);
48                 AcpiTerminate();
49                 return -1;
50         }
51
52         // AcpiInitializeTables?
53         rv = AcpiLoadTables();
54         if( ACPI_FAILURE(rv) )
55         {
56                 Log_Error("ACPI", "AcpiLoadTables: %i", rv);
57                 AcpiTerminate();
58                 return -1;
59         }
60         
61         rv = AcpiEnableSubsystem(ACPI_FULL_INITIALIZATION);
62         if( ACPI_FAILURE(rv) )
63         {
64                 Log_Error("ACPI", "AcpiEnableSubsystem: %i", rv);
65                 AcpiTerminate();
66                 return -1;
67         }
68
69         return 0;
70 }
71
72 // ---------------
73 // --- Exports ---
74 // ---------------
75 ACPI_STATUS AcpiOsInitialize(void)
76 {
77         return AE_OK;
78 }
79
80 ACPI_STATUS AcpiOsTerminate(void)
81 {
82         return AE_OK;
83 }
84
85 ACPI_PHYSICAL_ADDRESS AcpiOsGetRootPointer(void)
86 {
87         ACPI_SIZE       val;
88         ACPI_STATUS     rv;
89
90         if( gACPI_RSDPOverride )
91                 return gACPI_RSDPOverride;      
92
93         rv = AcpiFindRootPointer(&val);
94         if( ACPI_FAILURE(rv) )
95                 return 0;
96
97         LOG("val=0x%x", val);
98         
99         return val;
100         // (Or use EFI)
101 }
102
103 ACPI_STATUS AcpiOsPredefinedOverride(const ACPI_PREDEFINED_NAMES *PredefinedObject, ACPI_STRING *NewValue)
104 {
105         *NewValue = NULL;
106         return AE_OK;
107 }
108
109 ACPI_STATUS AcpiOsTableOverride(ACPI_TABLE_HEADER *ExisitingTable, ACPI_TABLE_HEADER **NewTable)
110 {
111         *NewTable = NULL;
112         return AE_OK;
113 }
114
115 ACPI_STATUS AcpiOsPhysicalTableOverride(ACPI_TABLE_HEADER *ExisitingTable, ACPI_PHYSICAL_ADDRESS *NewAddress, UINT32 *NewTableLength)
116 {
117         *NewAddress = 0;
118         return AE_OK;
119 }
120
121 // -- Memory Management ---
122 struct sACPICache
123 {
124         Uint16  nObj;
125         Uint16  ObjectSize;
126         char    *Name;
127         void    *First;
128         char    ObjectStates[];
129 };
130
131 ACPI_STATUS AcpiOsCreateCache(char *CacheName, UINT16 ObjectSize, UINT16 MaxDepth, ACPI_CACHE_T **ReturnCache)
132 {
133         tACPICache      *ret;
134          int    namelen = (CacheName ? strlen(CacheName) : 0) + 1;
135         LOG("CacheName=%s, ObjSize=%x, MaxDepth=%x", CacheName, ObjectSize, MaxDepth);
136
137         namelen = (namelen + 3) & ~3;
138
139         ret = malloc(sizeof(*ret) + MaxDepth*sizeof(char) + namelen + MaxDepth*ObjectSize);
140         if( !ret ) {
141                 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
142                 return AE_NO_MEMORY;
143         }
144
145         ret->nObj = MaxDepth;
146         ret->ObjectSize = ObjectSize;
147         ret->Name = (char*)(ret->ObjectStates + MaxDepth);
148         ret->First = ret->Name + namelen;
149         if( CacheName )
150                 strcpy(ret->Name, CacheName);
151         else
152                 ret->Name[0] = 0;
153         memset(ret->ObjectStates, 0, sizeof(char)*MaxDepth);
154
155         LOG("Allocated cache %p '%s' (%i x 0x%x)", ret, CacheName, MaxDepth, ObjectSize);
156         
157         *ReturnCache = ret;
158         
159         return AE_OK;
160 }
161
162 ACPI_STATUS AcpiOsDeleteCache(ACPI_CACHE_T *Cache)
163 {
164         if( Cache == NULL )
165                 return AE_BAD_PARAMETER;
166
167         free(Cache);
168         return AE_OK;
169 }
170
171 ACPI_STATUS AcpiOsPurgeCache(ACPI_CACHE_T *Cache)
172 {
173         if( Cache == NULL )
174                 return AE_BAD_PARAMETER;
175
176         memset(Cache->ObjectStates, 0, sizeof(char)*Cache->nObj);
177
178         return AE_OK;
179 }
180
181 void *AcpiOsAcquireObject(ACPI_CACHE_T *Cache)
182 {
183         ENTER("pCache", Cache);
184         for(int i = 0; i < Cache->nObj; i ++ )
185         {
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);
190                         LEAVE('p', rv);
191                         return rv;
192                 }
193         }
194
195         Log_Debug("ACPICA", "AcpiOsAcquireObject: %i objects used in cache '%s'", Cache->nObj, Cache->Name);
196
197         LEAVE('n');
198         return NULL;
199 }
200
201 ACPI_STATUS AcpiOsReleaseObject(ACPI_CACHE_T *Cache, void *Object)
202 {
203         if( Cache == NULL || Object == NULL )
204                 return AE_BAD_PARAMETER;
205
206         tVAddr delta = (tVAddr)Object - (tVAddr)Cache->First;
207         delta /= Cache->ObjectSize;
208         LOG("Cache=%p, delta = %i, (limit %i)", Cache, delta, Cache->nObj);
209         
210         if( delta >= Cache->nObj )
211                 return AE_BAD_PARAMETER;
212         
213         Cache->ObjectStates[delta] = 0;
214
215         return AE_OK;
216 }
217
218 void *AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_SIZE Length)
219 {
220         if( PhysicalAddress < ONEMEG )
221                 return (void*)(KERNEL_BASE | PhysicalAddress);
222         
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);
226         if(!maploc) {
227                 LOG("Mapping %P+0x%x failed", PhysicalAddress, Length);
228                 return NULL;
229         }
230 //      MM_DumpTables(0, -1);
231         void *rv = maploc + ofs;
232         LOG("Map (%P+%i pg) to %p", PhysicalAddress, npages, rv);
233         return rv;
234 }
235
236 void AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
237 {
238         if( (tVAddr)LogicalAddress - KERNEL_BASE < ONEMEG )
239                 return ;
240
241         LOG("%p", LogicalAddress);
242
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);
247 }
248
249 ACPI_STATUS AcpiOsGetPhysicalAddress(void *LogicalAddress, ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
250 {
251         if( LogicalAddress == NULL || PhysicalAddress == NULL )
252                 return AE_BAD_PARAMETER;
253         
254         tPAddr  rv = MM_GetPhysAddr(LogicalAddress);
255         if( rv == 0 )
256                 return AE_ERROR;
257         *PhysicalAddress = rv;
258         return AE_OK;
259 }
260
261 void *AcpiOsAllocate(ACPI_SIZE Size)
262 {
263         return malloc(Size);
264 }
265
266 void AcpiOsFree(void *Memory)
267 {
268         return free(Memory);
269 }
270
271 BOOLEAN AcpiOsReadable(void *Memory, ACPI_SIZE Length)
272 {
273         return CheckMem(Memory, Length);
274 }
275
276 BOOLEAN AcpiOsWritable(void *Memory, ACPI_SIZE Length)
277 {
278         // TODO: Actually check if it's writable
279         return CheckMem(Memory, Length);
280 }
281
282
283 // --- Threads ---
284 ACPI_THREAD_ID AcpiOsGetThreadId(void)
285 {
286         return Threads_GetTID() + 1;
287 }
288
289 ACPI_STATUS AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function, void *Context)
290 {
291         // TODO: Need to store currently executing functions
292         if( Function == NULL )
293                 return AE_BAD_PARAMETER;
294         Proc_SpawnWorker(Function, Context);
295         return AE_OK;
296 }
297
298 void AcpiOsSleep(UINT64 Milliseconds)
299 {
300         Time_Delay(Milliseconds);
301 }
302
303 void AcpiOsStall(UINT32 Microseconds)
304 {
305         // TODO: need a microsleep function
306         Microseconds += (1000-1);
307         Microseconds /= 1000;
308         Time_Delay(Microseconds);
309 }
310
311 void AcpiOsWaitEventsComplete(void)
312 {
313         // TODO: 
314 }
315
316 // --- Mutexes etc ---
317 ACPI_STATUS AcpiOsCreateMutex(ACPI_MUTEX *OutHandle)
318 {
319         LOG("()");
320         if( !OutHandle )
321                 return AE_BAD_PARAMETER;
322         tMutex  *ret = calloc( sizeof(tMutex), 1 );
323         if( !ret ) {
324                 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
325                 return AE_NO_MEMORY;
326         }
327         ret->Name = "AcpiOsCreateMutex";
328         *OutHandle = ret;
329         
330         return AE_OK;
331 }
332
333 void AcpiOsDeleteMutex(ACPI_MUTEX Handle)
334 {
335         Mutex_Acquire(Handle);
336         free(Handle);
337 }
338
339 ACPI_STATUS AcpiOsAcquireMutex(ACPI_MUTEX Handle, UINT16 Timeout)
340 {
341         if( Handle == NULL )
342                 return AE_BAD_PARAMETER;
343
344         Mutex_Acquire(Handle);  
345
346         return AE_OK;
347 }
348
349 void AcpiOsReleaseMutex(ACPI_MUTEX Handle)
350 {
351         Mutex_Release(Handle);
352 }
353
354 ACPI_STATUS AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, ACPI_SEMAPHORE *OutHandle)
355 {
356         LOG("(MaxUnits=%i,InitialUnits=%i)", MaxUnits, InitialUnits);
357         if( !OutHandle )
358                 return AE_BAD_PARAMETER;
359         tSemaphore      *ret = calloc( sizeof(tSemaphore), 1 );
360         if( !ret ) {
361                 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
362                 return AE_NO_MEMORY;
363         }
364         
365         Semaphore_Init(ret, InitialUnits, MaxUnits, "AcpiOsCreateSemaphore", "");
366         *OutHandle = ret;
367         return AE_OK;
368 }
369
370 ACPI_STATUS AcpiOsDeleteSemaphore(ACPI_SEMAPHORE Handle)
371 {
372         if( !Handle )
373                 return AE_BAD_PARAMETER;
374
375         free(Handle);   
376
377         return AE_OK;
378 }
379
380 ACPI_STATUS AcpiOsWaitSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units, UINT16 Timeout)
381 {
382         if( !Handle )
383                 return AE_BAD_PARAMETER;
384
385         // Special case
386         if( Timeout == 0 )
387         {
388                 // NOTE: Possible race condition
389                 if( Semaphore_GetValue(Handle) >= Units ) {
390                         Semaphore_Wait(Handle, Units);
391                         return AE_OK;
392                 }
393                 return AE_TIME;
394         }
395
396         tTime   start = now();
397         UINT32  rem = Units;
398         while(rem && now() - start < Timeout)
399         {
400                 rem -= Semaphore_Wait(Handle, rem);
401         }
402
403         if( rem ) {
404                 Semaphore_Signal(Handle, Units - rem);
405                 return AE_TIME;
406         }
407
408         return AE_OK;
409 }
410
411 ACPI_STATUS AcpiOsSignalSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units)
412 {
413         if( !Handle )
414                 return AE_BAD_PARAMETER;
415
416         // TODO: Support AE_LIMIT detection early (to avoid blocks)
417
418         // NOTE: Blocks
419         int rv = Semaphore_Signal(Handle, Units);
420         if( rv != Units )
421                 return AE_LIMIT;
422         
423         return AE_OK;
424 }
425
426 ACPI_STATUS AcpiOsCreateLock(ACPI_SPINLOCK *OutHandle)
427 {
428         LOG("()");
429         if( !OutHandle )
430                 return AE_BAD_PARAMETER;
431         tShortSpinlock  *lock = calloc(sizeof(tShortSpinlock), 1);
432         if( !lock )
433                 return AE_NO_MEMORY;
434         
435         *OutHandle = lock;
436         return AE_OK;
437 }
438
439 void AcpiOsDeleteLock(ACPI_SPINLOCK Handle)
440 {
441         free(Handle);
442 }
443
444 ACPI_CPU_FLAGS AcpiOsAcquireLock(ACPI_SPINLOCK Handle)
445 {
446         SHORTLOCK(Handle);
447         return 0;
448 }
449
450 void AcpiOsReleaseLock(ACPI_SPINLOCK Handle, ACPI_CPU_FLAGS Flags)
451 {
452         SHORTREL(Handle);
453 }
454
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];
460
461 void ACPI_int_InterruptProxy(int IRQ, void *data)
462 {
463         if( !gaACPI_InterruptHandlers[IRQ] )
464                 return ;
465         gaACPI_InterruptHandlers[IRQ](gaACPI_InterruptData[IRQ]);
466 }
467
468 ACPI_STATUS AcpiOsInstallInterruptHandler(UINT32 InterruptLevel, ACPI_OSD_HANDLER Handler, void *Context)
469 {
470         if( InterruptLevel >= N_INT_LEVELS || Handler == NULL )
471                 return AE_BAD_PARAMETER;
472         if( gaACPI_InterruptHandlers[InterruptLevel] )
473                 return AE_ALREADY_EXISTS;
474
475         gaACPI_InterruptHandlers[InterruptLevel] = Handler;
476         gaACPI_InterruptData[InterruptLevel] = Context;
477
478         gaACPI_InterruptHandles[InterruptLevel] = IRQ_AddHandler(InterruptLevel, ACPI_int_InterruptProxy, NULL);
479         return AE_OK;
480 }
481
482 ACPI_STATUS AcpiOsRemoveInterruptHandler(UINT32 InterruptLevel, ACPI_OSD_HANDLER Handler)
483 {
484         if( InterruptLevel >= N_INT_LEVELS || Handler == NULL )
485                 return AE_BAD_PARAMETER;
486         if( gaACPI_InterruptHandlers[InterruptLevel] != Handler )
487                 return AE_NOT_EXIST;
488         gaACPI_InterruptHandlers[InterruptLevel] = NULL;
489         IRQ_RemHandler(gaACPI_InterruptHandles[InterruptLevel]);
490         return AE_OK;
491 }
492
493 // --- Memory Access ---
494 ACPI_STATUS AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 *Value, UINT32 Width)
495 {
496         void *ptr;
497         if( Address < ONEMEG ) {
498                 ptr = (void*)(KERNEL_BASE | Address);
499         }
500         else {
501                 ptr = (char*)MM_MapTemp(Address) + (Address & 0xFFF);
502         }
503
504         switch(Width)
505         {
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;
510         }
511
512         if( Address >= ONEMEG ) {
513                 MM_FreeTemp(ptr);
514         }
515
516         return AE_OK;
517 }
518
519 ACPI_STATUS AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 Value, UINT32 Width)
520 {
521         void *ptr;
522         if( Address < ONEMEG ) {
523                 ptr = (void*)(KERNEL_BASE | Address);
524         }
525         else {
526                 ptr = (char*)MM_MapTemp(Address) + (Address & 0xFFF);
527         }
528
529         switch(Width)
530         {
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;
535         default:
536                 return AE_BAD_PARAMETER;
537         }
538
539         if( Address >= 1024*1024 ) {
540                 MM_FreeTemp(ptr);
541         }
542         
543         return AE_OK;
544 }
545
546 // --- Port Input / Output ---
547 ACPI_STATUS AcpiOsReadPort(ACPI_IO_ADDRESS Address, UINT32 *Value, UINT32 Width)
548 {
549         switch(Width)
550         {
551         case 8:         *Value = inb(Address);  break;
552         case 16:        *Value = inw(Address);  break;
553         case 32:        *Value = ind(Address);  break;
554         default:
555                 return AE_BAD_PARAMETER;
556         }
557         return AE_OK;
558 }
559
560 ACPI_STATUS AcpiOsWritePort(ACPI_IO_ADDRESS Address, UINT32 Value, UINT32 Width)
561 {
562         switch(Width)
563         {
564         case 8:         outb(Address, Value);   break;
565         case 16:        outw(Address, Value);   break;
566         case 32:        outd(Address, Value);   break;
567         default:
568                 return AE_BAD_PARAMETER;
569         }
570         return AE_OK;
571 }
572
573 // --- PCI Configuration Space Access ---
574 ACPI_STATUS AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 *Value, UINT32 Width)
575 {
576         UNIMPLEMENTED();
577         return AE_NOT_IMPLEMENTED;
578 }
579
580 ACPI_STATUS AcpiOsWritePciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 Value, UINT32 Width)
581 {
582         UNIMPLEMENTED();
583         return AE_NOT_IMPLEMENTED;
584 }
585
586 // --- Formatted Output ---
587 void AcpiOsPrintf(const char *Format, ...)
588 {
589         va_list args;
590         va_start(args, Format);
591
592         LogFV(Format, args);
593
594         va_end(args);
595 }
596
597 void AcpiOsVprintf(const char *Format, va_list Args)
598 {
599         LogFV(Format, Args);
600 }
601
602 void AcpiOsRedirectOutput(void *Destination)
603 {
604         // TODO: Do I even need to impliment this?
605 }
606
607 // --- Miscellaneous ---
608 UINT64 AcpiOsGetTimer(void)
609 {
610         return now() * 10 * 1000;
611 }
612
613 ACPI_STATUS AcpiOsSignal(UINT32 Function, void *Info)
614 {
615         switch(Function)
616         {
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);
620                 break; }
621         case ACPI_SIGNAL_BREAKPOINT: {
622                 Log_Notice("ACPI AML", "Breakpoint %s", Info);
623                 break; };
624         }
625         return AE_OK;
626 }
627
628 ACPI_STATUS AcpiOsGetLine(char *Buffer, UINT32 BufferLength, UINT32 *BytesRead)
629 {
630         UNIMPLEMENTED();
631         return AE_NOT_IMPLEMENTED;
632 }
633

UCC git Repository :: git.ucc.asn.au