crs.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <acpi.h>
  10. #define CHECK_STATUS(fmt, ...) do { if (ACPI_FAILURE(status)) { \
  11. printf("ACPI failed (%d): " fmt "\n", status, ## __VA_ARGS__); \
  12. goto failed; \
  13. } } while(0)
  14. extern void *ACPIRootPointer;
  15. extern int ACPITableSize;
  16. extern UINT32 AcpiDbgLevel;
  17. void hexdump(void *v, int length)
  18. {
  19. int i;
  20. uint8_t *m = v;
  21. uintptr_t memory = (uintptr_t) v;
  22. int all_zero = 0;
  23. print("hexdump: %p, %u\n", v, length);
  24. for (i = 0; i < length; i += 16) {
  25. int j;
  26. all_zero++;
  27. for (j = 0; (j < 16) && (i + j < length); j++) {
  28. if (m[i + j] != 0) {
  29. all_zero = 0;
  30. break;
  31. }
  32. }
  33. if (all_zero < 2) {
  34. print("%p:", (void *)(memory + i));
  35. for (j = 0; j < 16; j++)
  36. print(" %02x", m[i + j]);
  37. print(" ");
  38. for (j = 0; j < 16; j++)
  39. print("%c", isprint(m[i + j]) ? m[i + j] : '.');
  40. print("\n");
  41. } else if (all_zero == 2) {
  42. print("...\n");
  43. }
  44. }
  45. }
  46. /* these go somewhere else, someday. */
  47. ACPI_STATUS FindIOAPICs(int *pic_mode);
  48. static void DumpResource(ACPI_RESOURCE* resource)
  49. {
  50. while (resource->Type != ACPI_RESOURCE_TYPE_END_TAG) {
  51. printf("Got resource type %d\n", resource->Type);
  52. ACPI_RESOURCE_ADDRESS64 addr64;
  53. ACPI_STATUS status = AcpiResourceToAddress64(resource, &addr64);
  54. printf("Processed and got type 0x%x\n", addr64.ResourceType);
  55. if (status == AE_OK && addr64.ResourceType == ACPI_BUS_NUMBER_RANGE)
  56. {
  57. printf("RouteIRQ: Root bridge bus range %#x..%#x\n",
  58. addr64.Address.Minimum,
  59. addr64.Address.Maximum);
  60. break;
  61. }
  62. resource = ACPI_NEXT_RESOURCE(resource);
  63. }
  64. }
  65. static ACPI_STATUS PrintAcpiDevice(ACPI_HANDLE Device)
  66. {
  67. ACPI_BUFFER buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  68. ACPI_DEVICE_INFO* info = NULL;
  69. ACPI_STATUS status = AcpiGetObjectInfo(Device, &info);
  70. ACPI_STATUS crs;
  71. if (!ACPI_SUCCESS(status))
  72. return AE_OK;
  73. //ResetBuffer(&buffer);
  74. status = AcpiGetCurrentResources(Device, &buffer);
  75. if (!ACPI_SUCCESS(status))
  76. return AE_OK;
  77. //ACPI_FREE(info);
  78. //return_ACPI_STATUS(status);
  79. printf("Device %p type %#x\n", Device, info->Type);
  80. DumpResource((ACPI_RESOURCE*)buffer.Pointer);
  81. AcpiRsDumpResourceList((ACPI_RESOURCE*)buffer.Pointer);
  82. return AE_OK;
  83. }
  84. static ACPI_STATUS PrintDeviceCallback(ACPI_HANDLE Device, UINT32 Depth, void *Context, void** ReturnValue)
  85. {
  86. return PrintAcpiDevice(Device);
  87. }
  88. // PNP0C0F = PCI Interrupt Link Device
  89. // PNP0A03 = PCI Root Bridge
  90. static ACPI_STATUS PrintCRS(void) {
  91. ACPI_STATUS status = AE_OK;
  92. printf("Searching for PNP0A03\n");
  93. status = AcpiGetDevices(NULL/*"PNP0A03"*/, PrintDeviceCallback, NULL, NULL);
  94. #if 0
  95. CHECK_STATUS("AcpiGetDevices PNP0A03");
  96. if (DBGFLG) printf("Searching for PNP0C0F\n");
  97. status = AcpiGetDevices("PNP0C0F", PrintDeviceCallback, NULL, NULL);
  98. CHECK_STATUS("AcpiGetDevices PNP0C0F");
  99. #endif
  100. return status;
  101. }
  102. void
  103. main(int argc, char *argv[])
  104. {
  105. int set = -1, enable = -1;
  106. int seg = 0, bus = 0, dev = 2, fn = 0, pin = 0;
  107. ACPI_STATUS status;
  108. int verbose = 0;
  109. AcpiDbgLevel = ACPI_LV_VERBOSE | ACPI_LV_RESOURCES | ACPI_LV_VERBOSITY2;
  110. AcpiDbgLayer = ACPI_RESOURCES | ACPI_EXECUTER;
  111. if (0) AcpiDbgLayer |= ACPI_ALL_COMPONENTS;
  112. ARGBEGIN{
  113. case 'v':
  114. AcpiDbgLevel |= ACPI_LV_VERBOSITY1 | ACPI_LV_FUNCTIONS;
  115. verbose++;
  116. break;
  117. case 's':
  118. set = open("#P/irqmap", OWRITE);
  119. if (set < 0)
  120. sysfatal("%r");
  121. break;
  122. default:
  123. sysfatal("usage: acpi/irq [-v] [-s]");
  124. break;
  125. }ARGEND;
  126. status = AcpiInitializeSubsystem();
  127. if (ACPI_FAILURE(status)) {
  128. sysfatal("Error %d\n", status);
  129. }
  130. status = AcpiInitializeTables(NULL, 0, FALSE);
  131. if (ACPI_FAILURE(status))
  132. sysfatal("can't set up acpi tables: %d", status);
  133. if (verbose)
  134. print("init dables\n");
  135. status = AcpiLoadTables();
  136. if (ACPI_FAILURE(status))
  137. sysfatal("Can't load ACPI tables: %d", status);
  138. /* from acpi: */
  139. /* If the Hardware Reduced flag is set, machine is always in acpi mode */
  140. AcpiGbl_ReducedHardware = 1;
  141. if (verbose)
  142. print("LOADED TABLES.\n");
  143. status = AcpiEnableSubsystem(0);
  144. if (ACPI_FAILURE(status))
  145. print("Probably does not matter: Can't enable ACPI subsystem");
  146. if (verbose)
  147. print("enabled subsystem.\n");
  148. status = AcpiInitializeObjects(0);
  149. if (ACPI_FAILURE(status))
  150. sysfatal("Can't Initialize ACPI objects");
  151. int picmode;
  152. status = FindIOAPICs(&picmode);
  153. if (picmode == 0)
  154. sysfatal("PANIC: Can't handle picmode 0!");
  155. ACPI_STATUS ExecuteOSI(int pic_mode);
  156. if (verbose)
  157. print("FindIOAPICs returns status %d picmode %d\n", status, picmode);
  158. status = ExecuteOSI(picmode);
  159. CHECK_STATUS("ExecuteOSI");
  160. failed:
  161. if (verbose)
  162. print("inited objects.\n");
  163. if (verbose)
  164. AcpiDbgLevel |= ACPI_LV_VERBOSITY1 | ACPI_LV_FUNCTIONS;
  165. status = AcpiInitializeDebugger();
  166. if (ACPI_FAILURE(status)) {
  167. sysfatal("Error %d\n", status);
  168. }
  169. int GetPRT();
  170. status = GetPRT();
  171. if (ACPI_FAILURE(status)) {
  172. sysfatal("Error %d\n", status);
  173. }
  174. printf("Searching for PNP0A03\n");
  175. status = PrintCRS(); //AcpiGetDevices(NULL/*"PNP0A03"*/, CRSCallBack, NULL, NULL);
  176. CHECK_STATUS("AcpiGetDevices PNP0A03");
  177. exits(0);
  178. }