irq.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. extern ACPI_STATUS RouteIRQ(ACPI_PCI_ID* device, UINT64 pin, int* irq);
  18. void hexdump(void *v, int length)
  19. {
  20. int i;
  21. uint8_t *m = v;
  22. uintptr_t memory = (uintptr_t) v;
  23. int all_zero = 0;
  24. print("hexdump: %p, %u\n", v, length);
  25. for (i = 0; i < length; i += 16) {
  26. int j;
  27. all_zero++;
  28. for (j = 0; (j < 16) && (i + j < length); j++) {
  29. if (m[i + j] != 0) {
  30. all_zero = 0;
  31. break;
  32. }
  33. }
  34. if (all_zero < 2) {
  35. print("%p:", (void *)(memory + i));
  36. for (j = 0; j < 16; j++)
  37. print(" %02x", m[i + j]);
  38. print(" ");
  39. for (j = 0; j < 16; j++)
  40. print("%c", isprint(m[i + j]) ? m[i + j] : '.');
  41. print("\n");
  42. } else if (all_zero == 2) {
  43. print("...\n");
  44. }
  45. }
  46. }
  47. /* these go somewhere else, someday. */
  48. ACPI_STATUS FindIOAPICs(int *pic_mode);
  49. void
  50. main(int argc, char *argv[])
  51. {
  52. int set = -1, enable = -1;
  53. int seg = 0, bus = 0, dev = 2, fn = 0;
  54. ACPI_STATUS status;
  55. int verbose = 0;
  56. AcpiDbgLevel = 0;
  57. ARGBEGIN{
  58. case 'v':
  59. AcpiDbgLevel = ACPI_LV_VERBOSITY1;
  60. verbose++;
  61. break;
  62. case 's':
  63. set = open("#P/irqmap", OWRITE);
  64. if (set < 0)
  65. sysfatal("%r");
  66. break;
  67. default:
  68. sysfatal("usage: acpi/irq [-v] [-s]");
  69. break;
  70. }ARGEND;
  71. status = AcpiInitializeSubsystem();
  72. if (ACPI_FAILURE(status)) {
  73. sysfatal("Error %d\n", status);
  74. }
  75. status = AcpiInitializeTables(NULL, 0, FALSE);
  76. if (ACPI_FAILURE(status))
  77. sysfatal("can't set up acpi tables: %d", status);
  78. if (verbose)
  79. print("init dables\n");
  80. status = AcpiLoadTables();
  81. if (ACPI_FAILURE(status))
  82. sysfatal("Can't load ACPI tables: %d", status);
  83. /* from acpi: */
  84. /* If the Hardware Reduced flag is set, machine is always in acpi mode */
  85. AcpiGbl_ReducedHardware = 1;
  86. if (verbose)
  87. print("LOADED TABLES.\n");
  88. status = AcpiEnableSubsystem(0);
  89. if (ACPI_FAILURE(status))
  90. print("Probably does not matter: Can't enable ACPI subsystem");
  91. if (verbose)
  92. print("enabled subsystem.\n");
  93. status = AcpiInitializeObjects(0);
  94. if (ACPI_FAILURE(status))
  95. sysfatal("Can't Initialize ACPI objects");
  96. int picmode;
  97. status = FindIOAPICs(&picmode);
  98. if (picmode == 0)
  99. sysfatal("PANIC: Can't handle picmode 0!");
  100. ACPI_STATUS ExecuteOSI(int pic_mode);
  101. if (verbose)
  102. print("FindIOAPICs returns status %d picmode %d\n", status, picmode);
  103. status = ExecuteOSI(picmode);
  104. CHECK_STATUS("ExecuteOSI");
  105. failed:
  106. if (verbose)
  107. print("inited objects.\n");
  108. if (verbose)
  109. AcpiDbgLevel |= ACPI_LV_VERBOSITY1 | ACPI_LV_FUNCTIONS;
  110. status = AcpiInitializeDebugger();
  111. if (ACPI_FAILURE(status)) {
  112. sysfatal("Error %d\n", status);
  113. }
  114. int GetPRT();
  115. status = GetPRT();
  116. if (ACPI_FAILURE(status)) {
  117. sysfatal("Error %d\n", status);
  118. }
  119. UINT64 pin = 0;
  120. while (1) {
  121. if (scanf("%x %x", &bus, &dev) < 0)
  122. break;
  123. AcpiDbgLevel = 0;
  124. ACPI_PCI_ID id = (ACPI_PCI_ID){seg, bus, dev, fn};
  125. status = AcpiOsReadPciConfiguration (&id, 0x3d, &pin, 8);
  126. if (!ACPI_SUCCESS(status)){
  127. printf("Can't read pin for bus %d dev %d\n", bus, dev);
  128. continue;
  129. }
  130. print("ROUTE {%d, %d, %d, %d}, pin %llu\n", seg, bus, dev, fn, pin);
  131. int irq;
  132. status = RouteIRQ(&id, pin, &irq);
  133. print("status %d, irq %d\n", status, irq);
  134. AcpiDbgLevel = 0;
  135. print("echo %d %d %d %d 0x%x > /dev/irqmap", seg, bus, dev, fn, irq);
  136. //ACPI_STATUS PrintDevices(void);
  137. //status = PrintDevices();
  138. if (set > -1)
  139. fprint(set, "%d %d %d %d 0x%x", seg, bus, dev, fn, irq);
  140. }
  141. /* we're done. Enable the IRQs we might have set up. */
  142. enable = open("/dev/irqenable", OWRITE);
  143. if (enable < 0)
  144. sysfatal("/dev/irqenable: %r");
  145. if (write(enable, "", 1) < 1)
  146. sysfatal("Write irqenable: %r");
  147. exits(0);
  148. }