Skip to content

Commit

Permalink
[tools,gen-otp-mmap] Make this compatible with otp_ctrl ipgen
Browse files Browse the repository at this point in the history
This tool may become obsolete, but it is best to upgrade it per
the ipgen flow.
Comment out dif generation, and add a TODO with an explicit issue.

Part of lowRISC#25019

Signed-off-by: Guillermo Maturana <[email protected]>
  • Loading branch information
matutem committed Jan 5, 2025
1 parent 141213e commit b291777
Showing 1 changed file with 100 additions and 94 deletions.
194 changes: 100 additions & 94 deletions util/design/gen-otp-mmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from pathlib import Path
from typing import Dict

import hjson
from mako import exceptions
from mako.template import Template

Expand All @@ -35,30 +34,37 @@
# memory map source
MMAP_DEFINITION_FILE = "hw/top_earlgrey/data/otp/otp_ctrl_mmap.hjson"
# documentation tables to generate
PARTITIONS_TABLE_FILE = "hw/ip/otp_ctrl/doc/otp_ctrl_partitions.md"
DIGESTS_TABLE_FILE = "hw/ip/otp_ctrl/doc/otp_ctrl_digests.md"
MMAP_TABLE_FILE = "hw/ip/otp_ctrl/doc/otp_ctrl_mmap.md"
DESC_TABLE_FILE = "hw/ip/otp_ctrl/doc/otp_ctrl_field_descriptions.md"
PARTITIONS_TABLE_FILE = Path("doc") / "otp_ctrl_partitions.md"
DIGESTS_TABLE_FILE = Path("doc") / "otp_ctrl_digests.md"
MMAP_TABLE_FILE = Path("doc") / "otp_ctrl_mmap.md"
DESC_TABLE_FILE = Path("doc") / "otp_ctrl_field_descriptions.md"

# code templates to render
COV_TEMPLATES = ["hw/ip/otp_ctrl/data/otp_ctrl_cov_bind.sv.tpl"]
DATA_TEMPLATES = ["hw/ip/otp_ctrl/data/otp_ctrl.hjson.tpl"]
TEMPLATES_PATH = Path("hw") / "ip_templates" / "otp_ctrl"
DATA_TEMPLATES_PATH = TEMPLATES_PATH / "data"
DV_TEMPLATES_PATH = TEMPLATES_PATH / "dv"
COV_TEMPLATES_PATH = DV_TEMPLATES_PATH / "cov"
ENV_TEMPLATES_PATH = DV_TEMPLATES_PATH / "env"
SEQ_LIB_TEMPLATES_PATH = ENV_TEMPLATES_PATH / "seq_lib"
COV_TEMPLATES = [COV_TEMPLATES_PATH / "otp_ctrl_cov_bind.sv.tpl"]
DATA_TEMPLATES = [DATA_TEMPLATES_PATH / "otp_ctrl.hjson.tpl"]
DIF_TEMPLATES = [
"hw/ip/otp_ctrl/data/dif_otp_ctrl.c.tpl",
"hw/ip/otp_ctrl/data/dif_otp_ctrl.h.tpl",
"hw/ip/otp_ctrl/data/dif_otp_ctrl_unittest.cc.tpl"
DATA_TEMPLATES_PATH / "dif_otp_ctrl.c.tpl",
DATA_TEMPLATES_PATH / "dif_otp_ctrl.h.tpl",
DATA_TEMPLATES_PATH / "dif_otp_ctrl_unittest.cc.tpl"
]
ENV_TEMPLATES = [
"hw/ip/otp_ctrl/data/otp_ctrl_env_cov.sv.tpl",
"hw/ip/otp_ctrl/data/otp_ctrl_env_pkg.sv.tpl",
"hw/ip/otp_ctrl/data/otp_ctrl_if.sv.tpl",
"hw/ip/otp_ctrl/data/otp_ctrl_scoreboard.sv.tpl"
ENV_TEMPLATES_PATH / "otp_ctrl_env_cov.sv.tpl",
ENV_TEMPLATES_PATH / "otp_ctrl_env_pkg.sv.tpl",
ENV_TEMPLATES_PATH / "otp_ctrl_if.sv.tpl",
ENV_TEMPLATES_PATH / "otp_ctrl_scoreboard.sv.tpl"
]
RTL_TEMPLATES = ["hw/ip/otp_ctrl/data/otp_ctrl_part_pkg.sv.tpl"]
RTL_TEMPLATES_PATH = TEMPLATES_PATH / "rtl"
RTL_TEMPLATES = [RTL_TEMPLATES_PATH / "otp_ctrl_part_pkg.sv.tpl"]
SEQ_TEMPLATES = [
"hw/ip/otp_ctrl/data/otp_ctrl_base_vseq.sv.tpl",
"hw/ip/otp_ctrl/data/otp_ctrl_dai_lock_vseq.sv.tpl",
"hw/ip/otp_ctrl/data/otp_ctrl_smoke_vseq.sv.tpl"
SEQ_LIB_TEMPLATES_PATH / "otp_ctrl_base_vseq.sv.tpl",
SEQ_LIB_TEMPLATES_PATH / "otp_ctrl_dai_lock_vseq.sv.tpl",
SEQ_LIB_TEMPLATES_PATH / "otp_ctrl_smoke_vseq.sv.tpl"
]


