Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Amaury Pouly <[email protected]>
  • Loading branch information
pamaury committed Nov 27, 2024
1 parent 4da3f37 commit 2fa6c63
Show file tree
Hide file tree
Showing 57 changed files with 467 additions and 198 deletions.
16 changes: 16 additions & 0 deletions hw/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
load("//rules:fusesoc.bzl", "fusesoc_build")

package(default_visibility = ["//visibility:public"])

# This configuration exposes fusesoc's "verilator_options" option to the
# command line. This is intended to allow CI to specifically build a single
# -threaded Verilated model to suit it's resource constraints.
# By default, the Verilated model should be built to
# run with 4 threads.
load("@bazel_skylib//rules:common_settings.bzl", "string_list_flag")
load("//hw/top:defs.bzl", "opentitan_select_top")

string_list_flag(
name = "verilator_options",
Expand All @@ -20,6 +23,19 @@ string_list_flag(
],
)

# HACK
# This alias points to the correct sim_dv execution environment based on the top.
[
alias(
name = env,
actual = opentitan_select_top({
"earlgrey": "//hw/top_earlgrey:{}".format(env),
"darjeeling": "//hw/top_darjeeling:{}".format(env),
}, "//invalid/top"),
)
for env in ["sim_dv_base", "sim_dv"]
]

# This configuration exposes fusesoc's "make_options" to enable parallel
# compilation of the verilated model. Compilation takes about 30m of cpu time
# and 5m of time that isn't parallelized by this option, so this should reduce
Expand Down
20 changes: 19 additions & 1 deletion hw/top/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: Apache-2.0

load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("//rules/opentitan:hw.bzl", "select_top", "select_top_lib")
load("//rules/opentitan:hw.bzl", "select_top", "select_top_lib", "select_top_ld")
load("//rules/opentitan:util.bzl", "flatten")
load(
"//rules:autogen.bzl",
Expand Down Expand Up @@ -71,6 +71,24 @@ alias(
}),
)

[
select_top_ld(
name = "top_{}_ld".format(top),
all_tops = ALL_TOPS,
top = top,
)
for top in ALL_TOP_NAMES
]

# Point to the right top library.
alias(
name = "top_ld",
actual = select({
"is_{}".format(top): ":top_{}_ld".format(top)
for top in ALL_TOP_NAMES
}),
)

