Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dma] Port DMA tests from integrated_dev branch #26348

Merged
merged 6 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions sw/device/lib/dif/dif_spi_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,14 @@ static dif_result_t issue_data_phase(const dif_spi_host_t *spi_host,
return kDifOk;
}

dif_result_t dif_spi_host_transaction(const dif_spi_host_t *spi_host,
uint32_t csid,
dif_spi_host_segment_t *segments,
size_t length) {
dif_result_t dif_spi_host_start_transaction(const dif_spi_host_t *spi_host,
uint32_t csid,
dif_spi_host_segment_t *segments,
size_t length) {
if (spi_host == NULL || segments == NULL) {
return kDifBadArg;
}

// Write to chip select ID.
mmio_region_write32(spi_host->base_addr, SPI_HOST_CSID_REG_OFFSET, csid);

Expand Down Expand Up @@ -405,6 +409,15 @@ dif_result_t dif_spi_host_transaction(const dif_spi_host_t *spi_host,
return kDifBadArg;
}
}
return kDifOk;
}

dif_result_t dif_spi_host_transaction(const dif_spi_host_t *spi_host,
uint32_t csid,
dif_spi_host_segment_t *segments,
size_t length) {
DIF_RETURN_IF_ERROR(
dif_spi_host_start_transaction(spi_host, csid, segments, length));

// For each segment which receives data, read from the receive FIFO.
for (size_t i = 0; i < length; ++i) {
Expand Down
15 changes: 15 additions & 0 deletions sw/device/lib/dif/dif_spi_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,21 @@ OT_WARN_UNUSED_RESULT
dif_result_t dif_spi_host_fifo_read(const dif_spi_host_t *spi_host, void *dst,
uint16_t len);

/**
* Begins a SPI Host transaction without reading the FIFOs.
*
* @param spi_host A SPI Host handle.
* @param csid The chip-select ID of the SPI target.
* @param segments The SPI segments to send in this transaction.
* @param length The number of SPI segments in this transaction.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_spi_host_start_transaction(const dif_spi_host_t *spi_host,
uint32_t csid,
dif_spi_host_segment_t *segments,
size_t length);

/**
* Begins a SPI Host transaction.
*
Expand Down
15 changes: 10 additions & 5 deletions sw/device/lib/dif/dif_spi_host_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,13 @@ TEST_F(ConfigTest, SpiTxRxWatermark) {
EXPECT_DIF_OK(dif_spi_host_configure(&spi_host_, config_));
}

class TransactionTest : public SpiHostTest {
class TransactionStartTest : public SpiHostTest {
protected:
MockFifo fifo_;
};

// Checks that an opcode segment is sent correctly.
TEST_F(TransactionTest, IssueOpcode) {
TEST_F(TransactionStartTest, IssueOpcode) {
dif_spi_host_segment segment;
segment.type = kDifSpiHostSegmentTypeOpcode;
segment.opcode.opcode = 0x5a;
Expand All @@ -302,7 +302,7 @@ TEST_F(TransactionTest, IssueOpcode) {
}

// Checks that an address segment is sent correctly in 3-byte mode.
TEST_F(TransactionTest, IssueAddressMode3b) {
TEST_F(TransactionStartTest, IssueAddressMode3b) {
dif_spi_host_segment segment;
segment.type = kDifSpiHostSegmentTypeAddress;
segment.address.width = kDifSpiHostWidthStandard;
Expand All @@ -321,7 +321,7 @@ TEST_F(TransactionTest, IssueAddressMode3b) {
}

// Checks that an address segment is sent correctly in 4-byte mode.
TEST_F(TransactionTest, IssueAddressMode4b) {
TEST_F(TransactionStartTest, IssueAddressMode4b) {
dif_spi_host_segment segment;
segment.type = kDifSpiHostSegmentTypeAddress;
segment.address.width = kDifSpiHostWidthStandard;
Expand All @@ -340,7 +340,7 @@ TEST_F(TransactionTest, IssueAddressMode4b) {
}

// Checks that a dummy segment is sent correctly.
TEST_F(TransactionTest, IssueDummy) {
TEST_F(TransactionStartTest, IssueDummy) {
dif_spi_host_segment segment;
segment.type = kDifSpiHostSegmentTypeDummy;
segment.dummy.width = kDifSpiHostWidthStandard;
Expand All @@ -354,6 +354,11 @@ TEST_F(TransactionTest, IssueDummy) {
EXPECT_DIF_OK(dif_spi_host_transaction(&spi_host_, 0, &segment, 1));
}

class TransactionTest : public SpiHostTest {
protected:
MockFifo fifo_;
};

// Checks that a transmit segment is sent correctly.
TEST_F(TransactionTest, TransmitDual) {
uint8_t buf[32];
Expand Down
15 changes: 15 additions & 0 deletions sw/device/lib/testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -573,3 +573,18 @@ cc_library(
"//sw/device/lib/testing/test_framework:check",
],
)

cc_library(
name = "dma_testutils",
srcs = ["dma_testutils.c"],
hdrs = ["dma_testutils.h"],
target_compatible_with = [OPENTITAN_CPU],
deps = [
"//hw/top_darjeeling/sw/autogen:top_darjeeling",
"//sw/device/lib/dif:dma",
"//sw/device/lib/dif:pinmux",
"//sw/device/lib/dif:spi_host",
"//sw/device/lib/runtime:ibex",
"//sw/device/lib/testing/test_framework:check",
],
)
89 changes: 89 additions & 0 deletions sw/device/lib/testing/dma_testutils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

#include "sw/device/lib/testing/dma_testutils.h"

#include "hw/top_darjeeling/sw/autogen/top_darjeeling.h"
#include "spi_host_regs.h" // Generated.

static const top_darjeeling_direct_pads_t spi_host0_direct_pads[6] = {
kTopDarjeelingDirectPadsSpiHost0Sck, // sck
kTopDarjeelingDirectPadsSpiHost0Csb, // csb
kTopDarjeelingDirectPadsSpiHost0Sd3, // sio[3]
kTopDarjeelingDirectPadsSpiHost0Sd2, // sio[2]
kTopDarjeelingDirectPadsSpiHost0Sd1, // sio[1]
kTopDarjeelingDirectPadsSpiHost0Sd0}; // sio[0]

/**
* Initialize the provided SPI host for being used in the DMA hardware handshake
* mode.
*/
void init_spi_host(dif_spi_host_t *spi_host, uint32_t peripheral_clock_freq_hz,
uint32_t rx_watermark) {
dif_spi_host_config_t config = {
.spi_clock = peripheral_clock_freq_hz / 2,
.peripheral_clock_freq_hz = peripheral_clock_freq_hz,
.chip_select = {.idle = 2, .trail = 2, .lead = 2},
.full_cycle = true,
.cpha = true,
.cpol = true,
.rx_watermark = rx_watermark};
CHECK_DIF_OK(dif_spi_host_configure(spi_host, config));
CHECK_DIF_OK(dif_spi_host_output_set_enabled(spi_host, /*enabled=*/true));
}

/**
* Setup pads for spi_host0
*
* This peripheral is 'direct' connected to the pads.
*/
void setup_pads_spi_host0(dif_pinmux_t *pinmux) {
// set weak pull-ups for all the pads
dif_pinmux_pad_attr_t out_attr;
dif_pinmux_pad_attr_t in_attr = {
.slew_rate = 0,
.drive_strength = 0,
.flags = kDifPinmuxPadAttrPullResistorEnable |
kDifPinmuxPadAttrPullResistorUp};
for (uint32_t i = 0; i <= ARRAYSIZE(spi_host0_direct_pads); ++i) {
CHECK_DIF_OK(dif_pinmux_pad_write_attrs(pinmux, spi_host0_direct_pads[i],
kDifPinmuxPadKindDio, in_attr,
&out_attr));
}
}

void setup_spi_dma_transaction(dif_spi_host_t *spi_host, dif_dma_t *dma,
uint8_t *rx_buffer, uint32_t chunk_size,
uint32_t total_size) {
dif_spi_host_segment_t host_operations[1] = {
{.type = kDifSpiHostSegmentTypeRx,
.tx = {.width = kDifSpiHostWidthStandard,
.buf = NULL,
.length = total_size}},
};

// Issue the SPI transaction
CHECK_DIF_OK(dif_spi_host_start_transaction(
spi_host, /*csid=*/0, host_operations, ARRAYSIZE(host_operations)));

// Configure the DMA to read from SPI in hardware-handshake mode
dif_dma_transaction_t transaction = {
.source = {.address = TOP_DARJEELING_SPI_HOST0_BASE_ADDR +
SPI_HOST_RXDATA_REG_OFFSET,
.asid = kDifDmaOpentitanInternalBus},
.destination = {.address = (uint32_t)&rx_buffer[0],
.asid = kDifDmaOpentitanInternalBus},
.src_config = {.wrap = false, .increment = false},
.dst_config = {.wrap = false, .increment = true},
.total_size = total_size,
.chunk_size = chunk_size,
.width = kDifDmaTransWidth4Bytes};

CHECK_DIF_OK(dif_dma_memory_range_set(dma, TOP_DARJEELING_RAM_MAIN_BASE_ADDR,
TOP_DARJEELING_RAM_MAIN_SIZE_BYTES));
// Enable LSIO trigger for SPI host at bit 1
CHECK_DIF_OK(dif_dma_handshake_irq_enable(dma, 0x2));
CHECK_DIF_OK(dif_dma_configure(dma, transaction));
CHECK_DIF_OK(dif_dma_handshake_enable(dma));
}
58 changes: 58 additions & 0 deletions sw/device/lib/testing/dma_testutils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

#ifndef OPENTITAN_SW_DEVICE_LIB_TESTING_DMA_TESTUTILS_H_
#define OPENTITAN_SW_DEVICE_LIB_TESTING_DMA_TESTUTILS_H_

#include "sw/device/lib/dif/dif_dma.h"
#include "sw/device/lib/dif/dif_pinmux.h"
#include "sw/device/lib/dif/dif_spi_host.h"
#include "sw/device/lib/testing/test_framework/check.h"

/**
* Initialize the provided SPI host for being used in the DMA hardware handshake
* mode.
*
* @param spi_host An spi_host DIF handle.
* @param peripheral_clock_freq_hz The peripheral clock frequency in hertz.
* @param rx_watermark The watermark level of the FIFO to raise the status IRQ
* for the DMA.
*/
void init_spi_host(dif_spi_host_t *spi_host, uint32_t peripheral_clock_freq_hz,
uint32_t rx_watermark);

/**
* Setup pads for spi_host0
*
* This peripheral is 'direct' connected to the pads.
*
* @param pinmux An pinmux DIF handle.
*/
void setup_pads_spi_host0(dif_pinmux_t *pinmux);

/**
* Configure the SPI for a receiving transaction and configure the DMA for the
* hardware handshake mode to read the SPI host.
*
* @param spi_host An spi_host DIF handle.
* @param dma An DMA DIF handle.
* @param rx_buffer Pointer to the receiving buffer, where the DMA writes the
* received data.
* @param chunk_size The size of one chunk read from the LSIO peripheral.
* @param total_size The total size of data to be read from the LSIO
* peripheral.
*/
void setup_spi_dma_transaction(dif_spi_host_t *spi_host, dif_dma_t *dma,
uint8_t *rx_buffer, uint32_t chunk_size,
uint32_t total_size);

/**
* Return the digest length given a DMA opcode.
*
* @param opcode A DMA opcode.
* @return The digest length.
*/
uint32_t get_digest_length(dif_dma_transaction_opcode_t opcode);

#endif // OPENTITAN_SW_DEVICE_LIB_TESTING_DMA_TESTUTILS_H_
42 changes: 42 additions & 0 deletions sw/device/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7432,3 +7432,45 @@ opentitan_test(
),
deps = [":rom_exit_immediately_lib"],
)

opentitan_test(
name = "dma_inline_hashing",
srcs = ["dma_inline_hashing.c"],
exec_env = {
"//hw/top_darjeeling:sim_dv": None,
},
deps = [
"//hw/top:dt",
"//sw/device/lib/base:macros",
"//sw/device/lib/base:mmio",
"//sw/device/lib/dif:dma",
"//sw/device/lib/dif:pinmux",
"//sw/device/lib/dif:rv_plic",
"//sw/device/lib/dif:spi_host",
"//sw/device/lib/runtime:log",
"//sw/device/lib/runtime:print",
"//sw/device/lib/testing:dma_testutils",
"//sw/device/lib/testing:pinmux_testutils",
"//sw/device/lib/testing/test_framework:ottf_main",
],
)

opentitan_test(
name = "dma_abort",
srcs = ["dma_abort.c"],
exec_env = {
"//hw/top_darjeeling:sim_dv": None,
},
deps = [
"//hw/top:dt",
"//sw/device/lib/base:macros",
"//sw/device/lib/base:mmio",
"//sw/device/lib/dif:dma",
"//sw/device/lib/dif:spi_host",
"//sw/device/lib/runtime:log",
"//sw/device/lib/runtime:print",
"//sw/device/lib/testing:dma_testutils",
"//sw/device/lib/testing:pinmux_testutils",
"//sw/device/lib/testing/test_framework:ottf_main",
],
)
Loading
Loading