Expand All @@ -69,17 +75,30 @@ def check_in_repo_top():
exit(1)


def render_template(template: str, target_path: Path, otp_mmap: Dict,
gen_comment: str):
with open(template, 'r') as tplfile:
tpl = Template(tplfile.read())
try:
expansion = tpl.render(otp_mmap=otp_mmap, gen_comment=gen_comment)
except exceptions.MakoException:
log.error(exceptions.text_error_template().render())
def render_template(template: str, target_path: Path, params: Dict[str, object]):
try:
tpl = Template(filename=str(template))
except OSError as e:
log.error(f"Error creating template: {e}")
exit(1)

try:
expansion = tpl.render(**params)
except exceptions.MakoException:
log.error(exceptions.text_error_template().render())
exit(1)

try:
with target_path.open(mode='w', encoding='UTF-8') as outfile:
outfile.write(expansion)
except OSError as e:
log.error(f"Error rendering template: {e}")
exit(1)


with target_path.open(mode='w', encoding='UTF-8') as outfile:
outfile.write(expansion)
def _target_from_template_path(output_path: Path, template: Path) -> Path:
full_path = output_path / template.relative_to(TEMPLATES_PATH)
return Path(full_path.parents[0]) / full_path.stem


def main():
Expand All @@ -96,77 +115,64 @@ def main():
metavar='<seed>',
help='Custom seed for RNG to compute default values.')

parser.add_argument('--topname',
default='earlgrey',
help='The topname, as in earlgrey of darjeeling.')
args = parser.parse_args()

# The placement of sw difs requires this be run from repo_top.
check_in_repo_top()

with open(MMAP_DEFINITION_FILE, 'r') as infile:
config = hjson.load(infile)

# If specified, override the seed for random netlist constant computation.
if args.seed:
log.warning('Commandline override of seed with {}.'.format(
args.seed))
config['seed'] = args.seed
# Otherwise we make sure a seed exists in the HJSON config file.
elif 'seed' not in config:
log.error('Seed not found in configuration HJSON.')
exit(1)

try:
otp_mmap = OtpMemMap(config)
except RuntimeError as err:
log.error(err)
exit(1)

with open(PARTITIONS_TABLE_FILE, 'wb', buffering=2097152) as outfile:
outfile.write(TABLE_HEADER_COMMENT.encode('utf-8'))
outfile.write(otp_mmap.create_partitions_table().encode('utf-8'))
outfile.write('\n'.encode('utf-8'))

with open(DIGESTS_TABLE_FILE, 'wb', buffering=2097152) as outfile:
outfile.write(TABLE_HEADER_COMMENT.encode('utf-8'))
outfile.write(otp_mmap.create_digests_table().encode('utf-8'))
outfile.write('\n'.encode('utf-8'))

with open(MMAP_TABLE_FILE, 'wb', buffering=2097152) as outfile:
outfile.write(TABLE_HEADER_COMMENT.encode('utf-8'))
outfile.write(otp_mmap.create_mmap_table().encode('utf-8'))
outfile.write('\n'.encode('utf-8'))