[
opentitan_ip_c_header(
name = "{}_c_regs".format(ip),
Expand Down
1 change: 1 addition & 0 deletions hw/top_darjeeling/data/autogen/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ DARJEELING = opentitan_top(
name = "darjeeling",
hjson = "//hw/top_darjeeling/data/autogen:top_darjeeling.gen.hjson",
top_lib = "//hw/top_darjeeling/sw/autogen:top_darjeeling",
top_ld = "//hw/top_darjeeling/sw/autogen:top_darjeeling_memory",
ips = [
AES,
ALERT_HANDLER,
Expand Down
4 changes: 2 additions & 2 deletions hw/top_darjeeling/sw/autogen/top_darjeeling_memory.ld
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ MEMORY {
ram_mbox(rwx) : ORIGIN = 0x11000000, LENGTH = 0x1000
rom0(rx) : ORIGIN = 0x00008000, LENGTH = 0x8000
rom1(rx) : ORIGIN = 0x00020000, LENGTH = 0x10000
rom_ext_virtual(rx) : ORIGIN = 0x90000000, LENGTH = None
owner_virtual(rx) : ORIGIN = 0xa0000000, LENGTH = None
rom_ext_virtual(rx) : ORIGIN = 0x90000000, LENGTH = 0x80000
owner_virtual(rx) : ORIGIN = 0xa0000000, LENGTH = 0x80000
}

/**
Expand Down
1 change: 1 addition & 0 deletions hw/top_earlgrey/data/autogen/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ EARLGREY = opentitan_top(
name = "earlgrey",
hjson = "//hw/top_earlgrey/data/autogen:top_earlgrey.gen.hjson",
top_lib = "//hw/top_earlgrey/sw/autogen:top_earlgrey",
top_ld = "//hw/top_earlgrey/sw/autogen:top_earlgrey_memory",
ips = [
ADC_CTRL,
AES,
Expand Down
4 changes: 4 additions & 0 deletions rules/host.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def _host_tools_transition_impl(settings, attr):
"//hw/bitstream/universal:rom": "//hw/bitstream/universal:none",
"//hw/bitstream/universal:otp": "//hw/bitstream/universal:none",
"//hw/bitstream/universal:env": "//hw/bitstream/universal:none",
# WARNING This is a horrible hack: when we transition to host, we pretend
# that this is earlgrey so opentitantool can compile...
"//hw/top": "earlgrey",
}
return ret

Expand All @@ -39,5 +42,6 @@ host_tools_transition = transition(
"//hw/bitstream/universal:rom",
"//hw/bitstream/universal:otp",
"//hw/bitstream/universal:env",
"//hw/top",
],
)
31 changes: 28 additions & 3 deletions rules/linker.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@
"""Rules for declaring linker scripts and linker script fragments."""

def _ld_library_impl(ctx):
files = [] + ctx.files.includes
substitutions = {
key: ctx.expand_location(subst)
for (key, subst) in ctx.attr.substitutions.items()
}

def get_file(f):
if ctx.attr.substitutions == {}:
return f
fout = ctx.actions.declare_file(ctx.label.name + "_" + f.basename)
ctx.actions.expand_template(
template = f,
output = fout,
substitutions = substitutions,
)
return fout

files = [get_file(f) for f in ctx.files.includes]
user_link_flags = []

# Disable non-volatile scratch region and counters if building for english
Expand All @@ -21,12 +37,15 @@ def _ld_library_impl(ctx):
]

if ctx.file.script:
files += ctx.files.script
files.append(get_file(ctx.file.script))
user_link_flags += [
"-Wl,-T,{}".format(ctx.file.script.path),
"-Wl,-T,{}".format(files[-1].path),
]

return [
DefaultInfo(
files = depset(files),
),
cc_common.merge_cc_infos(
direct_cc_infos = [CcInfo(
linking_context = cc_common.create_linking_context(
Expand Down Expand Up @@ -57,6 +76,10 @@ ld_library = rule(
alignment for segments and not to include the headers in the first segment
(this is the -nmagic option of GNU ld). See https://reviews.llvm.org/D61201
for more details.
If the substitution dictionary is not empty, the content of the file will
be expanded using bazel's `actions.expand_template`. If some substitutions
refer to label not present in `deps`, they need to be added to `subst_deps`.
""",
attrs = {
"script": attr.label(allow_single_file = True),
Expand All @@ -71,5 +94,7 @@ ld_library = rule(
"non_page_aligned_segments": attr.bool(
default = False,
),
"substitutions": attr.string_dict(),
"subst_deps": attr.label_list()
},
)
4 changes: 3 additions & 1 deletion rules/opentitan/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ opentitan_manual_test = _opentitan_manual_test
EARLGREY_TEST_ENVS = {
"//hw/top_earlgrey:fpga_cw310_sival_rom_ext": None,
"//hw/top_earlgrey:fpga_cw310_rom_with_fake_keys": None,
"//hw/top_earlgrey:sim_dv": None,
# "//hw/top_earlgrey:sim_dv": None,
# HACK
"//hw:sim_dv": None,
"//hw/top_earlgrey:sim_verilator": None,
}

Expand Down
16 changes: 15 additions & 1 deletion rules/opentitan/hw.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def opentitan_ip(name, hjson):
hjson = hjson,
)

def opentitan_top(name, hjson, top_lib, ips):
def opentitan_top(name, hjson, top_lib, top_ld, ips):
"""
Return a structure describing a top.
Expand All @@ -32,6 +32,7 @@ def opentitan_top(name, hjson, top_lib, ips):
name = name,
hjson = hjson,
top_lib = top_lib,
top_ld = top_ld,
ips = ips,
)

Expand Down Expand Up @@ -124,3 +125,16 @@ def select_top_lib(name, all_tops, top):
name = name,
actual = libs[0],
)

def select_top_ld(name, all_tops, top):
"""
Create an alias to the top library.
"""
libs = [_top.top_ld for _top in all_tops if _top.name == top]
if len(libs) == 0:
fail("not top found with name {}".format(top))

native.alias(
name = name,
actual = libs[0],
)
2 changes: 1 addition & 1 deletion sw/device/examples/sram_program/sram_program.ld
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ OUTPUT_ARCH(riscv);
*/
__DYNAMIC = 0;

INCLUDE hw/top_earlgrey/sw/autogen/top_earlgrey_memory.ld
INCLUDE top_memory.ld

_stack_end = ORIGIN(ram_main) + LENGTH(ram_main);

Expand Down
2 changes: 1 addition & 1 deletion sw/device/lib/arch/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ cc_library(
":device",
"//hw/top:rv_core_ibex_c_regs",
"//hw/top:uart_c_regs",
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
"//hw/top:top_lib",
],
)

Expand Down
15 changes: 12 additions & 3 deletions sw/device/lib/arch/device_sim_dv.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@

#include "sw/device/lib/arch/device.h"

#include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"
#include "rv_core_ibex_regs.h"
#include "uart_regs.h"

#if defined(OPENTITAN_IS_EARLGREY)
#include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"
#define RV_CORE_IBEX_CFG_BASE_ADDR TOP_EARLGREY_RV_CORE_IBEX_CFG_BASE_ADDR
#elif defined(OPENTITAN_IS_DARJEELING)
#include "hw/top_darjeeling/sw/autogen/top_darjeeling.h"
#define RV_CORE_IBEX_CFG_BASE_ADDR TOP_DARJEELING_RV_CORE_IBEX_CFG_BASE_ADDR
#else
#error unsupported top
#endif

/**
* Device-specific symbol definitions for the DV simulation device.
*/
Expand Down Expand Up @@ -58,11 +67,11 @@ const uint32_t kAstCheckPollCpuCycles =
CALCULATE_AST_CHECK_POLL_CPU_CYCLES(kClockFreqCpuHz);

const uintptr_t kDeviceTestStatusAddress =
TOP_EARLGREY_RV_CORE_IBEX_CFG_BASE_ADDR +
RV_CORE_IBEX_CFG_BASE_ADDR +
RV_CORE_IBEX_DV_SIM_WINDOW_REG_OFFSET;

const uintptr_t kDeviceLogBypassUartAddress =
TOP_EARLGREY_RV_CORE_IBEX_CFG_BASE_ADDR +
RV_CORE_IBEX_CFG_BASE_ADDR +
RV_CORE_IBEX_DV_SIM_WINDOW_REG_OFFSET + 0x04;

void device_fpga_version_print(void) {}
44 changes: 44 additions & 0 deletions sw/device/lib/dif/dif_pinmux.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,21 @@ static bool dif_pinmux_get_sleep_status_bit(dif_pinmux_pad_kind_t kind,
switch (kind) {
case kDifPinmuxPadKindMio:
num_pads = PINMUX_PARAM_N_MIO_PADS;
// Only platforms with few MIOs, there is a single register with no index.
#ifdef PINMUX_MIO_PAD_SLEEP_STATUS_0_REG_OFFSET
reg_base = PINMUX_MIO_PAD_SLEEP_STATUS_0_REG_OFFSET;
#else
reg_base = PINMUX_MIO_PAD_SLEEP_STATUS_REG_OFFSET;
#endif
break;
case kDifPinmuxPadKindDio:
num_pads = PINMUX_PARAM_N_DIO_PADS;
// Only platforms with few DIOs, there is a single register with no index.
#ifdef PINMUX_DIO_PAD_SLEEP_STATUS_0_REG_OFFSET
reg_base = PINMUX_DIO_PAD_SLEEP_STATUS_0_REG_OFFSET;
#else
reg_base = PINMUX_DIO_PAD_SLEEP_STATUS_REG_OFFSET;
#endif
break;
default:
return false;
Expand Down Expand Up @@ -334,6 +344,40 @@ dif_result_t dif_pinmux_pad_write_attrs(const dif_pinmux_t *pinmux,
return kDifOk;
}

dif_result_t dif_pinmux_pad_from_dt_pad(dt_pad_t pad,
dif_pinmux_index_t *index_out,
dif_pinmux_pad_kind_t *type_out) {
if (index_out == NULL || type_out == NULL) {
return kDifBadArg;
}
switch (dt_pad_type(pad)) {
case kDtPadTypeMio:
*type_out = kDifPinmuxPadKindMio;
*index_out = dt_pad_mio_pad(pad);
return kDifOk;
case kDtPadTypeDio:
*type_out = kDifPinmuxPadKindDio;
*index_out = dt_pad_dio_pad(pad);
return kDifOk;
default:
return kDifBadArg;
}
}


dif_result_t dif_pinmux_pad_write_attrs_dt(const dif_pinmux_t *pinmux,
dt_pad_t pad,
dif_pinmux_pad_attr_t attrs_in,
dif_pinmux_pad_attr_t *attrs_out) {
dif_pinmux_index_t index;
dif_pinmux_pad_kind_t type;
dif_result_t res = dif_pinmux_pad_from_dt_pad(pad, &index, &type);
if (res == kDifOk) {
res = dif_pinmux_pad_write_attrs(pinmux, index, type, attrs_in, attrs_out);
}
return res;
}

dif_result_t dif_pinmux_pad_get_attrs(const dif_pinmux_t *pinmux,
dif_pinmux_index_t pad,
dif_pinmux_pad_kind_t type,
Expand Down
27 changes: 27 additions & 0 deletions sw/device/lib/dif/dif_pinmux.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,21 @@ OT_WARN_UNUSED_RESULT
dif_result_t dif_pinmux_mio_select_output(const dif_pinmux_t *pinmux,
dt_pad_t pad, dt_pin_t pin);

/** Convert a `dt_pad_t` into a pair (`dif_pinmux_index_t`, `dif_pinmux_pad_kind_t`)
* that can be used for pad control functions (`dif_pinmux_pad_*` functions).
*
* This function is there to handle legacy calls to various pad functions.
*
* @param pad A pad.
* @param index_out[out] The index of this pad.
* @param type_out[out] The type of pad.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_pinmux_pad_from_dt_pad(dt_pad_t pad,
dif_pinmux_index_t *index_out,
dif_pinmux_pad_kind_t *type_out);

/**
* Writes attributes for a pad.
*
Expand Down Expand Up @@ -400,6 +415,18 @@ dif_result_t dif_pinmux_pad_write_attrs(const dif_pinmux_t *pinmux,
dif_pinmux_pad_attr_t attrs_in,
dif_pinmux_pad_attr_t *attrs_out);

/**
* Write attributes for a pad.
*
* See `dif_pinmux_pad_write_attrs` except that the pad is represented by
* a `dt_pad_t` instead of a pair (index,type).
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_pinmux_pad_write_attrs_dt(const dif_pinmux_t *pinmux,
dt_pad_t pad,
dif_pinmux_pad_attr_t attrs_in,
dif_pinmux_pad_attr_t *attrs_out);

/**
* Get attributes for a pad.
*
Expand Down
4 changes: 2 additions & 2 deletions sw/device/lib/dif/dif_rstmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "rstmgr_regs.h" // Generated.

// These assertions are only defined for the Earl Grey chip.
#if !OT_IS_ENGLISH_BREAKFAST
#ifdef OPENTITAN_IS_EARLGREY
// This macro simplifies the `static_assert` check to make sure that the
// public reset info register bitfield matches register bits.
#define RSTMGR_RESET_INFO_CHECK(pub_name, priv_name) \
Expand Down Expand Up @@ -53,7 +53,7 @@ static_assert(
static_assert(
DIF_RSTMGR_ALERT_INFO_MAX_SIZE == RSTMGR_ALERT_INFO_CTRL_INDEX_MASK,
"Alert info dump max size has grown, please update the public define!");
#endif // !OT_IS_ENGLISH_BREAKFAST
#endif // OPENTITAN_IS_EARLGREY

/**
* Checks whether alert_info capture is disabled.
Expand Down
Loading

0 comments on commit 2fa6c63

Please sign in to comment.