acpi.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. "use strict";
  2. // http://www.uefi.org/sites/default/files/resources/ACPI_6_1.pdf
  3. /** @const */
  4. var PMTIMER_FREQ_SECONDS = 3579545;
  5. /**
  6. * @constructor
  7. * @param {CPU} cpu
  8. */
  9. function ACPI(cpu)
  10. {
  11. /** @type {CPU} */
  12. this.cpu = cpu;
  13. var io = cpu.io;
  14. var acpi = {
  15. pci_id: 0x07 << 3,
  16. pci_space: [
  17. 0x86, 0x80, 0x13, 0x71, 0x07, 0x00, 0x80, 0x02, 0x08, 0x00, 0x80, 0x06, 0x00, 0x00, 0x80, 0x00,
  18. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  20. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x01, 0x00, 0x00,
  21. ],
  22. pci_bars: [],
  23. name: "acpi",
  24. };
  25. // 00:07.0 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)
  26. cpu.devices.pci.register_device(acpi);
  27. this.timer_last_value = 0;
  28. this.timer_imprecision_offset = 0;
  29. this.status = 1;
  30. this.pm1_status = 0;
  31. this.pm1_enable = 0;
  32. this.last_timer = this.get_timer(v86.microtick());
  33. this.gpe = new Uint8Array(4);
  34. io.register_read(0xB000, this, undefined, function()
  35. {
  36. dbg_log("ACPI pm1_status read", LOG_ACPI);
  37. return this.pm1_status;
  38. });
  39. io.register_write(0xB000, this, undefined, function(value)
  40. {
  41. dbg_log("ACPI pm1_status write: " + h(value, 4), LOG_ACPI);
  42. this.pm1_status &= ~value;
  43. });
  44. io.register_read(0xB002, this, undefined, function()
  45. {
  46. dbg_log("ACPI pm1_enable read", LOG_ACPI);
  47. return this.pm1_enable;
  48. });
  49. io.register_write(0xB002, this, undefined, function(value)
  50. {
  51. dbg_log("ACPI pm1_enable write: " + h(value), LOG_ACPI);
  52. this.pm1_enable = value;
  53. });
  54. // ACPI status
  55. io.register_read(0xB004, this, undefined, function()
  56. {
  57. dbg_log("ACPI status read", LOG_ACPI);
  58. return this.status;
  59. });
  60. io.register_write(0xB004, this, undefined, function(value)
  61. {
  62. dbg_log("ACPI status write: " + h(value), LOG_ACPI);
  63. this.status = value;
  64. });
  65. // ACPI, pmtimer
  66. io.register_read(0xB008, this, undefined, undefined, function()
  67. {
  68. var value = this.get_timer(v86.microtick()) & 0xFFFFFF;
  69. //dbg_log("pmtimer read: " + h(value >>> 0), LOG_ACPI);
  70. return value;
  71. });
  72. // ACPI, gpe
  73. io.register_read(0xAFE0, this, function()
  74. {
  75. dbg_log("Read gpe#0", LOG_ACPI);
  76. return this.gpe[0];
  77. });
  78. io.register_read(0xAFE1, this, function()
  79. {
  80. dbg_log("Read gpe#1", LOG_ACPI);
  81. return this.gpe[1];
  82. });
  83. io.register_read(0xAFE2, this, function()
  84. {
  85. dbg_log("Read gpe#2", LOG_ACPI);
  86. return this.gpe[2];
  87. });
  88. io.register_read(0xAFE3, this, function()
  89. {
  90. dbg_log("Read gpe#3", LOG_ACPI);
  91. return this.gpe[3];
  92. });
  93. io.register_write(0xAFE0, this, function(value)
  94. {
  95. dbg_log("Write gpe#0: " + h(value), LOG_ACPI);
  96. this.gpe[0] = value;
  97. });
  98. io.register_write(0xAFE1, this, function(value)
  99. {
  100. dbg_log("Write gpe#1: " + h(value), LOG_ACPI);
  101. this.gpe[1] = value;
  102. });
  103. io.register_write(0xAFE2, this, function(value)
  104. {
  105. dbg_log("Write gpe#2: " + h(value), LOG_ACPI);
  106. this.gpe[2] = value;
  107. });
  108. io.register_write(0xAFE3, this, function(value)
  109. {
  110. dbg_log("Write gpe#3: " + h(value), LOG_ACPI);
  111. this.gpe[3] = value;
  112. });
  113. }
  114. ACPI.prototype.timer = function(now)
  115. {
  116. var timer = this.get_timer(now);
  117. var highest_bit_changed = ((timer ^ this.last_timer) & (1 << 23)) !== 0;
  118. if((this.pm1_enable & 1) && highest_bit_changed)
  119. {
  120. dbg_log("ACPI raise irq", LOG_ACPI);
  121. this.pm1_status |= 1;
  122. this.cpu.device_raise_irq(9);
  123. }
  124. else
  125. {
  126. this.cpu.device_lower_irq(9);
  127. }
  128. this.last_timer = timer;
  129. return 100; // TODO
  130. };
  131. ACPI.prototype.get_timer = function(now)
  132. {
  133. const t = Math.round(now * (PMTIMER_FREQ_SECONDS / 1000));
  134. // Due to the low precision of JavaScript's time functions we increment the
  135. // returned timer value every time it is read
  136. if(t === this.timer_last_value)
  137. {
  138. // don't go past 1ms
  139. if(this.timer_imprecision_offset < PMTIMER_FREQ_SECONDS / 1000)
  140. {
  141. this.timer_imprecision_offset++;
  142. }
  143. }
  144. else
  145. {
  146. dbg_assert(t > this.timer_last_value);
  147. const previous_timer = this.timer_last_value + this.timer_imprecision_offset;
  148. // don't go back in time
  149. if(previous_timer <= t)
  150. {
  151. this.timer_imprecision_offset = 0;
  152. this.timer_last_value = t;
  153. }
  154. else
  155. {
  156. dbg_log("Warning: Overshot pmtimer, waiting;" +
  157. " current=" + t +
  158. " last=" + this.timer_last_value +
  159. " offset=" + this.timer_imprecision_offset, LOG_ACPI);
  160. }
  161. }
  162. return this.timer_last_value + this.timer_imprecision_offset;
  163. };
  164. ACPI.prototype.get_state = function()
  165. {
  166. var state = [];
  167. state[0] = this.status;
  168. state[1] = this.pm1_status;
  169. state[2] = this.pm1_enable;
  170. state[3] = this.gpe;
  171. return state;
  172. };
  173. ACPI.prototype.set_state = function(state)
  174. {
  175. this.status = state[0];
  176. this.pm1_status = state[1];
  177. this.pm1_enable = state[2];
  178. this.gpe = state[3];
  179. };