with open(DESC_TABLE_FILE, 'wb', buffering=2097152) as outfile:
outfile.write(TABLE_HEADER_COMMENT.encode('utf-8'))
outfile.write(otp_mmap.create_description_table().encode('utf-8'))
outfile.write('\n'.encode('utf-8'))

# render all templates
for template in COV_TEMPLATES:
stem_path = Path(template).stem
target_path = Path(template).parents[1] / "dv" / "cov" / stem_path
render_template(template, target_path, otp_mmap, TPL_GEN_COMMENT)
for template in DATA_TEMPLATES:
stem_path = Path(template).stem
target_path = Path(template).parent / stem_path
render_template(template, target_path, otp_mmap, TPL_GEN_COMMENT)
for template in DIF_TEMPLATES:
stem_path = Path(template).stem
target_path = (Path.cwd() / "sw" / "device" / "lib" / "dif" /
stem_path)
render_template(template, target_path, otp_mmap, TPL_GEN_COMMENT)
for template in ENV_TEMPLATES:
stem_path = Path(template).stem
target_path = Path(template).parents[1] / "dv" / "env" / stem_path
render_template(template, target_path, otp_mmap, TPL_GEN_COMMENT)
for template in RTL_TEMPLATES:
stem_path = Path(template).stem
target_path = Path(template).parents[1] / "rtl" / stem_path
render_template(template, target_path, otp_mmap, TPL_GEN_COMMENT)
for template in SEQ_TEMPLATES:
stem_path = Path(template).stem
target_path = (Path(template).parents[1] / "dv" / "env" /
"seq_lib" / stem_path)
render_template(template, target_path, otp_mmap, TPL_GEN_COMMENT)
otp_mmap = OtpMemMap.from_mmap_path(
MMAP_DEFINITION_FILE.replace('earlgrey', args.topname), args.seed)

output_path = (Path("hw") / f"top_{args.topname}" / "ip_autogen" /
"otp_ctrl")
with open(output_path / PARTITIONS_TABLE_FILE, 'wb', buffering=2097152) as outfile:
outfile.write(TABLE_HEADER_COMMENT.encode('utf-8'))
outfile.write(otp_mmap.create_partitions_table().encode('utf-8'))
outfile.write('\n'.encode('utf-8'))

with open(output_path / DIGESTS_TABLE_FILE, 'wb', buffering=2097152) as outfile:
outfile.write(TABLE_HEADER_COMMENT.encode('utf-8'))
outfile.write(otp_mmap.create_digests_table().encode('utf-8'))
outfile.write('\n'.encode('utf-8'))

with open(output_path / MMAP_TABLE_FILE, 'wb', buffering=2097152) as outfile:
outfile.write(TABLE_HEADER_COMMENT.encode('utf-8'))
outfile.write(otp_mmap.create_mmap_table().encode('utf-8'))
outfile.write('\n'.encode('utf-8'))

with open(output_path / DESC_TABLE_FILE, 'wb', buffering=2097152) as outfile:
outfile.write(TABLE_HEADER_COMMENT.encode('utf-8'))
outfile.write(otp_mmap.create_description_table().encode('utf-8'))
outfile.write('\n'.encode('utf-8'))

# render all templates
params = {"otp_mmap": otp_mmap.config, "gen_comment": TPL_GEN_COMMENT,
"topname": args.topname}
for template in COV_TEMPLATES:
target_path = _target_from_template_path(output_path, template)
render_template(template, target_path, params)
for template in DATA_TEMPLATES:
target_path = _target_from_template_path(output_path, template)
render_template(template, target_path, params)
# This won't work correctly until top-specific difs are created.
# Comment it out for now.
# TODO(lowrisc/opentitan#25743): Enable this once multi-top deals with top-
# specific difs.
# for template in DIF_TEMPLATES:
# target_path = Path.cwd() / "sw" / "device" / "lib" / "dif" / template.stem
# render_template(template, target_path, params)
for template in ENV_TEMPLATES:
target_path = _target_from_template_path(output_path, template)
render_template(template, target_path, params)
for template in RTL_TEMPLATES:
target_path = _target_from_template_path(output_path, template)
render_template(template, target_path, params)
for template in SEQ_TEMPLATES:
target_path = _target_from_template_path(output_path, template)
render_template(template, target_path, params)


if __name__ == "__main__":
Expand Down

0 comments on commit b291777

Please sign in to comment.