From 4f2183a65d22ae643fefa0687aff7579ddaf106e Mon Sep 17 00:00:00 2001 From: Ray Lau Date: Fri, 4 Dec 2026 15:18:04 +0000 Subject: [PATCH] [rtl,pre_dv,doc] Fix ignored or error access handling Every register sits in byte 0 and a TL-UL request with a_mask[0] clear should have no effect. This change is needed for Mocha because mem_downsizer can generate requests with a_mask clear, which are intended to be ignored. This commit also fixes d_opcode from request errors. Co-authored-by: Martin Velay Signed-off-by: Ray Lau --- hw/ip/uart16550/doc/registers.md | 2 + hw/ip/uart16550/pre_dv/uart16550_tb.sv | 62 ++++++++++++++++++++-- hw/ip/uart16550/rtl/uart16550_translate.sv | 38 ++++++++++++- 3 files changed, 95 insertions(+), 7 deletions(-) diff --git a/hw/ip/uart16550/doc/registers.md b/hw/ip/uart16550/doc/registers.md index 2b6c6b1..24bb553 100644 --- a/hw/ip/uart16550/doc/registers.md +++ b/hw/ip/uart16550/doc/registers.md @@ -1,6 +1,8 @@ # UART 16550 registers All standard 16550 registers are implemented and accessible at the least significant byte of consecutive 4-byte words. Writes to the more significant 3 bytes are ignored and reads from these 3 bytes return 0. +A write with `a_mask[0]` clear changes no register. +A read with `a_mask[0]` clear returns 0 on the least significant byte and has no other effect. The register map has a size of 32 bytes and wraps every 32 bytes. Accesses with a non-zero `a_address[1:0]` return an error. diff --git a/hw/ip/uart16550/pre_dv/uart16550_tb.sv b/hw/ip/uart16550/pre_dv/uart16550_tb.sv index 0a6e351..d083153 100644 --- a/hw/ip/uart16550/pre_dv/uart16550_tb.sv +++ b/hw/ip/uart16550/pre_dv/uart16550_tb.sv @@ -83,6 +83,8 @@ initial begin uart_req(.is_wr(0), .a(1), .data_in(32'hFFFFEEEE), .exp_data_out(32'h00000000)); // Reg 1 rd uart_req(.is_wr(1), .a(1), .data_in(32'hFE120005), .exp_data_out(32'hFFFFFFFF)); // Reg 1 wr FE120005 uart_req(.is_wr(0), .a(1), .data_in(32'hFFFFEEEE), .exp_data_out(32'h00000005)); // Reg 1 rd + uart_req(.is_wr(1), .byte_en(0), .a(1), .data_in(32'h00000000), .exp_data_out(32'hFFFFFFFF)); // Reg 1 wr 00000000, byte 0 disabled + uart_req(.is_wr(0), .a(1), .data_in(32'hFFFFEEEE), .exp_data_out(32'h00000005)); // Reg 1 rd uart_req(.is_wr(1), .a(1), .data_in(32'h44EEFF0A), .exp_data_out(32'hFFFFFFFF)); // Reg 1 wr 44EEFF0A uart_req(.is_wr(0), .a(1), .data_in(32'hFFFFEEEE), .exp_data_out(32'h0000000A)); // Reg 1 rd uart_req(.is_wr(1), .a(1), .data_in(32'h00000000), .exp_data_out(32'hFFFFFFFF)); // Reg 1 wr 00000000 @@ -113,6 +115,8 @@ initial begin uart_req(.is_wr(0), .a(7), .data_in(32'hFFFFEEEE), .exp_data_out(32'h0000005A)); // Reg 7 rd uart_req(.is_wr(1), .a(7), .data_in(32'hFC5612A5), .exp_data_out(32'hFFFFFFFF)); // Reg 7 wr FC5612A5 uart_req(.is_wr(0), .a(7), .data_in(32'hFFFFEEEE), .exp_data_out(32'h000000A5)); // Reg 7 rd + uart_req(.is_wr(1), .byte_en(0), .a(7), .data_in(32'h0000003C), .exp_data_out(32'hFFFFFFFF)); // Reg 7 wr 0000003C, byte 0 disabled + uart_req(.is_wr(0), .a(7), .data_in(32'hFFFFEEEE), .exp_data_out(32'h000000A5)); // Reg 7 rd uart_req(.is_wr(1), .a(1), .data_in(32'h44EEFF0A), .exp_data_out(32'hFFFFFFFF)); // Reg 1 wr 44EEFF0A uart_req(.is_wr(1), .a(3), .data_in(32'h00000080), .exp_data_out(32'hFFFFFFFF)); // Reg 3 wr 00000080 uart_req(.is_wr(0), .a(0), .data_in(32'hFFFFEEEE), .exp_data_out(32'h00000000)); // Reg 0 rd @@ -125,6 +129,17 @@ initial begin uart_req(.is_wr(0), .a(0), .data_in(32'hFFFFEEEE), .exp_data_out(32'h00000000)); // Reg 0 rd uart_req(.is_wr(0), .a(1), .data_in(32'hFFFFEEEE), .exp_data_out(32'h0000000A)); // Reg 1 rd + // Misaligned accesses + // Should return error response with no other effect + repeat(30) uart_req(.is_wr(1'($urandom())), .byte_en(1'($urandom())), .a(3'($urandom())), .misaligned(1), .data_in($urandom()), .exp_data_out(32'hFFFFFFFF)); + + // Reads with byte 0 disabled + // Should return success response with no other effect + repeat(30) uart_req(.is_wr(0), .byte_en(0), .a(3'($urandom())), .data_in($urandom()), .exp_data_out(32'h00000000)); + + // Check that IER and SCR are unchanged + uart_req(.is_wr(0), .a(7), .data_in(32'hFFFFEEEE), .exp_data_out(32'h000000A5)); // Reg 7 rd + uart_req(.is_wr(0), .a(1), .data_in(32'hFFFFEEEE), .exp_data_out(32'h0000000A)); // Reg 1 rd repeat(10) @(posedge clk); @@ -139,7 +154,9 @@ end // Task to make TL-UL access to uart16550 task uart_req( input logic is_wr, + input logic byte_en = 1'b1, input logic [3-1:0] a, + input logic misaligned = 1'b0, input logic [top_pkg::TL_DW-1:0] data_in, input logic [top_pkg::TL_DW-1:0] exp_data_out ); @@ -149,6 +166,36 @@ tlul_pkg::tl_d_op_e exp_d_opcode; logic [top_pkg::TL_AIW-1:0] exp_d_source; begin + // Generate byte enable mask + if (byte_en) begin + if ($urandom_range(1) == 1) begin + // Full read/write with byte 0 enabled + req_tl_h2d.a_mask = '1; + end else begin + // Partial read/write with byte 0 enabled + if (is_wr && $urandom_range(1) == 1) begin + // Non-continuous mask + req_tl_h2d.a_mask = top_pkg::TL_DBW'($urandom()); + req_tl_h2d.a_mask[0] = 1'b1; + end else begin + // Continuous mask + // Can only be 1-byte or 2-byte accesses + req_tl_h2d.a_mask = ($urandom_range(1) == 1) ? 4'b0001 : 4'b0011; + end + end + end else begin + // Partial read/write with byte 0 disabled + if (is_wr && $urandom_range(1) == 1) begin + // Non-continuous mask + req_tl_h2d.a_mask = top_pkg::TL_DBW'($urandom()); + req_tl_h2d.a_mask[0] = 1'b0; + end else begin + // Continuous mask + // Can only be 0-byte accesses + req_tl_h2d.a_mask = '0; + end + end + // Set expected device opcode if (is_wr) begin exp_d_opcode = tlul_pkg::AccessAck; @@ -162,18 +209,23 @@ begin // Send request @(posedge clk); req_tl_h2d.a_valid = 1; - if (is_wr) begin + if (is_wr && req_tl_h2d.a_mask == '1) begin req_tl_h2d.a_opcode = tlul_pkg::PutFullData; + end else if (is_wr) begin + req_tl_h2d.a_opcode = tlul_pkg::PutPartialData; end else begin req_tl_h2d.a_opcode = tlul_pkg::Get; end req_tl_h2d.a_param = '0; req_tl_h2d.a_size = 2; req_tl_h2d.a_source = exp_d_source; - req_tl_h2d.a_address = $urandom(); // Unused address bits are randomised - req_tl_h2d.a_address[0 +: $clog2(top_pkg::TL_DBW)] = '0; // Address is word aligned + req_tl_h2d.a_address = $urandom(); // Unused address bits are randomised + if (misaligned) begin + req_tl_h2d.a_address[0 +: $clog2(top_pkg::TL_DBW)] = $clog2(top_pkg::TL_DBW)'($urandom_range(top_pkg::TL_DBW - 1, 1)); // Address is misaligned + end else begin + req_tl_h2d.a_address[0 +: $clog2(top_pkg::TL_DBW)] = '0; // Address is word aligned + end req_tl_h2d.a_address[$clog2(top_pkg::TL_DBW) +: 3] = a; // Register ID - req_tl_h2d.a_mask = 4'hF; req_tl_h2d.a_data = data_in; req_tl_h2d.a_user = '{ rsvd: '0, @@ -201,7 +253,7 @@ begin end while (!tl_o.d_valid) @(posedge clk); - assert(tl_o.d_error == 0) else begin + assert(tl_o.d_error == misaligned) else begin $warning("[t=%0t] d_error test failed!", $time()); has_assert_fail = 1; end diff --git a/hw/ip/uart16550/rtl/uart16550_translate.sv b/hw/ip/uart16550/rtl/uart16550_translate.sv index d9dcf4a..80abd9c 100644 --- a/hw/ip/uart16550/rtl/uart16550_translate.sv +++ b/hw/ip/uart16550/rtl/uart16550_translate.sv @@ -71,7 +71,8 @@ module uart16550_translate( DLReqSt = 10'h1B, DLReqWaitSt = 10'h1C, DLRespSt = 10'h1D, - ErrorRespSt = 10'h1E + ErrorRespSt = 10'h1E, + IgnoreRespSt = 10'h1F } state_e; state_e state_d, state_q; @@ -193,8 +194,12 @@ module uart16550_translate( req_mask_d = host_tl_i.a_mask; req_source_d = host_tl_i.a_source; + // Reset step register req_step_d = 2'h0; + end + if (host_tl_i.a_valid && host_tl_i.a_mask[0]) begin + // Valid request and byte is enabled // Decode request unique case (host_tl_i.a_address[4:0]) 'h0: begin @@ -272,6 +277,15 @@ module uart16550_translate( end default: state_d = ErrorRespSt; // Unsupported address endcase + end else if (host_tl_i.a_valid) begin + // Valid request and byte is disabled + if (host_tl_i.a_address[1:0] != '0) begin + // Misaligned address + state_d = ErrorRespSt; + end else begin + // Perform no operation and return successful response + state_d = IgnoreRespSt; + end end end // RBR read: send request to UART @@ -957,10 +971,30 @@ module uart16550_translate( state_d = IdleSt; end end + // Send response to host after no operation + IgnoreRespSt: begin + host_tl_d2h_pre.d_valid = 1'b1; + if (req_opcode_q == tlul_pkg::Get) begin + host_tl_d2h_pre.d_opcode = tlul_pkg::AccessAckData; + host_tl_d2h_pre.d_data = '0; + end else begin + host_tl_d2h_pre.d_opcode = tlul_pkg::AccessAck; + host_tl_d2h_pre.d_data = '1; + end + host_tl_d2h_pre.d_error = 1'b0; + + if (host_tl_i.d_ready) begin + state_d = IdleSt; + end + end // Send error response to host ErrorRespSt: begin host_tl_d2h_pre.d_valid = 1'b1; - host_tl_d2h_pre.d_opcode = tlul_pkg::AccessAck; + if (req_opcode_q == tlul_pkg::Get) begin + host_tl_d2h_pre.d_opcode = tlul_pkg::AccessAckData; + end else begin + host_tl_d2h_pre.d_opcode = tlul_pkg::AccessAck; + end host_tl_d2h_pre.d_data = '1; host_tl_d2h_pre.d_error = 1'b1;