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 12 commits into
base: devel
Choose a base branch
from
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
146 changes: 146 additions & 0 deletions cmake/opentitan.cmake
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would move this file into drivers/opentitan. Please look at the summary comment.

Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# 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)

set(OPENTITAN_DIR ${CMAKE_SOURCE_DIR}/drivers/opentitan)
set(SPARSE_CHECKOUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/sparse-checkout)

if(NOT DEFINED OPENTITAN_COMMIT_HASH)
message(FATAL_ERROR "Please set a commit hash in your target.")
endif()

message(STATUS "[OT Drivers] Using OpenTitan commit hash: ${OPENTITAN_COMMIT_HASH}")
Copy link
Collaborator

Choose a reason for hiding this comment

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

NIT, but please make sure the message aligns with the other ones.
Also, I would use a [CHIMERA-SDK] tag to be consistent and avoid polluting the messages. This way all messages from the SDK can be easily spotted.


set(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"
)

file(WRITE ${SPARSE_CHECKOUT_FILE} "")
foreach(PATTERN ${SPARSE_PATTERNS})
file(APPEND ${SPARSE_CHECKOUT_FILE} "${PATTERN}\n")
endforeach()

# Clone and set up the OpenTitan repository during configuration
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
OUTPUT_VARIABLE GIT_CLONE_OUTPUT
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 "[OT Drivers] OpenTitan repository already exists.")
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
ERROR_VARIABLE GIT_CONFIG_ERROR
)
if(NOT GIT_CONFIG_RESULT EQUAL 0)
message(FATAL_ERROR "Failed to configure sparse checkout: ${GIT_CONFIG_ERROR}")
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
ERROR_VARIABLE GIT_READ_TREE_ERROR
)
if(NOT GIT_READ_TREE_RESULT EQUAL 0)
message(FATAL_ERROR "Failed to apply sparse checkout: ${GIT_READ_TREE_ERROR}")
endif()

# Checkout the desired branch (e.g., master)
execute_process(
COMMAND git -C ${OPENTITAN_DIR} checkout ${OPENTITAN_COMMIT_HASH}
RESULT_VARIABLE GIT_CHECKOUT_RESULT
OUTPUT_VARIABLE GIT_CHECKOUT_OUTPUT
ERROR_VARIABLE GIT_CHECKOUT_ERROR
)
if(NOT GIT_CHECKOUT_RESULT EQUAL 0)
message(FATAL_ERROR "Failed to checkout commit ${OPENTITAN_COMMIT_HASH}: ${GIT_CHECKOUT_ERROR}")
endif()

# Now proceed with adding the external project to manage build steps
ExternalProject_Add(
opentitan_build
SOURCE_DIR ${OPENTITAN_DIR}
CONFIGURE_COMMAND "" # No configure step needed
BUILD_COMMAND "" # No build step needed (unless you have specific build steps)
INSTALL_COMMAND "" # No install step needed
BUILD_BYPRODUCTS ${OPENTITAN_DIR}/.git/config
)

# Define directories for OpenTitan sources
set(OPENTITAN_SW_DIR ${OPENTITAN_DIR}/sw)
set(OPENTITAN_HW_IP_DIR ${OPENTITAN_DIR}/hw/ip)
set(OPENTITAN_UTIL_DIR ${OPENTITAN_DIR}/util)

# Paths for regtool and UART headers
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}"
)

add_custom_target(
generate_uart_regs ALL
DEPENDS ${UART_REGS_H}
)

add_dependencies(generate_uart_regs opentitan_build)

file(GLOB_RECURSE OPENTITAN_SOURCES
LIST_DIRECTORIES false
${OPENTITAN_SW_DIR}/device/lib/base/*.c
)

# Create OpenTitan library
add_library(opentitan_lib STATIC ${OPENTITAN_SOURCES})

# Ensure the library depends on the external project build
add_dependencies(opentitan_lib opentitan_build generate_uart_regs)

# Include directories for OpenTitan library
target_include_directories(opentitan_lib
PUBLIC
${OPENTITAN_DIR}
)

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")
7 changes: 7 additions & 0 deletions drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# 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
Expand All @@ -17,3 +18,9 @@ add_chimera_subdirectories(${TARGET_PLATFORM} "Driver" "${DRIVER_MAPPINGS}")

# WIESEP: Export this directory as root include directory for the drivers
target_include_directories(runtime_host PUBLIC ${CMAKE_CURRENT_LIST_DIR})

# VIVIANEP: Link to the opentitan library
target_link_libraries(runtime_host
PUBLIC
opentitan_lib
)
Comment on lines +22 to +26
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is the wrong place to do this. Please look at the summary comment.

10 changes: 10 additions & 0 deletions targets/chimera-open/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@
################################################################################
# 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})

# VIVIANEP: Add OpenTitan as an external project
include(${CMAKE_SOURCE_DIR}/cmake/opentitan.cmake)

Comment on lines +12 to +20
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is the wrong place to do this. Please look at the summary comment.

add_library(runtime_host STATIC)


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