test_6400_v1_control_integration.py 13 KB

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