Skip to content
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
43 changes: 40 additions & 3 deletions hw/top/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load(
"//rules:autogen.bzl",
"opentitan_ip_c_header",
"opentitan_ip_rust_header",
"opentitan_ip_rust_module",
)
load("//rules/opentitan:hw.bzl", "get_top_attr")
load(
Expand All @@ -19,6 +19,7 @@ load(
"opentitan_alias_top_attr",
"opentitan_if_ip",
)
load("@rules_rust//rust:defs.bzl", "rust_library")

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

Expand Down Expand Up @@ -126,15 +127,51 @@ alias(
for ip in ALL_IP_NAMES
]

# Rust register headers for all IPs.
# Rust register modules for all IPs.
[
opentitan_ip_rust_header(
opentitan_ip_rust_module(
name = "{}_rust_regs".format(ip),
ip = ip,
kind = "generic",
)
for ip in ALL_IP_NAMES
]

# `lib.rs` file defining a module for each IP.
genrule(
name = "ot_regs_lib_rs",
srcs = [],
outs = ["lib.rs"],
cmd = "echo '{}' > \"$@\"".format(
"\n".join([
'#[cfg(feature = "{ip}")]\npub mod {ip};'.format(ip = ip)
for ip in ALL_IP_NAMES
]),
),
visibility = ["//visibility:private"],
)

# Crate with a module for the registers of each IP.
rust_library(
name = "ot_regs",
srcs = [":ot_regs_lib_rs"] + flatten([
opentitan_if_ip(
ip,
[":{}_rust_regs".format(ip)],
[],
)
for ip in ALL_IP_NAMES
]),
crate_features = flatten([
opentitan_if_ip(
ip,
[ip],
[],
)
for ip in ALL_IP_NAMES
]),
)

cc_library(
name = "doxy_target",
deps = [
Expand Down
3 changes: 2 additions & 1 deletion hw/top/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ Furthermore, adding `//hw/top:top_lib` or `//hw/top:top_ld` as a dependency of a

The headers for every IP are exposed as:
- `//hw/top:<ip>_c_regs` for the C registers,
- `//hw/top:<ip>_rust_regs` for the rust registers.
- `//hw/top:<ip>_rust_regs` for the Rust registers,
- `//hw/top/tock:<ip>_regs` for the Rust registers for Tock.

[**Compatibility annotations**](#compatibility-annotations): those targets are only marked as compatible with the tops that contain at least an instance of them.

Expand Down
18 changes: 18 additions & 0 deletions hw/top/tock/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright lowRISC contributors (OpenTitan project).
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

load("//rules:autogen.bzl", "opentitan_ip_rust_module")
load("//hw/top:defs.bzl", "ALL_IP_NAMES")

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

# Rust Tock register modules for all IPs.
[
opentitan_ip_rust_module(
name = "{}_regs".format(ip),
ip = ip,
kind = "tock",
)
for ip in ALL_IP_NAMES
]
39 changes: 26 additions & 13 deletions rules/autogen.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -98,40 +98,52 @@ def opentitan_ip_c_header(name, ip, target_compatible_with = [], **kwargs):
**kwargs
)

def _opentitan_ip_rust_header_impl(ctx):
tock = ctx.actions.declare_file("{}.rs".format(ctx.attr.ip))
def _opentitan_ip_rust_module_impl(ctx):
module = ctx.actions.declare_file("{}.rs".format(ctx.attr.ip))

stamp_args = []
stamp_files = []
if stamping_enabled(ctx):
stamp_files = [ctx.version_file]
stamp_args.append("--version-stamp={}".format(ctx.version_file.path))

if ctx.attr.kind == "generic":
kind_flag = "--rust"
elif ctx.attr.kind == "tock":
kind_flag = "--tock"
else:
fail("unhandled kind '{}'".format(ctx.attr.kind))

ctx.actions.run(
outputs = [tock],
outputs = [module],
inputs = [ctx.file.hjson] + stamp_files,
arguments = [
"--tock",
kind_flag,
"-q",
"-o",
tock.path,
module.path,
] + stamp_args + [ctx.file.hjson.path],
executable = ctx.executable._regtool,
)

return [
DefaultInfo(files = depset([tock])),
DefaultInfo(files = depset([module])),
OutputGroupInfo(
tock = depset([tock]),
module = depset([module]),
),
]

opentitan_ip_rust_header_rule = rule(
implementation = _opentitan_ip_rust_header_impl,
doc = "Generate the Rust headers for an IP block as used in a top",
opentitan_ip_rust_module_rule = rule(
implementation = _opentitan_ip_rust_module_impl,
doc = "Generate the Rust modules for an IP block as used in a top",
attrs = {
"hjson": attr.label(allow_single_file = True, doc = "Hjson description of the IP"),
"ip": attr.string(doc = "Name of the IP block"),
"kind": attr.string(
doc = "Kind of Rust module to generate (generic or for Tock)",
default = "generic",
values = ["generic", "tock"],
),
"_regtool": attr.label(
default = "//util:regtool",
executable = True,
Expand All @@ -140,14 +152,15 @@ opentitan_ip_rust_header_rule = rule(
} | stamp_attr(-1, "//rules:stamp_flag"),
)

def opentitan_ip_rust_header(name, ip, target_compatible_with = [], **kwargs):
def opentitan_ip_rust_module(name, ip, kind, target_compatible_with = [], **kwargs):
"""
Macro around `opentitan_ip_rust_header_rule` that automatically sets `hjson` for the current top.
Macro around `opentitan_ip_rust_module_rule` that automatically sets `hjson` for the current top.
The target will also be marked as compatible only with tops containing this IP.
"""
opentitan_ip_rust_header_rule(
opentitan_ip_rust_module_rule(
name = name,
ip = ip,
kind = kind,
hjson = opentitan_select_ip_attr(ip, "hjson"),
target_compatible_with = target_compatible_with + opentitan_require_ip_attr(ip, "hjson"),
**kwargs
Expand Down
70 changes: 35 additions & 35 deletions sw/device/tock/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,48 @@ package(default_visibility = ["//visibility:public"])
filegroup(
name = "lowrisc_defs",
srcs = [
"//hw/top:adc_ctrl_rust_regs",
"//hw/top:aes_rust_regs",
"//hw/top:aon_timer_rust_regs",
"//hw/top:csrng_rust_regs",
"//hw/top:edn_rust_regs",
"//hw/top:entropy_src_rust_regs",
"//hw/top:gpio_rust_regs",
"//hw/top:hmac_rust_regs",
"//hw/top:i2c_rust_regs",
"//hw/top:keymgr_rust_regs",
"//hw/top:kmac_rust_regs",
"//hw/top:lc_ctrl_rust_regs",
"//hw/top:otbn_rust_regs",
"//hw/top:otp_ctrl_rust_regs",
"//hw/top:pattgen_rust_regs",
"//hw/top:pwm_rust_regs",
"//hw/top:rom_ctrl_rust_regs",
"//hw/top:rv_core_ibex_rust_regs",
"//hw/top:rv_timer_rust_regs",
"//hw/top:spi_device_rust_regs",
"//hw/top:spi_host_rust_regs",
"//hw/top:sram_ctrl_rust_regs",
"//hw/top:sysrst_ctrl_rust_regs",
"//hw/top:uart_rust_regs",
"//hw/top:usbdev_rust_regs",
"//hw/top/tock:adc_ctrl_regs",
"//hw/top/tock:aes_regs",
"//hw/top/tock:aon_timer_regs",
"//hw/top/tock:csrng_regs",
"//hw/top/tock:edn_regs",
"//hw/top/tock:entropy_src_regs",
"//hw/top/tock:gpio_regs",
"//hw/top/tock:hmac_regs",
"//hw/top/tock:i2c_regs",
"//hw/top/tock:keymgr_regs",
"//hw/top/tock:kmac_regs",
"//hw/top/tock:lc_ctrl_regs",
"//hw/top/tock:otbn_regs",
"//hw/top/tock:otp_ctrl_regs",
"//hw/top/tock:pattgen_regs",
"//hw/top/tock:pwm_regs",
"//hw/top/tock:rom_ctrl_regs",
"//hw/top/tock:rv_core_ibex_regs",
"//hw/top/tock:rv_timer_regs",
"//hw/top/tock:spi_device_regs",
"//hw/top/tock:spi_host_regs",
"//hw/top/tock:sram_ctrl_regs",
"//hw/top/tock:sysrst_ctrl_regs",
"//hw/top/tock:uart_regs",
"//hw/top/tock:usbdev_regs",
],
output_group = "tock",
)

filegroup(
name = "earlgrey_defs",
srcs = [
"//hw/top:alert_handler_rust_regs",
"//hw/top:ast_rust_regs",
"//hw/top:clkmgr_rust_regs",
"//hw/top:flash_ctrl_rust_regs",
"//hw/top:otp_ctrl_rust_regs",
"//hw/top:pinmux_rust_regs",
"//hw/top:pwrmgr_rust_regs",
"//hw/top:rstmgr_rust_regs",
"//hw/top:rv_plic_rust_regs",
"//hw/top:sensor_ctrl_rust_regs",
"//hw/top/tock:alert_handler_regs",
"//hw/top/tock:ast_regs",
"//hw/top/tock:clkmgr_regs",
"//hw/top/tock:flash_ctrl_regs",
"//hw/top/tock:otp_ctrl_regs",
"//hw/top/tock:pinmux_regs",
"//hw/top/tock:pwrmgr_regs",
"//hw/top/tock:rstmgr_regs",
"//hw/top/tock:rv_plic_regs",
"//hw/top/tock:sensor_ctrl_regs",
],
output_group = "tock",
)
Expand Down
3 changes: 2 additions & 1 deletion util/reggen/gen_rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ def gen_const_register(outstr: TextIO, reg: Register, comp: str, width: int,
rname = reg.name
offset = reg.offset

genout(outstr, format_comment(first_line(reg.desc)))
if reg.desc:
genout(outstr, format_comment(first_line(reg.desc)))
defname = as_define(comp + '_' + rname)
gen_const(outstr, defname, 'REG_OFFSET', offset, existing_defines, True)

Expand Down
Loading