test_6400_v1_control_integration.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. """Production-profile full-top integration over real Host and BMC UART bits."""
  2. from __future__ import annotations
  3. import cocotb
  4. from cocotb.triggers import ReadOnly, RisingEdge, Timer, with_timeout
  5. from thermal_6400_uart import (
  6. BMC_CLKS_PER_BIT,
  7. CLOCK_NS,
  8. GET_SNAPSHOT,
  9. GET_SNAPSHOT_ACK,
  10. HOST_CLKS_PER_BIT,
  11. SET_STIMULUS,
  12. SET_TARGET,
  13. SET_TARGET_ACK,
  14. SNAPSHOT_DATA,
  15. bring_auto,
  16. check_pwm_timing,
  17. encode_frame,
  18. get_snapshot,
  19. host_transaction,
  20. pulse_tick_1s,
  21. receive_frame,
  22. send_bmc_snapshot,
  23. send_uart,
  24. wait_for,
  25. )
  26. def duties(snapshot: dict[str, object], name: str) -> list[int]:
  27. value = snapshot[name]
  28. assert isinstance(value, list)
  29. return [int(item) for item in value]
  30. @cocotb.test()
  31. async def test_control_dual_host_sensor_safety_and_snapshot(dut):
  32. pwm_timing = cocotb.start_soon(check_pwm_timing(dut))
  33. await bring_auto(dut, verify_boot=True)
  34. # GET is one locked two-frame transaction. Traffic on the alternate Host
  35. # after ACK must not change port_sel, unlock snapshot banks, or commit a
  36. # command before the 0x42 DATA frame's final physical stop bit retires.
  37. get_state = {"ack": False, "done": False}
  38. async def receive_locked_get() -> tuple[int, bytes]:
  39. ack = await receive_frame(dut.host_ttl_tx)
  40. assert ack == (GET_SNAPSHOT_ACK, b"")
  41. get_state["ack"] = True
  42. data = await receive_frame(dut.host_ttl_tx)
  43. get_state["done"] = True
  44. return data
  45. target_before_locked_get = int(dut.current_target_mon.value)
  46. get_task = cocotb.start_soon(receive_locked_get())
  47. await send_uart(dut.host_ttl_rx, encode_frame(GET_SNAPSHOT), HOST_CLKS_PER_BIT)
  48. while not get_state["ack"]:
  49. await RisingEdge(dut.sys_clk)
  50. assert int(dut.host_locked_mon.value) == 1
  51. assert int(dut.host_port_sel_mon.value) == 1
  52. assert int(dut.u_dut.snapshot_locked.value) == 1
  53. alternate = cocotb.start_soon(
  54. send_uart(dut.host_rs232_rx, encode_frame(SET_TARGET, b"\x46"), HOST_CLKS_PER_BIT)
  55. )
  56. while not get_state["done"]:
  57. await RisingEdge(dut.sys_clk)
  58. await ReadOnly()
  59. assert int(dut.host_locked_mon.value) == 1
  60. assert int(dut.host_port_sel_mon.value) == 1
  61. assert int(dut.u_dut.snapshot_locked.value) == 1
  62. data_kind, data_payload = await with_timeout(get_task, 5, "ms")
  63. assert data_kind == SNAPSHOT_DATA and len(data_payload) == 193
  64. # receive_frame completes at the center of the final stop bit; lock must
  65. # still be present until uart_tx byte_done at the end of that stop bit.
  66. assert int(dut.host_locked_mon.value) == 1
  67. assert int(dut.u_dut.snapshot_locked.value) == 1
  68. await alternate
  69. await wait_for(
  70. dut,
  71. lambda: int(dut.host_locked_mon.value) == 0
  72. and int(dut.u_dut.snapshot_locked.value) == 0,
  73. cycles=100,
  74. label="GET final-stop unlock",
  75. )
  76. assert int(dut.current_target_mon.value) == target_before_locked_get
  77. # Production must silently reject the identification-only command.
  78. rejected_payload = bytes([2, *([20] * 32)])
  79. await send_uart(
  80. dut.host_rs232_rx,
  81. encode_frame(SET_STIMULUS, rejected_payload),
  82. HOST_CLKS_PER_BIT,
  83. )
  84. await Timer(HOST_CLKS_PER_BIT * CLOCK_NS * 20, units="ns")
  85. assert int(dut.host_rs232_tx.value) == 1
  86. assert int(dut.host_ttl_tx.value) == 1
  87. assert int(dut.stimulus_active_mon.value) == 0
  88. # Exercise simultaneous Host and BMC receive frontends. ACK is routed
  89. # only to RS-232 while the BMC commits a complete fifth snapshot.
  90. old_control_epoch = int(dut.control_epoch_mon.value)
  91. bmc_task = cocotb.start_soon(send_bmc_snapshot(dut, [61] * 24))
  92. ack_task = cocotb.start_soon(receive_frame(dut.host_rs232_tx))
  93. await send_uart(dut.host_rs232_rx, encode_frame(SET_TARGET, b"\x41"), HOST_CLKS_PER_BIT)
  94. ack_kind, ack_payload = await with_timeout(ack_task, 20, "ms")
  95. assert (ack_kind, ack_payload) == (SET_TARGET_ACK, b"")
  96. assert int(dut.host_ttl_tx.value) == 1
  97. await bmc_task
  98. # A target change deliberately restarts CT/preheat qualification. The
  99. # concurrent BMC frame enters WAIT_BMC_STABLE; three further new epochs
  100. # are required before AUTO resumes.
  101. for _ in range(3):
  102. await send_bmc_snapshot(dut, [61] * 24)
  103. await wait_for(
  104. dut,
  105. lambda: int(dut.control_state_mon.value) == 4
  106. and int(dut.control_epoch_mon.value) > old_control_epoch,
  107. cycles=1000,
  108. label="target/BMC control commit",
  109. )
  110. await Timer(HOST_CLKS_PER_BIT * CLOCK_NS * 2, units="ns")
  111. snapshot = await get_snapshot(dut, "ttl")
  112. assert snapshot["control_state"] == 4
  113. assert snapshot["current_target"] == 65
  114. assert snapshot["host_target_seen"] == 1
  115. assert int(snapshot["bmc_epoch"]) >= 5
  116. assert int(snapshot["ct_epoch"]) >= 1
  117. assert snapshot["bmc_valid_mask"] == (1 << 24) - 1
  118. assert snapshot["ct_valid_mask"] == (1 << 27) - 1
  119. assert duties(snapshot, "bmc_raw") == [0x3D00] * 24
  120. assert duties(snapshot, "ct_raw") == [0x3C00] * 27
  121. ptc = duties(snapshot, "ptc_applied")
  122. assert len(ptc) == 24 and all(0 <= value <= 100 for value in ptc) and any(ptc)
  123. assert duties(snapshot, "front_applied") == [30] * 4
  124. assert duties(snapshot, "rear_applied") == [30] * 4
  125. # A 74 C sample reduces only the matching PTC and raises both fans for
  126. # that lane to the contract's 80% protection duty. The public safety bit
  127. # remains telemetry and must not turn the soft action into a full cutoff.
  128. old_control_epoch = int(dut.control_epoch_mon.value)
  129. await send_bmc_snapshot(dut, [74, *([60] * 23)])
  130. await wait_for(
  131. dut,
  132. lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
  133. cycles=1000,
  134. label="74 C soft-limit commit",
  135. )
  136. soft = await get_snapshot(dut, "rs232")
  137. soft_ptc = duties(soft, "ptc_applied")
  138. assert int(soft["safety_override_mask"]) & 1
  139. assert ptc[0] > 1 and soft_ptc[0] == ptc[0] - 1
  140. assert duties(soft, "front_applied") == [80, 30, 30, 30]
  141. assert duties(soft, "rear_applied") == [80, 30, 30, 30]
  142. # At 76 C the matching PTC is fully off, but the lane is not yet hard
  143. # faulted and therefore remains at 80% rather than 100% airflow.
  144. old_control_epoch = int(dut.control_epoch_mon.value)
  145. await send_bmc_snapshot(dut, [76, *([60] * 23)])
  146. await wait_for(
  147. dut,
  148. lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
  149. cycles=1000,
  150. label="76 C pretrip commit",
  151. )
  152. pretrip = await get_snapshot(dut, "ttl")
  153. assert int(pretrip["safety_override_mask"]) & 1
  154. assert not (int(pretrip["lane_fault_mask"]) & 1)
  155. assert duties(pretrip, "ptc_applied")[0] == 0
  156. assert duties(pretrip, "front_applied") == [80, 30, 30, 30]
  157. assert duties(pretrip, "rear_applied") == [80, 30, 30, 30]
  158. # One invalid DIMM is quarantined without corrupting the other 23 values.
  159. await send_bmc_snapshot(dut, [None, *([60] * 23)])
  160. await wait_for(
  161. dut,
  162. lambda: int(dut.control_state_mon.value) == 5 and bool(int(dut.safety_mask_mon.value) & 1),
  163. cycles=1000,
  164. label="single-channel invalid override",
  165. )
  166. invalid = await get_snapshot(dut, "rs232")
  167. assert int(invalid["bmc_valid_mask"]) == ((1 << 24) - 2)
  168. assert int(invalid["safety_override_mask"]) & 1
  169. assert duties(invalid, "ptc_applied")[0] == 0
  170. # A 77 C sample latches the full L0 hard override and both lane fans.
  171. await send_bmc_snapshot(dut, [77, *([60] * 23)])
  172. await wait_for(
  173. dut,
  174. lambda: bool(int(dut.lane_fault_mask_mon.value) & 1),
  175. cycles=1000,
  176. label="lane hard limit",
  177. )
  178. hard = await get_snapshot(dut, "ttl")
  179. assert int(hard["lane_fault_mask"]) & 1
  180. assert duties(hard, "ptc_applied")[:6] == [0] * 6
  181. assert duties(hard, "front_applied")[0] == 100
  182. assert duties(hard, "rear_applied")[0] == 100
  183. # Repeating a legal target while the lane hard fault is active must ACK
  184. # but cannot reduce its applied airflow or its physical connector request.
  185. old_control_epoch = int(dut.control_epoch_mon.value)
  186. kind, payload = await host_transaction(dut, "rs232", SET_TARGET, b"\x41")
  187. assert (kind, payload) == (SET_TARGET_ACK, b"")
  188. await wait_for(
  189. dut,
  190. lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
  191. cycles=1000,
  192. label="hard-fault repeated target safe commit",
  193. )
  194. hard_after_target = await get_snapshot(dut, "ttl")
  195. assert int(hard_after_target["lane_fault_mask"]) & 1
  196. assert duties(hard_after_target, "front_applied")[0] == 100
  197. assert duties(hard_after_target, "rear_applied")[0] == 100
  198. for _ in range(2):
  199. await wait_for(dut, lambda: int(dut.pwm_boundary_mon.value) == 1,
  200. cycles=600, label="hard-fault physical fan boundary")
  201. await Timer(CLOCK_NS, units="ns")
  202. assert not (int(dut.front_fan_pwm_out.value) & 0x1)
  203. assert int(dut.rear_fan_pwm_out.value) & 0x1
  204. # Requalify acquisition while retaining the hot lane, so the stale test
  205. # below starts from an admitted runtime state rather than startup gating.
  206. for _ in range(4):
  207. await send_bmc_snapshot(dut, [77, *([60] * 23)])
  208. await wait_for(
  209. dut,
  210. lambda: int(dut.control_state_mon.value) == 5,
  211. cycles=1000,
  212. label="hard-fault runtime requalification",
  213. )
  214. # Three explicit age ticks without a new BMC epoch create global stale.
  215. await pulse_tick_1s(dut, 3)
  216. await wait_for(
  217. dut,
  218. lambda: int(dut.global_fault_mon.value) == 1,
  219. cycles=200,
  220. label="global BMC stale",
  221. )
  222. stale = await get_snapshot(dut, "rs232")
  223. assert stale["global_fault"] == 1
  224. assert duties(stale, "ptc_applied") == [0] * 24
  225. assert duties(stale, "front_applied") == [100] * 4
  226. assert duties(stale, "rear_applied") == [100] * 4
  227. # A legal target during global stale also ACKs without weakening the
  228. # already-applied all-fan safety request.
  229. old_control_epoch = int(dut.control_epoch_mon.value)
  230. kind, payload = await host_transaction(dut, "ttl", SET_TARGET, b"\x41")
  231. assert (kind, payload) == (SET_TARGET_ACK, b"")
  232. await wait_for(
  233. dut,
  234. lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
  235. cycles=1000,
  236. label="stale target safe commit",
  237. )
  238. stale_after_target = await get_snapshot(dut, "rs232")
  239. assert stale_after_target["global_fault"] == 1
  240. assert duties(stale_after_target, "front_applied") == [100] * 4
  241. assert duties(stale_after_target, "rear_applied") == [100] * 4
  242. for _ in range(2):
  243. await wait_for(dut, lambda: int(dut.pwm_boundary_mon.value) == 1,
  244. cycles=600, label="stale physical fan boundary")
  245. await Timer(CLOCK_NS, units="ns")
  246. assert int(dut.front_fan_pwm_out.value) == 0x0
  247. assert int(dut.rear_fan_pwm_out.value) == 0xF
  248. # Three complete safe epochs retain the clamp through the release commit;
  249. # the following epoch resumes only the normal slew-limited ramp.
  250. for _ in range(3):
  251. await send_bmc_snapshot(dut, [60] * 24)
  252. await wait_for(
  253. dut,
  254. lambda: int(dut.control_epoch_mon.value) >= 1,
  255. cycles=1000,
  256. label="recovery control pass",
  257. )
  258. await wait_for(
  259. dut,
  260. lambda: int(dut.global_fault_mon.value) == 0
  261. and int(dut.lane_fault_mask_mon.value) == 0
  262. and int(dut.control_state_mon.value) == 4,
  263. cycles=1000,
  264. label="three-epoch recovery",
  265. )
  266. released = await get_snapshot(dut, "ttl")
  267. assert duties(released, "ptc_applied") == [0] * 24
  268. await send_bmc_snapshot(dut, [60] * 24)
  269. await wait_for(
  270. dut,
  271. lambda: int(dut.control_epoch_mon.value) > int(released["control_epoch"]),
  272. cycles=1000,
  273. label="post-recovery limited ramp",
  274. )
  275. ramped = await get_snapshot(dut, "rs232")
  276. assert all(0 <= value <= 5 for value in duties(ramped, "ptc_applied"))
  277. assert any(duties(ramped, "ptc_applied"))
  278. kind, payload = await host_transaction(dut, "ttl", SET_TARGET, b"\x00")
  279. assert (kind, payload) == (SET_TARGET_ACK, b"")
  280. stopped = await get_snapshot(dut, "rs232")
  281. assert stopped["control_state"] == 6
  282. assert duties(stopped, "ptc_applied") == [0] * 24
  283. await pwm_timing