1 /****************************************************************************** 2 * 3 * Module Name: oshaiku - Haiku OSL interfaces 4 * 5 *****************************************************************************/ 6 7 /****************************************************************************** 8 * 9 * 1. Copyright Notice 10 * 11 * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp. 12 * All rights reserved. 13 * 14 * 2. License 15 * 16 * 2.1. This is your license from Intel Corp. under its intellectual property 17 * rights. You may have additional license terms from the party that provided 18 * you this software, covering your right to use that party's intellectual 19 * property rights. 20 * 21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a 22 * copy of the source code appearing in this file ("Covered Code") an 23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the 24 * base code distributed originally by Intel ("Original Intel Code") to copy, 25 * make derivatives, distribute, use and display any portion of the Covered 26 * Code in any form, with the right to sublicense such rights; and 27 * 28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent 29 * license (with the right to sublicense), under only those claims of Intel 30 * patents that are infringed by the Original Intel Code, to make, use, sell, 31 * offer to sell, and import the Covered Code and derivative works thereof 32 * solely to the minimum extent necessary to exercise the above copyright 33 * license, and in no event shall the patent license extend to any additions 34 * to or modifications of the Original Intel Code. No other license or right 35 * is granted directly or by implication, estoppel or otherwise; 36 * 37 * The above copyright and patent license is granted only if the following 38 * conditions are met: 39 * 40 * 3. Conditions 41 * 42 * 3.1. Redistribution of Source with Rights to Further Distribute Source. 43 * Redistribution of source code of any substantial portion of the Covered 44 * Code or modification with rights to further distribute source must include 45 * the above Copyright Notice, the above License, this list of Conditions, 46 * and the following Disclaimer and Export Compliance provision. In addition, 47 * Licensee must cause all Covered Code to which Licensee contributes to 48 * contain a file documenting the changes Licensee made to create that Covered 49 * Code and the date of any change. Licensee must include in that file the 50 * documentation of any changes made by any predecessor Licensee. Licensee 51 * must include a prominent statement that the modification is derived, 52 * directly or indirectly, from Original Intel Code. 53 * 54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source. 55 * Redistribution of source code of any substantial portion of the Covered 56 * Code or modification without rights to further distribute source must 57 * include the following Disclaimer and Export Compliance provision in the 58 * documentation and/or other materials provided with distribution. In 59 * addition, Licensee may not authorize further sublicense of source of any 60 * portion of the Covered Code, and must include terms to the effect that the 61 * license from Licensee to its licensee is limited to the intellectual 62 * property embodied in the software Licensee provides to its licensee, and 63 * not to intellectual property embodied in modifications its licensee may 64 * make. 65 * 66 * 3.3. Redistribution of Executable. Redistribution in executable form of any 67 * substantial portion of the Covered Code or modification must reproduce the 68 * above Copyright Notice, and the following Disclaimer and Export Compliance 69 * provision in the documentation and/or other materials provided with the 70 * distribution. 71 * 72 * 3.4. Intel retains all right, title, and interest in and to the Original 73 * Intel Code. 74 * 75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by 76 * Intel shall be used in advertising or otherwise to promote the sale, use or 77 * other dealings in products derived from or relating to the Covered Code 78 * without prior written authorization from Intel. 79 * 80 * 4. Disclaimer and Export Compliance 81 * 82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED 83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE 84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE, 85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY 86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY 87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A 88 * PARTICULAR PURPOSE. 89 * 90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES 91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR 92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT, 93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY 94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL 95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS 96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY 97 * LIMITED REMEDY. 98 * 99 * 4.3. Licensee shall not export, either directly or indirectly, any of this 100 * software or system incorporating such software without first obtaining any 101 * required license or other approval from the U. S. Department of Commerce or 102 * any other agency or department of the United States Government. In the 103 * event Licensee exports any such software from the United States or 104 * re-exports any such software from a foreign destination, Licensee shall 105 * ensure that the distribution and export/re-export of the software is in 106 * compliance with all laws, regulations, orders, or other restrictions of the 107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor 108 * any of its subsidiaries will export/re-export any technical data, process, 109 * software, or service, directly or indirectly, to any country for which the 110 * United States government or any agency thereof requires an export license, 111 * other governmental approval, or letter of assurance, without first obtaining 112 * such license, approval or letter. 113 * 114 *****************************************************************************/ 115 116 117 #include <stdio.h> 118 #include <sys/cdefs.h> 119 #include <time.h> 120 #include <unistd.h> 121 122 #include <OS.h> 123 124 #ifdef _KERNEL_MODE 125 # include <KernelExport.h> 126 127 # include <dpc.h> 128 # include <PCI.h> 129 130 # include <boot_item.h> 131 # include <kernel.h> 132 # include <vm/vm.h> 133 #endif 134 135 __BEGIN_DECLS 136 #include "acpi.h" 137 #include "accommon.h" 138 #include "amlcode.h" 139 #include "acparser.h" 140 #include "acdebug.h" 141 __END_DECLS 142 143 #include "arch_init.h" 144 145 146 ACPI_MODULE_NAME("Haiku ACPI Module") 147 148 #define _COMPONENT ACPI_OS_SERVICES 149 150 // verbosity level 0 = off, 1 = normal, 2 = all 151 #define DEBUG_OSHAIKU 0 152 153 #if DEBUG_OSHAIKU <= 0 154 // No debugging, do nothing 155 # define DEBUG_FUNCTION() 156 # define DEBUG_FUNCTION_F(x, y...) 157 # define DEBUG_FUNCTION_V() 158 # define DEBUG_FUNCTION_VF(x, y...) 159 #else 160 # define DEBUG_FUNCTION() \ 161 dprintf("acpi[%" B_PRId32 "]: %s\n", find_thread(NULL), __PRETTY_FUNCTION__); 162 # define DEBUG_FUNCTION_F(x, y...) \ 163 dprintf("acpi[%" B_PRId32 "]: %s(" x ")\n", find_thread(NULL), __PRETTY_FUNCTION__, y); 164 # if DEBUG_OSHAIKU == 1 165 // No verbose debugging, do nothing 166 # define DEBUG_FUNCTION_V() 167 # define DEBUG_FUNCTION_VF(x, y...) 168 # else 169 // Full debugging 170 # define DEBUG_FUNCTION_V() \ 171 dprintf("acpi[%" B_PRId32 "]: %s\n", find_thread(NULL), __PRETTY_FUNCTION__); 172 # define DEBUG_FUNCTION_VF(x, y...) \ 173 dprintf("acpi[%" B_PRId32 "]: %s(" x ")\n", find_thread(NULL), __PRETTY_FUNCTION__, y); 174 # endif 175 #endif 176 177 178 #ifdef _KERNEL_MODE 179 extern pci_module_info *gPCIManager; 180 extern dpc_module_info *gDPC; 181 extern void *gDPCHandle; 182 #endif 183 184 extern FILE *AcpiGbl_DebugFile; 185 FILE *AcpiGbl_OutputFile; 186 187 static ACPI_PHYSICAL_ADDRESS sACPIRoot = 0; 188 static void *sInterruptHandlerData[32]; 189 190 191 /****************************************************************************** 192 * 193 * FUNCTION: AcpiOsInitialize, AcpiOsTerminate 194 * 195 * PARAMETERS: None 196 * 197 * RETURN: Status 198 * 199 * DESCRIPTION: Init and terminate. Nothing to do. 200 * 201 *****************************************************************************/ 202 ACPI_STATUS 203 AcpiOsInitialize() 204 { 205 #ifndef _KERNEL_MODE 206 AcpiGbl_OutputFile = stdout; 207 #else 208 AcpiGbl_OutputFile = NULL; 209 #endif 210 DEBUG_FUNCTION(); 211 return AE_OK; 212 } 213 214 215 ACPI_STATUS 216 AcpiOsTerminate() 217 { 218 DEBUG_FUNCTION(); 219 return AE_OK; 220 } 221 222 223 /****************************************************************************** 224 * 225 * FUNCTION: AcpiOsGetRootPointer 226 * 227 * PARAMETERS: None 228 * 229 * RETURN: RSDP physical address 230 * 231 * DESCRIPTION: Gets the root pointer (RSDP) 232 * 233 *****************************************************************************/ 234 ACPI_PHYSICAL_ADDRESS 235 AcpiOsGetRootPointer() 236 { 237 #ifdef _KERNEL_MODE 238 DEBUG_FUNCTION(); 239 if (sACPIRoot == 0) { 240 phys_addr_t* acpiRootPointer = (phys_addr_t*)get_boot_item("ACPI_ROOT_POINTER", NULL); 241 if (acpiRootPointer != NULL) 242 sACPIRoot = *acpiRootPointer; 243 244 if (sACPIRoot == 0) 245 sACPIRoot = arch_init_find_root_pointer(); 246 } 247 return sACPIRoot; 248 #else 249 return AeLocalGetRootPointer(); 250 #endif 251 } 252 253 254 /****************************************************************************** 255 * 256 * FUNCTION: AcpiOsPredefinedOverride 257 * 258 * PARAMETERS: initVal - Initial value of the predefined object 259 * newVal - The new value for the object 260 * 261 * RETURN: Status, pointer to value. Null pointer returned if not 262 * overriding. 263 * 264 * DESCRIPTION: Allow the OS to override predefined names 265 * 266 *****************************************************************************/ 267 ACPI_STATUS 268 AcpiOsPredefinedOverride(const ACPI_PREDEFINED_NAMES *initVal, 269 ACPI_STRING *newVal) 270 { 271 DEBUG_FUNCTION(); 272 if (!initVal || !newVal) 273 return AE_BAD_PARAMETER; 274 275 *newVal = NULL; 276 return AE_OK; 277 } 278 279 280 /****************************************************************************** 281 * 282 * FUNCTION: AcpiOsTableOverride 283 * 284 * PARAMETERS: existingTable - Header of current table (probably firmware) 285 * newTable - Where an entire new table is returned. 286 * 287 * RETURN: Status, pointer to new table. Null pointer returned if no 288 * table is available to override 289 * 290 * DESCRIPTION: Return a different version of a table if one is available 291 * 292 *****************************************************************************/ 293 ACPI_STATUS 294 AcpiOsTableOverride(ACPI_TABLE_HEADER *existingTable, 295 ACPI_TABLE_HEADER **newTable) 296 { 297 DEBUG_FUNCTION(); 298 if (!existingTable || !newTable) 299 return AE_BAD_PARAMETER; 300 301 *newTable = NULL; 302 303 #ifdef ACPI_EXEC_APP 304 AeTableOverride(existingTable, newTable); 305 return AE_OK; 306 #else 307 return AE_NO_ACPI_TABLES; 308 #endif 309 } 310 311 312 /****************************************************************************** 313 * 314 * FUNCTION: AcpiOsPhysicalTableOverride 315 * 316 * PARAMETERS: existingTable - Header of current table (probably firmware) 317 * newAddress - Where new table address is returned 318 * (Physical address) 319 * newTableLength - Where new table length is returned 320 * 321 * RETURN: Status, address/length of new table. Null pointer returned 322 * if no table is available to override. 323 * 324 * DESCRIPTION: Returns AE_SUPPORT, function not used in user space. 325 * 326 *****************************************************************************/ 327 328 ACPI_STATUS 329 AcpiOsPhysicalTableOverride(ACPI_TABLE_HEADER *existingTable, 330 ACPI_PHYSICAL_ADDRESS *newAddress, UINT32 *newTableLength) 331 { 332 DEBUG_FUNCTION(); 333 return (AE_SUPPORT); 334 } 335 336 337 /****************************************************************************** 338 * 339 * FUNCTION: AcpiOsRedirectOutput 340 * 341 * PARAMETERS: destination - An open file handle/pointer 342 * 343 * RETURN: None 344 * 345 * DESCRIPTION: Causes redirect of AcpiOsPrintf and AcpiOsVprintf 346 * 347 *****************************************************************************/ 348 void 349 AcpiOsRedirectOutput(void *destination) 350 { 351 DEBUG_FUNCTION(); 352 AcpiGbl_OutputFile = (FILE*)destination; 353 } 354 355 356 /****************************************************************************** 357 * 358 * FUNCTION: AcpiOsPrintf 359 * 360 * PARAMETERS: fmt, ... Standard printf format 361 * 362 * RETURN: None 363 * 364 * DESCRIPTION: Formatted output 365 * 366 *****************************************************************************/ 367 void ACPI_INTERNAL_VAR_XFACE 368 AcpiOsPrintf(const char *fmt, ...) 369 { 370 va_list args; 371 372 DEBUG_FUNCTION(); 373 va_start(args, fmt); 374 AcpiOsVprintf(fmt, args); 375 va_end(args); 376 } 377 378 379 /****************************************************************************** 380 * 381 * FUNCTION: AcpiOsVprintf 382 * 383 * PARAMETERS: fmt Standard printf format 384 * args Argument list 385 * 386 * RETURN: None 387 * 388 * DESCRIPTION: Formatted output with argument list pointer 389 * 390 *****************************************************************************/ 391 void 392 AcpiOsVprintf(const char *fmt, va_list args) 393 { 394 #ifndef _KERNEL_MODE 395 UINT8 flags; 396 397 flags = AcpiGbl_DbOutputFlags; 398 if (flags & ACPI_DB_REDIRECTABLE_OUTPUT) { 399 // Output is directable to either a file (if open) or the console 400 if (AcpiGbl_DebugFile) { 401 // Output file is open, send the output there 402 vfprintf(AcpiGbl_DebugFile, fmt, args); 403 } else { 404 // No redirection, send output to console (once only!) 405 flags |= ACPI_DB_CONSOLE_OUTPUT; 406 } 407 } 408 409 if (flags & ACPI_DB_CONSOLE_OUTPUT) { 410 vfprintf(AcpiGbl_OutputFile, fmt, args); 411 } 412 #else 413 // Buffer the output until we have a complete line to send to syslog, this avoids added 414 // "KERN:" entries in the middle of the line, and mixing up of the ACPI output with other 415 // messages from other CPUs 416 static char outputBuffer[1024]; 417 418 // Append the new text to the buffer 419 size_t len = strlen(outputBuffer); 420 size_t printed = vsnprintf(outputBuffer + len, 1024 - len, fmt, args); 421 if (printed >= 1024 - len) { 422 // There was no space to fit the printed string in the outputBuffer. Remove what we added 423 // there, fush the buffer, and print the long string directly 424 outputBuffer[len] = '\0'; 425 dprintf("%s\n", outputBuffer); 426 outputBuffer[0] = '\0'; 427 dvprintf(fmt, args); 428 return; 429 } 430 431 // See if we have a complete line 432 char* eol = strchr(outputBuffer + len, '\n'); 433 while (eol != nullptr) { 434 // Print the completed line, then remove it from the buffer 435 *eol = 0; 436 dprintf("%s\n", outputBuffer); 437 memmove(outputBuffer, eol + 1, strlen(eol + 1) + 1); 438 // See if there is another line to print still in the buffer (in case ACPICA would call 439 // this function with a single string containing multiple newlines) 440 eol = strchr(outputBuffer, '\n'); 441 } 442 #endif 443 } 444 445 446 /****************************************************************************** 447 * 448 * FUNCTION: AcpiOsGetLine 449 * 450 * PARAMETERS: fmt Standard printf format 451 * args Argument list 452 * 453 * RETURN: Actual bytes read 454 * 455 * DESCRIPTION: Formatted input with argument list pointer 456 * 457 *****************************************************************************/ 458 UINT32 459 AcpiOsGetLine(char *buffer) 460 { 461 uint32 i = 0; 462 463 #ifndef _KERNEL_MODE 464 uint8 temp; 465 466 for (i = 0; ; i++) { 467 scanf("%1c", &temp); 468 if (!temp || temp == '\n') 469 break; 470 471 buffer[i] = temp; 472 } 473 #endif 474 475 buffer[i] = 0; 476 DEBUG_FUNCTION_F("buffer: \"%s\"; result: %" B_PRIu32, buffer, i); 477 return i; 478 } 479 480 481 /****************************************************************************** 482 * 483 * FUNCTION: AcpiOsMapMemory 484 * 485 * PARAMETERS: where Physical address of memory to be mapped 486 * length How much memory to map 487 * 488 * RETURN: Pointer to mapped memory. Null on error. 489 * 490 * DESCRIPTION: Map physical memory into caller's address space 491 * 492 *****************************************************************************/ 493 void * 494 AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS where, ACPI_SIZE length) 495 { 496 #ifdef _KERNEL_MODE 497 void *there; 498 area_id area = map_physical_memory("acpi_physical_mem_area", 499 (phys_addr_t)where, length, B_ANY_KERNEL_ADDRESS, 500 B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA, &there); 501 502 DEBUG_FUNCTION_F("addr: 0x%08lx; length: %lu; mapped: %p; area: %" B_PRId32, 503 (addr_t)where, (size_t)length, there, area); 504 if (area < 0) { 505 dprintf("ACPI: cannot map memory at 0x%" B_PRIu64 ", length %" 506 B_PRIu64 "\n", (uint64)where, (uint64)length); 507 return NULL; 508 } 509 return there; 510 #else 511 return NULL; 512 #endif 513 514 // return ACPI_TO_POINTER((ACPI_SIZE) where); 515 } 516 517 518 /****************************************************************************** 519 * 520 * FUNCTION: AcpiOsUnmapMemory 521 * 522 * PARAMETERS: where Logical address of memory to be unmapped 523 * length How much memory to unmap 524 * 525 * RETURN: None. 526 * 527 * DESCRIPTION: Delete a previously created mapping. Where and Length must 528 * correspond to a previous mapping exactly. 529 * 530 *****************************************************************************/ 531 void 532 AcpiOsUnmapMemory(void *where, ACPI_SIZE length) 533 { 534 DEBUG_FUNCTION_F("mapped: %p; length: %lu", where, (size_t)length); 535 delete_area(area_for(where)); 536 } 537 538 539 /****************************************************************************** 540 * 541 * FUNCTION: AcpiOsAllocate 542 * 543 * PARAMETERS: size Amount to allocate, in bytes 544 * 545 * RETURN: Pointer to the new allocation. Null on error. 546 * 547 * DESCRIPTION: Allocate memory. Algorithm is dependent on the OS. 548 * 549 *****************************************************************************/ 550 void * 551 AcpiOsAllocate(ACPI_SIZE size) 552 { 553 void *mem = (void *) malloc(size); 554 DEBUG_FUNCTION_VF("result: %p", mem); 555 return mem; 556 } 557 558 559 /****************************************************************************** 560 * 561 * FUNCTION: AcpiOsFree 562 * 563 * PARAMETERS: mem Pointer to previously allocated memory 564 * 565 * RETURN: None. 566 * 567 * DESCRIPTION: Free memory allocated via AcpiOsAllocate 568 * 569 *****************************************************************************/ 570 void 571 AcpiOsFree(void *mem) 572 { 573 DEBUG_FUNCTION_VF("mem: %p", mem); 574 free(mem); 575 } 576 577 578 /****************************************************************************** 579 * 580 * FUNCTION: AcpiOsCreateSemaphore 581 * 582 * PARAMETERS: initialUnits - Units to be assigned to the new semaphore 583 * outHandle - Where a handle will be returned 584 * 585 * RETURN: Status 586 * 587 * DESCRIPTION: Create an OS semaphore 588 * 589 *****************************************************************************/ 590 ACPI_STATUS 591 AcpiOsCreateSemaphore(UINT32 maxUnits, UINT32 initialUnits, 592 ACPI_SEMAPHORE *outHandle) 593 { 594 if (!outHandle) 595 return AE_BAD_PARAMETER; 596 597 *outHandle = create_sem(initialUnits, "acpi_sem"); 598 DEBUG_FUNCTION_F("max: %" B_PRIu32 "; count: %" B_PRIu32 "; result: %" PRId32, 599 (uint32)maxUnits, (uint32)initialUnits, *outHandle); 600 601 if (*outHandle >= B_OK) 602 return AE_OK; 603 604 return *outHandle == B_BAD_VALUE ? AE_BAD_PARAMETER : AE_NO_MEMORY; 605 } 606 607 608 /****************************************************************************** 609 * 610 * FUNCTION: AcpiOsDeleteSemaphore 611 * 612 * PARAMETERS: handle - Handle returned by AcpiOsCreateSemaphore 613 * 614 * RETURN: Status 615 * 616 * DESCRIPTION: Delete an OS semaphore 617 * 618 *****************************************************************************/ 619 ACPI_STATUS 620 AcpiOsDeleteSemaphore(ACPI_SEMAPHORE handle) 621 { 622 DEBUG_FUNCTION_F("sem: %" B_PRId32, handle); 623 return delete_sem(handle) == B_OK ? AE_OK : AE_BAD_PARAMETER; 624 } 625 626 627 /****************************************************************************** 628 * 629 * FUNCTION: AcpiOsWaitSemaphore 630 * 631 * PARAMETERS: handle - Handle returned by AcpiOsCreateSemaphore 632 * units - How many units to wait for 633 * timeout - How long to wait 634 * 635 * RETURN: Status 636 * 637 * DESCRIPTION: Wait for units 638 * 639 *****************************************************************************/ 640 ACPI_STATUS 641 AcpiOsWaitSemaphore(ACPI_SEMAPHORE handle, UINT32 units, UINT16 timeout) 642 { 643 ACPI_STATUS result = AE_OK; 644 DEBUG_FUNCTION_VF("sem: %ld; count: %lu; timeout: %u", 645 handle, (uint32)units, timeout); 646 647 if (timeout == ACPI_WAIT_FOREVER) { 648 result = acquire_sem_etc(handle, units, 0, 0) 649 == B_OK ? AE_OK : AE_BAD_PARAMETER; 650 } else { 651 switch (acquire_sem_etc(handle, units, B_RELATIVE_TIMEOUT, 652 (bigtime_t)timeout * 1000)) { 653 case B_OK: 654 result = AE_OK; 655 break; 656 case B_INTERRUPTED: 657 case B_TIMED_OUT: 658 case B_WOULD_BLOCK: 659 result = AE_TIME; 660 break; 661 case B_BAD_VALUE: 662 default: 663 result = AE_BAD_PARAMETER; 664 break; 665 } 666 } 667 DEBUG_FUNCTION_VF("sem: %ld; count: %lu; timeout: %u result: %lu", 668 handle, (uint32)units, timeout, (uint32)result); 669 return result; 670 } 671 672 673 /****************************************************************************** 674 * 675 * FUNCTION: AcpiOsSignalSemaphore 676 * 677 * PARAMETERS: handle - Handle returned by AcpiOsCreateSemaphore 678 * units - Number of units to send 679 * 680 * RETURN: Status 681 * 682 * DESCRIPTION: Send units 683 * 684 *****************************************************************************/ 685 ACPI_STATUS 686 AcpiOsSignalSemaphore(ACPI_SEMAPHORE handle, UINT32 units) 687 { 688 status_t result; 689 DEBUG_FUNCTION_VF("sem: %ld; count: %lu", handle, (uint32)units); 690 // We can be called from interrupt handler, so don't reschedule 691 result = release_sem_etc(handle, units, B_DO_NOT_RESCHEDULE); 692 return result == B_OK ? AE_OK : AE_BAD_PARAMETER; 693 } 694 695 696 /****************************************************************************** 697 * 698 * FUNCTION: Spinlock interfaces 699 * 700 * DESCRIPTION: Map these interfaces to semaphore interfaces 701 * 702 *****************************************************************************/ 703 ACPI_STATUS 704 AcpiOsCreateLock(ACPI_SPINLOCK *outHandle) 705 { 706 *outHandle = (ACPI_SPINLOCK) malloc(sizeof(spinlock)); 707 DEBUG_FUNCTION_F("result: %p", *outHandle); 708 if (*outHandle == NULL) 709 return AE_NO_MEMORY; 710 711 B_INITIALIZE_SPINLOCK(*outHandle); 712 return AE_OK; 713 } 714 715 716 void 717 AcpiOsDeleteLock(ACPI_SPINLOCK handle) 718 { 719 DEBUG_FUNCTION(); 720 free((void*)handle); 721 } 722 723 724 ACPI_CPU_FLAGS 725 AcpiOsAcquireLock(ACPI_SPINLOCK handle) 726 { 727 cpu_status cpu; 728 DEBUG_FUNCTION_F("spinlock: %p", handle); 729 cpu = disable_interrupts(); 730 acquire_spinlock(handle); 731 return cpu; 732 } 733 734 735 void 736 AcpiOsReleaseLock(ACPI_SPINLOCK handle, ACPI_CPU_FLAGS flags) 737 { 738 release_spinlock(handle); 739 restore_interrupts(flags); 740 DEBUG_FUNCTION_F("spinlock: %p", handle); 741 } 742 743 744 /****************************************************************************** 745 * 746 * FUNCTION: AcpiOsInstallInterruptHandler 747 * 748 * PARAMETERS: interruptNumber Level handler should respond to. 749 * Isr Address of the ACPI interrupt handler 750 * ExceptPtr Where status is returned 751 * 752 * RETURN: Handle to the newly installed handler. 753 * 754 * DESCRIPTION: Install an interrupt handler. Used to install the ACPI 755 * OS-independent handler. 756 * 757 *****************************************************************************/ 758 UINT32 759 AcpiOsInstallInterruptHandler(UINT32 interruptNumber, 760 ACPI_OSD_HANDLER serviceRoutine, void *context) 761 { 762 status_t result; 763 DEBUG_FUNCTION_F("vector: %" B_PRIu32 "; handler: %p context %p", 764 (uint32)interruptNumber, serviceRoutine, context); 765 766 #ifdef _KERNEL_MODE 767 // It so happens that the Haiku and ACPI-CA interrupt handler routines 768 // return the same values with the same meanings 769 sInterruptHandlerData[interruptNumber] = context; 770 result = install_io_interrupt_handler(interruptNumber, 771 (interrupt_handler)serviceRoutine, context, 0); 772 773 DEBUG_FUNCTION_F("vector: %" B_PRIu32 "; handler: %p context %p returned %" B_PRId32, 774 (uint32)interruptNumber, serviceRoutine, context, (uint32)result); 775 776 return result == B_OK ? AE_OK : AE_BAD_PARAMETER; 777 #else 778 return AE_BAD_PARAMETER; 779 #endif 780 } 781 782 783 /****************************************************************************** 784 * 785 * FUNCTION: AcpiOsRemoveInterruptHandler 786 * 787 * PARAMETERS: Handle Returned when handler was installed 788 * 789 * RETURN: Status 790 * 791 * DESCRIPTION: Uninstalls an interrupt handler. 792 * 793 *****************************************************************************/ 794 ACPI_STATUS 795 AcpiOsRemoveInterruptHandler(UINT32 interruptNumber, 796 ACPI_OSD_HANDLER serviceRoutine) 797 { 798 DEBUG_FUNCTION_F("vector: %" B_PRIu32 "; handler: %p", (uint32)interruptNumber, 799 serviceRoutine); 800 #ifdef _KERNEL_MODE 801 return remove_io_interrupt_handler(interruptNumber, 802 (interrupt_handler)serviceRoutine, 803 sInterruptHandlerData[interruptNumber]) == B_OK ? AE_OK : AE_ERROR; 804 #else 805 return AE_ERROR; 806 #endif 807 } 808 809 810 /****************************************************************************** 811 * 812 * FUNCTION: AcpiOsExecute 813 * 814 * PARAMETERS: type - Type of execution 815 * function - Address of the function to execute 816 * context - Passed as a parameter to the function 817 * 818 * RETURN: Status. 819 * 820 * DESCRIPTION: Execute a new thread 821 * 822 *****************************************************************************/ 823 ACPI_STATUS 824 AcpiOsExecute(ACPI_EXECUTE_TYPE type, ACPI_OSD_EXEC_CALLBACK function, 825 void *context) 826 { 827 DEBUG_FUNCTION(); 828 /* TODO: Prioritize urgent? 829 switch (type) { 830 case OSL_GLOBAL_LOCK_HANDLER: 831 case OSL_NOTIFY_HANDLER: 832 case OSL_GPE_HANDLER: 833 case OSL_DEBUGGER_THREAD: 834 case OSL_EC_POLL_HANDLER: 835 case OSL_EC_BURST_HANDLER: 836 break; 837 } 838 */ 839 840 if (gDPC->queue_dpc(gDPCHandle, function, context) != B_OK) { 841 DEBUG_FUNCTION_F("Serious failure in AcpiOsExecute! function: %p", 842 function); 843 return AE_BAD_PARAMETER; 844 } 845 return AE_OK; 846 } 847 848 849 /****************************************************************************** 850 * 851 * FUNCTION: AcpiOsStall 852 * 853 * PARAMETERS: microseconds To sleep 854 * 855 * RETURN: Blocks until sleep is completed. 856 * 857 * DESCRIPTION: Sleep at microsecond granularity 858 * 859 *****************************************************************************/ 860 void 861 AcpiOsStall(UINT32 microseconds) 862 { 863 DEBUG_FUNCTION_F("microseconds: %" B_PRIu32, (uint32)microseconds); 864 if (microseconds) 865 spin(microseconds); 866 } 867 868 869 /****************************************************************************** 870 * 871 * FUNCTION: AcpiOsSleep 872 * 873 * PARAMETERS: milliseconds To sleep 874 * 875 * RETURN: Blocks until sleep is completed. 876 * 877 * DESCRIPTION: Sleep at millisecond granularity 878 * 879 *****************************************************************************/ 880 void 881 AcpiOsSleep(ACPI_INTEGER milliseconds) 882 { 883 DEBUG_FUNCTION_F("milliseconds: %" B_PRIu32, (uint32)milliseconds); 884 if (gKernelStartup) 885 spin(milliseconds * 1000); 886 else 887 snooze(milliseconds * 1000); 888 } 889 890 891 /****************************************************************************** 892 * 893 * FUNCTION: AcpiOsGetTimer 894 * 895 * PARAMETERS: None 896 * 897 * RETURN: Current time in 100 nanosecond units 898 * 899 * DESCRIPTION: Get the current system time 900 * 901 *****************************************************************************/ 902 UINT64 903 AcpiOsGetTimer() 904 { 905 DEBUG_FUNCTION(); 906 return system_time() * 10; 907 } 908 909 910 /****************************************************************************** 911 * 912 * FUNCTION: AcpiOsReadPciConfiguration 913 * 914 * PARAMETERS: pciId Seg/Bus/Dev 915 * reg Device Register 916 * value Buffer where value is placed 917 * width Number of bits 918 * 919 * RETURN: Status 920 * 921 * DESCRIPTION: Read data from PCI configuration space 922 * 923 *****************************************************************************/ 924 ACPI_STATUS 925 AcpiOsReadPciConfiguration(ACPI_PCI_ID *pciId, UINT32 reg, UINT64 *value, 926 UINT32 width) 927 { 928 #ifdef _KERNEL_MODE 929 DEBUG_FUNCTION(); 930 931 switch (width) { 932 case 8: 933 case 16: 934 case 32: 935 *value = gPCIManager->read_pci_config( 936 pciId->Bus, pciId->Device, pciId->Function, reg, width / 8); 937 break; 938 default: 939 return AE_ERROR; 940 } 941 return AE_OK; 942 #else 943 return AE_ERROR; 944 #endif 945 } 946 947 948 /****************************************************************************** 949 * 950 * FUNCTION: AcpiOsWritePciConfiguration 951 * 952 * PARAMETERS: pciId Seg/Bus/Dev 953 * reg Device Register 954 * value Value to be written 955 * width Number of bits 956 * 957 * RETURN: Status. 958 * 959 * DESCRIPTION: Write data to PCI configuration space 960 * 961 *****************************************************************************/ 962 ACPI_STATUS 963 AcpiOsWritePciConfiguration(ACPI_PCI_ID *pciId, UINT32 reg, 964 ACPI_INTEGER value, UINT32 width) 965 { 966 #ifdef _KERNEL_MODE 967 DEBUG_FUNCTION(); 968 gPCIManager->write_pci_config( 969 pciId->Bus, pciId->Device, pciId->Function, reg, width / 8, value); 970 return AE_OK; 971 #else 972 return AE_ERROR; 973 #endif 974 } 975 976 977 /****************************************************************************** 978 * 979 * FUNCTION: AcpiOsReadPort 980 * 981 * PARAMETERS: address Address of I/O port/register to read 982 * Value Where value is placed 983 * width Number of bits 984 * 985 * RETURN: Value read from port 986 * 987 * DESCRIPTION: Read data from an I/O port or register 988 * 989 *****************************************************************************/ 990 ACPI_STATUS 991 AcpiOsReadPort(ACPI_IO_ADDRESS address, UINT32 *value, UINT32 width) 992 { 993 #ifdef _KERNEL_MODE 994 DEBUG_FUNCTION_F("addr: 0x%08lx; width: %" B_PRIu32, (addr_t)address, (uint32)width); 995 switch (width) { 996 case 8: 997 *value = gPCIManager->read_io_8(address); 998 break; 999 1000 case 16: 1001 *value = gPCIManager->read_io_16(address); 1002 break; 1003 1004 case 32: 1005 *value = gPCIManager->read_io_32(address); 1006 break; 1007 1008 default: 1009 return AE_ERROR; 1010 } 1011 1012 return AE_OK; 1013 #else 1014 return AE_ERROR; 1015 #endif 1016 } 1017 1018 1019 /****************************************************************************** 1020 * 1021 * FUNCTION: AcpiOsWritePort 1022 * 1023 * PARAMETERS: address Address of I/O port/register to write 1024 * value Value to write 1025 * width Number of bits 1026 * 1027 * RETURN: None 1028 * 1029 * DESCRIPTION: Write data to an I/O port or register 1030 * 1031 *****************************************************************************/ 1032 ACPI_STATUS 1033 AcpiOsWritePort(ACPI_IO_ADDRESS address, UINT32 value, UINT32 width) 1034 { 1035 #ifdef _KERNEL_MODE 1036 DEBUG_FUNCTION_F("addr: 0x%08lx; value: %" B_PRIu32 "; width: %" B_PRIu32, 1037 (addr_t)address, (uint32)value, (uint32)width); 1038 switch (width) { 1039 case 8: 1040 gPCIManager->write_io_8(address, value); 1041 break; 1042 1043 case 16: 1044 gPCIManager->write_io_16(address,value); 1045 break; 1046 1047 case 32: 1048 gPCIManager->write_io_32(address,value); 1049 break; 1050 1051 default: 1052 return AE_ERROR; 1053 } 1054 1055 return AE_OK; 1056 #else 1057 return AE_ERROR; 1058 #endif 1059 } 1060 1061 1062 /****************************************************************************** 1063 * 1064 * FUNCTION: AcpiOsReadMemory 1065 * 1066 * PARAMETERS: address Physical Memory Address to read 1067 * value Where value is placed 1068 * width Number of bits 1069 * 1070 * RETURN: Value read from physical memory address 1071 * 1072 * DESCRIPTION: Read data from a physical memory address 1073 * 1074 *****************************************************************************/ 1075 ACPI_STATUS 1076 AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS address, UINT64 *value, UINT32 width) 1077 { 1078 #ifdef _KERNEL_MODE 1079 if (vm_memcpy_from_physical(value, (phys_addr_t)address, width / 8, false) 1080 != B_OK) { 1081 return AE_ERROR; 1082 } 1083 return AE_OK; 1084 #else 1085 return AE_ERROR; 1086 #endif 1087 } 1088 1089 1090 /****************************************************************************** 1091 * 1092 * FUNCTION: AcpiOsWriteMemory 1093 * 1094 * PARAMETERS: address Physical Memory Address to write 1095 * value Value to write 1096 * width Number of bits 1097 * 1098 * RETURN: None 1099 * 1100 * DESCRIPTION: Write data to a physical memory address 1101 * 1102 *****************************************************************************/ 1103 ACPI_STATUS 1104 AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS address, UINT64 value, UINT32 width) 1105 { 1106 #ifdef _KERNEL_MODE 1107 if (vm_memcpy_to_physical((phys_addr_t)address, &value, width / 8, false) 1108 != B_OK) { 1109 return AE_ERROR; 1110 } 1111 return AE_OK; 1112 #else 1113 return AE_ERROR; 1114 #endif 1115 } 1116 1117 1118 /****************************************************************************** 1119 * 1120 * FUNCTION: AcpiOsReadable 1121 * 1122 * PARAMETERS: pointer - Area to be verified 1123 * length - Size of area 1124 * 1125 * RETURN: TRUE if readable for entire length 1126 * 1127 * DESCRIPTION: Verify that a pointer is valid for reading 1128 * 1129 *****************************************************************************/ 1130 BOOLEAN 1131 AcpiOsReadable(void *pointer, ACPI_SIZE length) 1132 { 1133 #ifdef _KERNEL_MODE 1134 return true; 1135 #else 1136 area_id id; 1137 area_info info; 1138 1139 DEBUG_FUNCTION_F("addr: %p; length: %lu", pointer, (size_t)length); 1140 1141 id = area_for(pointer); 1142 if (id == B_ERROR) return false; 1143 if (get_area_info(id, &info) != B_OK) return false; 1144 return (info.protection & B_READ_AREA) != 0 && 1145 ((char *)pointer) + length <= info.address + info.ram_size; 1146 #endif 1147 } 1148 1149 1150 /****************************************************************************** 1151 * 1152 * FUNCTION: AcpiOsWritable 1153 * 1154 * PARAMETERS: pointer - Area to be verified 1155 * length - Size of area 1156 * 1157 * RETURN: TRUE if writable for entire length 1158 * 1159 * DESCRIPTION: Verify that a pointer is valid for writing 1160 * 1161 *****************************************************************************/ 1162 BOOLEAN 1163 AcpiOsWritable(void *pointer, ACPI_SIZE length) 1164 { 1165 #ifdef _KERNEL_MODE 1166 return true; 1167 #else 1168 area_id id; 1169 area_info info; 1170 1171 DEBUG_FUNCTION_F("addr: %p; length: %lu", pointer, (size_t)length); 1172 1173 id = area_for(pointer); 1174 if (id == B_ERROR) return false; 1175 if (get_area_info(id, &info) != B_OK) return false; 1176 return (info.protection & B_READ_AREA) != 0 && 1177 (info.protection & B_WRITE_AREA) != 0 && 1178 ((char *)pointer) + length <= info.address + info.ram_size; 1179 #endif 1180 } 1181 1182 1183 /****************************************************************************** 1184 * 1185 * FUNCTION: AcpiOsGetThreadId 1186 * 1187 * PARAMETERS: None 1188 * 1189 * RETURN: Id of the running thread 1190 * 1191 * DESCRIPTION: Get the Id of the current (running) thread 1192 * 1193 * NOTE: The environment header should contain this line: 1194 * #define ACPI_THREAD_ID pthread_t 1195 * 1196 *****************************************************************************/ 1197 ACPI_THREAD_ID 1198 AcpiOsGetThreadId() 1199 { 1200 thread_id thread = find_thread(NULL); 1201 // TODO: We arn't allowed threads with id 0, handle this case. 1202 // ACPI treats a 0 return as an error, 1203 // but we are thread 0 in early boot 1204 return thread; 1205 } 1206 1207 1208 /****************************************************************************** 1209 * 1210 * FUNCTION: AcpiOsSignal 1211 * 1212 * PARAMETERS: function ACPI CA signal function code 1213 * info Pointer to function-dependent structure 1214 * 1215 * RETURN: Status 1216 * 1217 * DESCRIPTION: Miscellaneous functions. Example implementation only. 1218 * 1219 *****************************************************************************/ 1220 ACPI_STATUS 1221 AcpiOsSignal(UINT32 function, void *info) 1222 { 1223 DEBUG_FUNCTION(); 1224 1225 switch (function) { 1226 case ACPI_SIGNAL_FATAL: 1227 #ifdef _KERNEL_MODE 1228 panic("%s", info == NULL ? "AcpiOsSignal: fatal" : (const char*)info); 1229 break; 1230 #endif 1231 case ACPI_SIGNAL_BREAKPOINT: 1232 if (info != NULL) 1233 AcpiOsPrintf("AcpiOsBreakpoint: %s ****\n", (const char*)info); 1234 else 1235 AcpiOsPrintf("At AcpiOsBreakpoint ****\n"); 1236 break; 1237 } 1238 1239 return AE_OK; 1240 } 1241 1242 1243 /* 1244 * Adapted from FreeBSD since the documentation of its intended impl 1245 * is lacking. 1246 * Section 5.2.10.1: global lock acquire/release functions */ 1247 1248 /* 1249 * Adapted from FreeBSD since the documentation of its intended impl 1250 * is lacking. 1251 * Acquire the global lock. If busy, set the pending bit. The caller 1252 * will wait for notification from the BIOS that the lock is available 1253 * and then attempt to acquire it again. 1254 */ 1255 int 1256 AcpiOsAcquireGlobalLock(volatile uint32_t *lock) 1257 { 1258 uint32_t newValue; 1259 uint32_t oldValue; 1260 1261 do { 1262 oldValue = *lock; 1263 newValue = ((oldValue & ~ACPI_GLOCK_PENDING) | ACPI_GLOCK_OWNED); 1264 if ((oldValue & ACPI_GLOCK_OWNED) != 0) 1265 newValue |= ACPI_GLOCK_PENDING; 1266 } while (atomic_test_and_set((int32*)lock, newValue, oldValue) != (int32)oldValue); 1267 1268 return (newValue & ACPI_GLOCK_PENDING) == 0; 1269 } 1270 1271 1272 /* 1273 * Adapted from FreeBSD since the documentation of its intended impl 1274 * is lacking. 1275 * Release the global lock, returning whether there is a waiter pending. 1276 * If the BIOS set the pending bit, OSPM must notify the BIOS when it 1277 * releases the lock. 1278 */ 1279 int 1280 AcpiOsReleaseGlobalLock(volatile uint32_t *lock) 1281 { 1282 uint32 newValue; 1283 uint32 oldValue; 1284 1285 do { 1286 oldValue = *lock; 1287 newValue = oldValue & ~(ACPI_GLOCK_PENDING | ACPI_GLOCK_OWNED); 1288 } while (atomic_test_and_set((int32*)lock, newValue, oldValue) != (int32)oldValue); 1289 1290 return (oldValue & ACPI_GLOCK_PENDING) != 0; 1291 } 1292 1293 1294 ACPI_STATUS 1295 AcpiOsCreateMutex(ACPI_MUTEX* outHandle) 1296 { 1297 *outHandle = (ACPI_MUTEX) malloc(sizeof(mutex)); 1298 DEBUG_FUNCTION_F("result: %p", *outHandle); 1299 if (*outHandle == NULL) 1300 return AE_NO_MEMORY; 1301 1302 mutex_init(*outHandle, "acpi mutex"); 1303 return AE_OK; 1304 } 1305 1306 1307 void 1308 AcpiOsDeleteMutex(ACPI_MUTEX handle) 1309 { 1310 DEBUG_FUNCTION_F("mutex: %ld", (addr_t)handle); 1311 mutex_destroy(handle); 1312 free((void*)handle); 1313 } 1314 1315 1316 ACPI_STATUS 1317 AcpiOsAcquireMutex(ACPI_MUTEX handle, UINT16 timeout) 1318 { 1319 ACPI_STATUS result = AE_OK; 1320 DEBUG_FUNCTION_VF("mutex: %p; timeout: %u", handle, timeout); 1321 1322 if (timeout == ACPI_WAIT_FOREVER) { 1323 result = (mutex_lock(handle) == B_OK) ? AE_OK : AE_BAD_PARAMETER; 1324 } else if (timeout == ACPI_DO_NOT_WAIT) { 1325 result = (mutex_trylock(handle) == B_OK) ? AE_OK : AE_TIME; 1326 } else { 1327 switch (mutex_lock_with_timeout(handle, B_RELATIVE_TIMEOUT, 1328 (bigtime_t)timeout * 1000)) { 1329 case B_OK: 1330 result = AE_OK; 1331 break; 1332 case B_INTERRUPTED: 1333 case B_TIMED_OUT: 1334 case B_WOULD_BLOCK: 1335 result = AE_TIME; 1336 break; 1337 case B_BAD_VALUE: 1338 default: 1339 result = AE_BAD_PARAMETER; 1340 break; 1341 } 1342 } 1343 DEBUG_FUNCTION_VF("mutex: %p; timeout: %u result: %lu", 1344 handle, timeout, (uint32)result); 1345 return result; 1346 } 1347 1348 1349 void 1350 AcpiOsReleaseMutex(ACPI_MUTEX handle) 1351 { 1352 DEBUG_FUNCTION_F("mutex: %p", handle); 1353 mutex_unlock(handle); 1354 } 1355 1356 1357 /****************************************************************************** 1358 * 1359 * FUNCTION: AcpiOsWaitEventsComplete 1360 * 1361 * PARAMETERS: None 1362 * 1363 * RETURN: None 1364 * 1365 * DESCRIPTION: Wait for all asynchronous events to complete. This 1366 * implementation does nothing. 1367 * 1368 *****************************************************************************/ 1369 void 1370 AcpiOsWaitEventsComplete() 1371 { 1372 //TODO: FreeBSD See description. 1373 return; 1374 } 1375 1376 1377 /****************************************************************************** 1378 * 1379 * FUNCTION: AcpiOsEnterSleep 1380 * 1381 * PARAMETERS: SleepState - Which sleep state to enter 1382 * RegaValue - Register A value 1383 * RegbValue - Register B value 1384 * 1385 * RETURN: Status 1386 * 1387 * DESCRIPTION: A hook before writing sleep registers to enter the sleep 1388 * state. Return AE_CTRL_TERMINATE to skip further sleep register 1389 * writes. 1390 * 1391 *****************************************************************************/ 1392 1393 ACPI_STATUS 1394 AcpiOsEnterSleep ( 1395 UINT8 SleepState, 1396 UINT32 RegaValue, 1397 UINT32 RegbValue) 1398 { 1399 return (AE_OK); 1400 } 1401