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

[opentitan] Integrate OpenTitan Peripherals #11

Draft
wants to merge 19 commits into
base: devel
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 14 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*~
.ninja*
**/build/*
drivers/*/thirdparty/

.vscode/settings.json

Expand Down
3 changes: 0 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ set(CMAKE_C_COMPILER_WORKS 1)
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
set(CMAKE_VERBOSE_MAKEFILE TRUE)

# SCHEREMO: Help most IDE's LSPs find definitions
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

viv-eth marked this conversation as resolved.
Show resolved Hide resolved
# SCHEREMO: This toolchain file is only used for test compilation!
set(CMAKE_TOOLCHAIN_FILE cmake/toolchain_llvm.cmake)

Expand Down
110 changes: 110 additions & 0 deletions cmake/opentitan.cmake
Xeratec marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Copyright 2024 ETH Zurich and University of Bologna.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
#
# Viviane Potocnik <[email protected]>

include(ExternalProject)

# ------------------------------------------------------------------------------
# Validate prerequisites
# ------------------------------------------------------------------------------
if(NOT DEFINED OPENTITAN_COMMIT_HASH)
message(FATAL_ERROR "[CHIMERA-SDK] Please set OPENTITAN_COMMIT_HASH in your target.")
endif()

if(NOT DEFINED OPENTITAN_SPARSE_PATTERNS)
message(FATAL_ERROR "[CHIMERA-SDK] Please set OPENTITAN_SPARSE_PATTERNS for your target drivers.")
endif()

if(NOT DEFINED OPENTITAN_DIR)
message(FATAL_ERROR "[CHIMERA-SDK] Please set OPENTITAN_DIR in your target driver.")
endif()

# ------------------------------------------------------------------------------
# Write sparse checkout file
# ------------------------------------------------------------------------------
set(SPARSE_CHECKOUT_FILE "${CMAKE_CURRENT_BINARY_DIR}/sparse-checkout")
file(WRITE "${SPARSE_CHECKOUT_FILE}" "")
foreach(PATTERN ${OPENTITAN_SPARSE_PATTERNS})
file(APPEND "${SPARSE_CHECKOUT_FILE}" "${PATTERN}\n")
endforeach()

# ------------------------------------------------------------------------------
# Clone logic
# ------------------------------------------------------------------------------
if(NOT EXISTS "${OPENTITAN_DIR}/.git")
execute_process(
COMMAND git clone --no-checkout https://github.com/lowRISC/opentitan.git "${OPENTITAN_DIR}"
RESULT_VARIABLE GIT_CLONE_RESULT
ERROR_VARIABLE GIT_CLONE_ERROR
)
if(NOT GIT_CLONE_RESULT EQUAL 0)
message(FATAL_ERROR "Failed to clone OpenTitan repository: ${GIT_CLONE_ERROR}")
endif()
else()
message(STATUS "[CHIMERA-SDK] OpenTitan repository already exists at ${OPENTITAN_DIR}")
endif()

# ------------------------------------------------------------------------------
# Configure sparse checkout
# ------------------------------------------------------------------------------
execute_process(
COMMAND git -C "${OPENTITAN_DIR}" config core.sparseCheckout true
RESULT_VARIABLE GIT_CONFIG_RESULT
OUTPUT_VARIABLE GIT_CONFIG_OUTPUT
)

if(NOT GIT_CONFIG_RESULT EQUAL 0)
message(FATAL_ERROR "Failed to configure sparse checkout: ${GIT_CONFIG_OUTPUT}")
endif()

file(COPY "${SPARSE_CHECKOUT_FILE}" DESTINATION "${OPENTITAN_DIR}/.git/info")

# ------------------------------------------------------------------------------
# Apply sparse checkout
# ------------------------------------------------------------------------------
execute_process(
COMMAND git -C "${OPENTITAN_DIR}" read-tree -mu HEAD
RESULT_VARIABLE GIT_READ_TREE_RESULT
OUTPUT_VARIABLE GIT_READ_TREE_OUTPUT
)

if(NOT GIT_READ_TREE_RESULT EQUAL 0)
message(FATAL_ERROR "Failed to apply sparse checkout: ${GIT_READ_TREE_OUTPUT}")
endif()

# ------------------------------------------------------------------------------
# Checkout the desired commit hash
# ------------------------------------------------------------------------------
execute_process(
COMMAND git -C "${OPENTITAN_DIR}" checkout "${OPENTITAN_COMMIT_HASH}"
RESULT_VARIABLE GIT_CHECKOUT_RESULT
OUTPUT_VARIABLE GIT_CHECKOUT_OUTPUT
)

if(NOT GIT_CHECKOUT_RESULT EQUAL 0)
message(FATAL_ERROR "Failed to checkout commit ${OPENTITAN_COMMIT_HASH}: ${GIT_CHECKOUT_OUTPUT}")
endif()

# ------------------------------------------------------------------------------
# Add OpenTitan as an external project
# ------------------------------------------------------------------------------
ExternalProject_Add(
opentitan_build_${OPENTITAN_COMMIT_HASH}
SOURCE_DIR "${OPENTITAN_DIR}"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
BUILD_BYPRODUCTS "${OPENTITAN_DIR}/.git/config"
)

add_custom_target(opentitan_target_${OPENTITAN_COMMIT_HASH} ALL
DEPENDS opentitan_build_${OPENTITAN_COMMIT_HASH}
)

