Merge branch 'master' of git://localhost/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=%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", Cache->nObj);       
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         void *rv = ((char*)MM_MapHWPages(PhysicalAddress, npages)) + ofs;
226 //      MM_DumpTables(0, -1);
227         LOG("Map (%P+%i pg) to %p", PhysicalAddress, npages, rv);
228         return rv;
229 }
230
231 void AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
232 {
233         if( (tVAddr)LogicalAddress - KERNEL_BASE < ONEMEG )
234                 return ;
235
236         LOG("%p", LogicalAddress);
237
238         Uint    ofs = (tVAddr)LogicalAddress & (PAGE_SIZE-1);
239         int npages = (ofs + Length + (PAGE_SIZE-1)) / PAGE_SIZE;
240         // TODO: Validate `Length` is the same as was passed to AcpiOsMapMemory
241         MM_UnmapHWPages( (tVAddr)LogicalAddress, npages);
242 }
243
244 ACPI_STATUS AcpiOsGetPhysicalAddress(void *LogicalAddress, ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
245 {
246         if( LogicalAddress == NULL || PhysicalAddress == NULL )
247                 return AE_BAD_PARAMETER;
248         
249         tPAddr  rv = MM_GetPhysAddr(LogicalAddress);
250         if( rv == 0 )
251                 return AE_ERROR;
252         *PhysicalAddress = rv;
253         return AE_OK;
254 }
255
256 void *AcpiOsAllocate(ACPI_SIZE Size)
257 {
258         return malloc(Size);
259 }
260
261 void AcpiOsFree(void *Memory)
262 {
263         return free(Memory);
264 }
265
266 BOOLEAN AcpiOsReadable(void *Memory, ACPI_SIZE Length)
267 {
268         return CheckMem(Memory, Length);
269 }
270
271 BOOLEAN AcpiOsWritable(void *Memory, ACPI_SIZE Length)
272 {
273         // TODO: Actually check if it's writable
274         return CheckMem(Memory, Length);
275 }
276
277
278 // --- Threads ---
279 ACPI_THREAD_ID AcpiOsGetThreadId(void)
280 {
281         return Threads_GetTID() + 1;
282 }
283
284 ACPI_STATUS AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function, void *Context)
285 {
286         // TODO: Need to store currently executing functions
287         if( Function == NULL )
288                 return AE_BAD_PARAMETER;
289         Proc_SpawnWorker(Function, Context);
290         return AE_OK;
291 }
292
293 void AcpiOsSleep(UINT64 Milliseconds)
294 {
295         Time_Delay(Milliseconds);
296 }
297
298 void AcpiOsStall(UINT32 Microseconds)
299 {
300         // TODO: need a microsleep function
301         Microseconds += (1000-1);
302         Microseconds /= 1000;
303         Time_Delay(Microseconds);
304 }
305
306 void AcpiOsWaitEventsComplete(void)
307 {
308         // TODO: 
309 }
310
311 // --- Mutexes etc ---
312 ACPI_STATUS AcpiOsCreateMutex(ACPI_MUTEX *OutHandle)
313 {
314         LOG("()");
315         if( !OutHandle )
316                 return AE_BAD_PARAMETER;
317         tMutex  *ret = calloc( sizeof(tMutex), 1 );
318         if( !ret ) {
319                 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
320                 return AE_NO_MEMORY;
321         }
322         ret->Name = "AcpiOsCreateMutex";
323         *OutHandle = ret;
324         
325         return AE_OK;
326 }
327
328 void AcpiOsDeleteMutex(ACPI_MUTEX Handle)
329 {
330         Mutex_Acquire(Handle);
331         free(Handle);
332 }
333
334 ACPI_STATUS AcpiOsAcquireMutex(ACPI_MUTEX Handle, UINT16 Timeout)
335 {
336         if( Handle == NULL )
337                 return AE_BAD_PARAMETER;
338
339         Mutex_Acquire(Handle);  
340
341         return AE_OK;
342 }
343
344 void AcpiOsReleaseMutex(ACPI_MUTEX Handle)
345 {
346         Mutex_Release(Handle);
347 }
348
349 ACPI_STATUS AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, ACPI_SEMAPHORE *OutHandle)
350 {
351         LOG("(MaxUnits=%i,InitialUnits=%i)", MaxUnits, InitialUnits);
352         if( !OutHandle )
353                 return AE_BAD_PARAMETER;
354         tSemaphore      *ret = calloc( sizeof(tSemaphore), 1 );
355         if( !ret ) {
356                 Log_Notice("ACPICA", "%s: malloc() fail", __func__);
357                 return AE_NO_MEMORY;
358         }
359         
360         Semaphore_Init(ret, InitialUnits, MaxUnits, "AcpiOsCreateSemaphore", "");
361         *OutHandle = ret;
362         return AE_OK;
363 }
364
365 ACPI_STATUS AcpiOsDeleteSemaphore(ACPI_SEMAPHORE Handle)
366 {
367         if( !Handle )
368                 return AE_BAD_PARAMETER;
369
370         free(Handle);   
371
372         return AE_OK;
373 }
374
375 ACPI_STATUS AcpiOsWaitSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units, UINT16 Timeout)
376 {
377         if( !Handle )
378                 return AE_BAD_PARAMETER;
379
380         // Special case
381         if( Timeout == 0 )
382         {
383                 // NOTE: Possible race condition
384                 if( Semaphore_GetValue(Handle) >= Units ) {
385                         Semaphore_Wait(Handle, Units);
386                         return AE_OK;
387                 }
388                 return AE_TIME;
389         }
390
391         tTime   start = now();
392         UINT32  rem = Units;
393         while(rem && now() - start < Timeout)
394         {
395                 rem -= Semaphore_Wait(Handle, rem);
396         }
397
398         if( rem ) {
399                 Semaphore_Signal(Handle, Units - rem);
400                 return AE_TIME;
401         }
402
403         return AE_OK;
404 }
405
406 ACPI_STATUS AcpiOsSignalSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units)
407 {
408         if( !Handle )
409                 return AE_BAD_PARAMETER;
410
411         // TODO: Support AE_LIMIT detection early (to avoid blocks)
412
413         // NOTE: Blocks
414         int rv = Semaphore_Signal(Handle, Units);
415         if( rv != Units )
416                 return AE_LIMIT;
417         
418         return AE_OK;
419 }
420
421 ACPI_STATUS AcpiOsCreateLock(ACPI_SPINLOCK *OutHandle)
422 {
423         LOG("()");
424         if( !OutHandle )
425                 return AE_BAD_PARAMETER;
426         tShortSpinlock  *lock = calloc(sizeof(tShortSpinlock), 1);
427         if( !lock )
428                 return AE_NO_MEMORY;
429         
430         *OutHandle = lock;
431         return AE_OK;
432 }
433
434 void AcpiOsDeleteLock(ACPI_SPINLOCK Handle)
435 {
436         free(Handle);
437 }
438
439 ACPI_CPU_FLAGS AcpiOsAcquireLock(ACPI_SPINLOCK Handle)
440 {
441         SHORTLOCK(Handle);
442         return 0;
443 }
444
445 void AcpiOsReleaseLock(ACPI_SPINLOCK Handle, ACPI_CPU_FLAGS Flags)
446 {
447         SHORTREL(Handle);
448 }
449
450 // --- Interrupt handling ---
451 #define N_INT_LEVELS    16
452 ACPI_OSD_HANDLER        gaACPI_InterruptHandlers[N_INT_LEVELS];
453 void    *gaACPI_InterruptData[N_INT_LEVELS];
454  int    gaACPI_InterruptHandles[N_INT_LEVELS];
455
456 void ACPI_int_InterruptProxy(int IRQ, void *data)
457 {
458         if( !gaACPI_InterruptHandlers[IRQ] )
459                 return ;
460         gaACPI_InterruptHandlers[IRQ](gaACPI_InterruptData[IRQ]);
461 }
462
463 ACPI_STATUS AcpiOsInstallInterruptHandler(UINT32 InterruptLevel, ACPI_OSD_HANDLER Handler, void *Context)
464 {
465         if( InterruptLevel >= N_INT_LEVELS || Handler == NULL )
466                 return AE_BAD_PARAMETER;
467         if( gaACPI_InterruptHandlers[InterruptLevel] )
468                 return AE_ALREADY_EXISTS;
469
470         gaACPI_InterruptHandlers[InterruptLevel] = Handler;
471         gaACPI_InterruptData[InterruptLevel] = Context;
472
473         gaACPI_InterruptHandles[InterruptLevel] = IRQ_AddHandler(InterruptLevel, ACPI_int_InterruptProxy, NULL);
474         return AE_OK;
475 }
476
477 ACPI_STATUS AcpiOsRemoveInterruptHandler(UINT32 InterruptLevel, ACPI_OSD_HANDLER Handler)
478 {
479         if( InterruptLevel >= N_INT_LEVELS || Handler == NULL )
480                 return AE_BAD_PARAMETER;
481         if( gaACPI_InterruptHandlers[InterruptLevel] != Handler )
482                 return AE_NOT_EXIST;
483         gaACPI_InterruptHandlers[InterruptLevel] = NULL;
484         IRQ_RemHandler(gaACPI_InterruptHandles[InterruptLevel]);
485         return AE_OK;
486 }
487
488 // --- Memory Access ---
489 ACPI_STATUS AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 *Value, UINT32 Width)
490 {
491         void *ptr;
492         if( Address < ONEMEG ) {
493                 ptr = (void*)(KERNEL_BASE | Address);
494         }
495         else {
496                 ptr = (char*)MM_MapTemp(Address) + (Address & 0xFFF);
497         }
498
499         switch(Width)
500         {
501         case 8:         *Value = *(Uint8 *)ptr; break;
502         case 16:        *Value = *(Uint16*)ptr; break;
503         case 32:        *Value = *(Uint32*)ptr; break;
504         case 64:        *Value = *(Uint64*)ptr; break;
505         }
506
507         if( Address >= ONEMEG ) {
508                 MM_FreeTemp(ptr);
509         }
510
511         return AE_OK;
512 }
513
514 ACPI_STATUS AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 Value, UINT32 Width)
515 {
516         void *ptr;
517         if( Address < ONEMEG ) {
518                 ptr = (void*)(KERNEL_BASE | Address);
519         }
520         else {
521                 ptr = (char*)MM_MapTemp(Address) + (Address & 0xFFF);
522         }
523
524         switch(Width)
525         {
526         case 8:         *(Uint8 *)ptr = Value;  break;
527         case 16:        *(Uint16*)ptr = Value;  break;
528         case 32:        *(Uint32*)ptr = Value;  break;
529         case 64:        *(Uint64*)ptr = Value;  break;
530         default:
531                 return AE_BAD_PARAMETER;
532         }
533
534         if( Address >= 1024*1024 ) {
535                 MM_FreeTemp(ptr);
536         }
537         
538         return AE_OK;
539 }
540
541 // --- Port Input / Output ---
542 ACPI_STATUS AcpiOsReadPort(ACPI_IO_ADDRESS Address, UINT32 *Value, UINT32 Width)
543 {
544         switch(Width)
545         {
546         case 8:         *Value = inb(Address);  break;
547         case 16:        *Value = inw(Address);  break;
548         case 32:        *Value = ind(Address);  break;
549         default:
550                 return AE_BAD_PARAMETER;
551         }
552         return AE_OK;
553 }
554
555 ACPI_STATUS AcpiOsWritePort(ACPI_IO_ADDRESS Address, UINT32 Value, UINT32 Width)
556 {
557         switch(Width)
558         {
559         case 8:         outb(Address, Value);   break;
560         case 16:        outw(Address, Value);   break;
561         case 32:        outd(Address, Value);   break;
562         default:
563                 return AE_BAD_PARAMETER;
564         }
565         return AE_OK;
566 }
567
568 // --- PCI Configuration Space Access ---
569 ACPI_STATUS AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 *Value, UINT32 Width)
570 {
571         UNIMPLEMENTED();
572         return AE_NOT_IMPLEMENTED;
573 }
574
575 ACPI_STATUS AcpiOsWritePciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 Value, UINT32 Width)
576 {
577         UNIMPLEMENTED();
578         return AE_NOT_IMPLEMENTED;
579 }
580
581 // --- Formatted Output ---
582 void AcpiOsPrintf(const char *Format, ...)
583 {
584         va_list args;
585         va_start(args, Format);
586
587         LogFV(Format, args);
588
589         va_end(args);
590 }
591
592 void AcpiOsVprintf(const char *Format, va_list Args)
593 {
594         LogFV(Format, Args);
595 }
596
597 void AcpiOsRedirectOutput(void *Destination)
598 {
599         // TODO: Do I even need to impliment this?
600 }
601
602 // --- Miscellaneous ---
603 UINT64 AcpiOsGetTimer(void)
604 {
605         return now() * 10 * 1000;
606 }
607
608 ACPI_STATUS AcpiOsSignal(UINT32 Function, void *Info)
609 {
610         switch(Function)
611         {
612         case ACPI_SIGNAL_FATAL: {
613                 ACPI_SIGNAL_FATAL_INFO  *finfo = Info;
614                 Log_Error("ACPI AML", "Fatal %x %x %x", finfo->Type, finfo->Code, finfo->Argument);
615                 break; }
616         case ACPI_SIGNAL_BREAKPOINT: {
617                 Log_Notice("ACPI AML", "Breakpoint %s", Info);
618                 break; };
619         }
620         return AE_OK;
621 }
622
623 ACPI_STATUS AcpiOsGetLine(char *Buffer, UINT32 BufferLength, UINT32 *BytesRead)
624 {
625         UNIMPLEMENTED();
626         return AE_NOT_IMPLEMENTED;
627 }
628

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