# ------------------------------------------------------------------------------
# Interface target for other targets to depend on
# ------------------------------------------------------------------------------
add_library(opentitan_interface_${OPENTITAN_COMMIT_HASH} INTERFACE)
add_dependencies(opentitan_interface_${OPENTITAN_COMMIT_HASH} opentitan_target_${OPENTITAN_COMMIT_HASH})
2 changes: 1 addition & 1 deletion cmake/toolchain_llvm.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ set(CMAKE_OBJDUMP ${TOOLCHAIN_DIR}/${LLVM_TAG}-objdump)
set(CMAKE_AR ${TOOLCHAIN_DIR}/${LLVM_TAG}-ar)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --target=riscv32-unknown-elf")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --target=riscv32-unknown-elf")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --target=riscv32-unknown-elf")
3 changes: 2 additions & 1 deletion drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
#
# Viviane Potocnik <[email protected]>
# Philip Wiese <[email protected]>

# Define mappings for drivers
set(DRIVER_MAPPINGS
chimera-convolve:cluster
chimera-open:cluster
chimera-open:cluster,uart_${UART_DRIVER_VERSION}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see the comment below. I am not a big fan of the uart_${UART_DRIVER_VERSION} and suggest uart_opentitan.

chimera-host:
)

Expand Down
67 changes: 67 additions & 0 deletions drivers/uart_opentitan/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2024 ETH Zurich and University of Bologna.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
#
# Viviane Potocnik <[email protected]>

# ------------------------------------------------------------------------------
# Set the variables for the OpenTitan UART driver
# ------------------------------------------------------------------------------
set(OPENTITAN_DIR "${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/opentitan")
set(OPENTITAN_SPARSE_PATTERNS
"util/design/*"
"util/reggen/*"
"util/regtool.py"
"util/version_file.py"
"hw/ip/uart/data/uart.hjson"
"sw/device/lib/base/abs_mmio.c"
"sw/device/lib/base/abs_mmio.h"
"sw/device/lib/base/bitfield.c"
"sw/device/lib/base/bitfield.h"
"sw/device/lib/base/macros.h"
"sw/device/lib/base/math.c"
"sw/device/lib/base/math.h"
"sw/device/lib/base/math_builtins.c"
"sw/device/lib/base/memory.c"
"sw/device/lib/base/memory.h"
)

# ------------------------------------------------------------------------------
# Call the OpenTitan driver setup
# ------------------------------------------------------------------------------
include(${CMAKE_SOURCE_DIR}/cmake/opentitan.cmake)

# ------------------------------------------------------------------------------
# Generate the uart_regs.h header using the OT regtool
# ------------------------------------------------------------------------------
set(OPENTITAN_SW_DIR "${OPENTITAN_DIR}/sw")
set(OPENTITAN_HW_IP_DIR "${OPENTITAN_DIR}/hw/ip")
set(OPENTITAN_UTIL_DIR "${OPENTITAN_DIR}/util")

set(REGTOOL_PY "${OPENTITAN_UTIL_DIR}/regtool.py")
set(UART_HJSON "${OPENTITAN_HW_IP_DIR}/uart/data/uart.hjson")
set(UART_REGS_H "${OPENTITAN_SW_DIR}/device/lib/dif/autogen/uart_regs.h")


# Define the output directory
get_filename_component(UART_REGS_DIR ${UART_REGS_H} DIRECTORY)

# Custom command to generate UART registers header
add_custom_command(
OUTPUT ${UART_REGS_H}
COMMAND ${CMAKE_COMMAND} -E make_directory ${UART_REGS_DIR}
COMMAND ${Python3_EXECUTABLE} ${REGTOOL_PY} ${UART_HJSON} -D > ${UART_REGS_H}
DEPENDS ${REGTOOL_PY} ${UART_HJSON}
COMMENT "Generating ${UART_REGS_H} with COMMAND ${Python3_EXECUTABLE} ${REGTOOL_PY} ${UART_HJSON} -D > ${UART_REGS_H}"
)

message(STATUS "[REMOVE ME] COMMAND: ${Python3_EXECUTABLE} ${REGTOOL_PY} ${UART_HJSON} -D > ${UART_REGS_H}")

# Generate the uart_regs.h header using the OT regtool
add_custom_target(
generate_uart_regs
ALL
DEPENDS "${UART_REGS_H}"
)

add_dependencies(generate_uart_regs opentitan_interface_${OPENTITAN_COMMIT_HASH})
9 changes: 9 additions & 0 deletions targets/chimera-open/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@
################################################################################
# Host Runtime Library #
################################################################################

# VIVIANEP: Select target-specific commit hash of the OT lib
set(TARGET_OPENTITAN_COMMIT_HASH 22168a3d42febfca725b36dd0d1554b68cf5783e)
set(OPENTITAN_COMMIT_HASH ${TARGET_OPENTITAN_COMMIT_HASH} CACHE STRING "OpenTitan commit hash")

# VIVIANEP: Choose driver versions (if applicable)
set(UART_DRIVER_VERSION "opentitan" CACHE STRING "UART driver version")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this additional abstraction and not just include uart_opentitan above? I don't see any benefit but potential unclarity.


add_library(runtime_host STATIC)


file(GLOB_RECURSE ASM_SOURCES
"src/crt0.S"
)
Expand Down
Loading