Skip to content

Commit

Permalink
[manuf] make OTP image consts a link-time dep
Browse files Browse the repository at this point in the history
This updates the structure of the auto-generated OTP image key-value
contants header file (making it a C file now), and the `individualize_preop`
provisioning library to inject such contants as link-time dependency to
allow for easily adding (future) OTP SW configuration constants for
future OpenTitan SKUs.

Signed-off-by: Tim Trippel <[email protected]>
  • Loading branch information
timothytrippel committed Aug 9, 2023
1 parent 5447f88 commit d35c795
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@ ${fileheader}
// AUTOGENERATED. Do not edit this file by hand.
// See the hw/ip/otp_ctrl/data README for details.

#ifndef ${include_guard}
#define ${include_guard}
#include <stddef.h>

// See the following include file for details on the types used in this header
// file.
// See the following include file for details on the types used in this file.
#include "sw/device/silicon_creator/manuf/lib/otp_img_types.h"

#include "otp_ctrl_regs.h"

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#include "otp_ctrl_regs.h" // Generated.

<%
def ToPascalCase(in_str):
Expand Down Expand Up @@ -123,7 +117,8 @@ ${ConstTypeDefinition(item, data[partition_name]["alignment"])}
alignment = data[partition_name]["alignment"]
%>
// Partition ${partition_name}
static otp_kv_t ${"kOtpKv" + ToPascalCase(partition_name)}[] = {
const size_t ${"kOtpKv" + ToPascalCase(partition_name) + "Size"} = ${len(data[partition_name]["items"])};
const otp_kv_t ${"kOtpKv" + ToPascalCase(partition_name)}[] = {
% for item in data[partition_name]["items"]:
{
.type = ${ToOtpValType(alignment)},
Expand All @@ -134,11 +129,3 @@ static otp_kv_t ${"kOtpKv" + ToPascalCase(partition_name)}[] = {
% endfor
};
% endfor


#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

#endif // ${include_guard}

15 changes: 12 additions & 3 deletions hw/ip/otp_ctrl/data/sku_earlgrey_a0/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

load("//rules:otp.bzl", "otp_image_header", "otp_json")
load("//rules:otp.bzl", "otp_image_consts", "otp_json")

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

Expand All @@ -12,12 +12,21 @@ otp_json(
seed = "94259314771464387",
)

otp_image_header(
name = "otp_img_sku_earlgrey_a0_stage_individualize",
otp_image_consts(
name = "otp_img_consts_c_file",
src = ":otp_json_baseline",
overlays = [
"//hw/ip/otp_ctrl/data:otp_json_creator_sw_cfg",
"//hw/ip/otp_ctrl/data:otp_json_owner_sw_cfg",
"//hw/ip/otp_ctrl/data:otp_json_alert_digest_cfg",
],
)

cc_library(
name = "otp_img_consts",
srcs = [":otp_img_consts_c_file"],
deps = [
"//hw/ip/otp_ctrl/data:otp_ctrl_regs",
"//sw/device/silicon_creator/manuf/lib:otp_img_types",
],
)
28 changes: 10 additions & 18 deletions rules/otp.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ format expected by the image generation tool.
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@bazel_skylib//lib:new_sets.bzl", "sets")
load("//rules:host.bzl", "host_tools_transition")
load("//rules:const.bzl", "CONST", "hex", "hex_digits")
load("//rules:const.bzl", "CONST", "hex")

def get_otp_images():
"""Returns a list of (otp_name, img_target) tuples.
Expand Down Expand Up @@ -246,8 +246,8 @@ otp_image = rule(
},
)

def _otp_image_header(ctx):
output = ctx.actions.declare_file(ctx.attr.name + ".h")
def _otp_image_consts_impl(ctx):
output = ctx.actions.declare_file(ctx.attr.name + ".c")
args = ctx.actions.args()
if not ctx.attr.verbose:
args.add("--quiet")
Expand All @@ -257,34 +257,26 @@ def _otp_image_header(ctx):
args.add("--lc-seed", ctx.attr.lc_seed[BuildSettingInfo].value)
args.add("--otp-seed", ctx.attr.otp_seed[BuildSettingInfo].value)
args.add("--img-cfg", ctx.file.src)
args.add("--header-template", ctx.file.header_template)
args.add("--header-out", "{}/{}.h".format(output.dirname, ctx.attr.name))
args.add("--c-template", ctx.file.c_template)
args.add("--c-out", "{}/{}.c".format(output.dirname, ctx.attr.name))
args.add_all(ctx.files.overlays, before_each = "--add-cfg")
ctx.actions.run(
outputs = [output],
inputs = [
ctx.file.src,
ctx.file.header_template,
ctx.file.c_template,
ctx.file.lc_state_def,
ctx.file.mmap_def,
] + ctx.files.overlays,
arguments = [args],
executable = ctx.executable._tool,
)
return [
CcInfo(compilation_context = cc_common.create_compilation_context(
includes = depset([output.dirname]),
headers = depset([output]),
defines = depset(["RULE_NAME=\"{}\"".format(ctx.label.name)]),
)),
DefaultInfo(files = depset([output]), runfiles = ctx.runfiles(files = [output])),
OutputGroupInfo(
header = depset([output]),
),
]

otp_image_header = rule(
implementation = _otp_image_header,
otp_image_consts = rule(
implementation = _otp_image_consts_impl,
attrs = {
"src": attr.label(
allow_single_file = [".json", ".hjson"],
Expand Down Expand Up @@ -316,9 +308,9 @@ otp_image_header = rule(
default = "//hw/ip/otp_ctrl/data:otp_seed",
doc = "Configuration override seed used to randomize OTP netlist constants.",
),
"header_template": attr.label(
"c_template": attr.label(
allow_single_file = True,
default = "//hw/ip/otp_ctrl/data:otp_ctrl_img.h.tpl",
default = "//hw/ip/otp_ctrl/data:otp_ctrl_img.c.tpl",
doc = "OTP image header template.",
),
"verbose": attr.bool(
Expand Down
20 changes: 15 additions & 5 deletions sw/device/silicon_creator/manuf/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,31 @@ opentitan_functest(
)

cc_library(
name = "individualize_preop",
srcs = ["individualize_preop.c"],
hdrs = [
name = "individualize_preop_generic",
srcs = [
"individualize_preop.c",
"individualize_preop.h",
],
deps = [
":otp_img",
"//hw/ip/otp_ctrl/data/sku_earlgrey_a0:otp_img_sku_earlgrey_a0_stage_individualize",
":otp_img_types",
"//sw/device/lib/base:status",
"//sw/device/lib/dif:otp_ctrl",
"//sw/device/lib/testing:lc_ctrl_testutils",
"//sw/device/lib/testing:otp_ctrl_testutils",
],
)

# As more SKUs are created with different OTP software configuration partitions,
# libraries can be added accordingly.
cc_library(
name = "individualize_preop_sku_earlgrey_a0",
deps = [
":individualize_preop_generic",
"//hw/ip/otp_ctrl/data/sku_earlgrey_a0:otp_img_consts",
],
)

opentitan_functest(
name = "individualize_preop_functest",
srcs = ["individualize_preop_functest.c"],
Expand All @@ -132,7 +142,7 @@ opentitan_functest(
"cw310_rom_with_fake_keys",
],
deps = [
":individualize_preop",
":individualize_preop_sku_earlgrey_a0",
"//hw/ip/otp_ctrl/data:otp_ctrl_regs",
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
"//sw/device/lib/base:status",
Expand Down
6 changes: 3 additions & 3 deletions sw/device/silicon_creator/manuf/lib/individualize_preop.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

#include "sw/device/silicon_creator/manuf/lib/individualize_preop.h"

#include "otp_img_sku_earlgrey_a0_stage_individualize.h" // Generated.
#include "sw/device/lib/dif/dif_otp_ctrl.h"
#include "sw/device/silicon_creator/manuf/lib/otp_img.h"
#include "sw/device/silicon_creator/manuf/lib/otp_img_types.h"

status_t manuf_individualize_device_sw_cfg(const dif_otp_ctrl_t *otp_ctrl) {
TRY(otp_img_write(otp_ctrl, kDifOtpCtrlPartitionCreatorSwCfg,
kOtpKvCreatorSwCfg, ARRAYSIZE(kOtpKvCreatorSwCfg)));
kOtpKvCreatorSwCfg, kOtpKvCreatorSwCfgSize));
TRY(otp_img_write(otp_ctrl, kDifOtpCtrlPartitionOwnerSwCfg, kOtpKvOwnerSwCfg,
ARRAYSIZE(kOtpKvOwnerSwCfg)));
kOtpKvOwnerSwCfgSize));
return OK_STATUS();
}
13 changes: 13 additions & 0 deletions sw/device/silicon_creator/manuf/lib/individualize_preop.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@

#include "sw/device/lib/base/status.h"
#include "sw/device/lib/dif/dif_otp_ctrl.h"
#include "sw/device/silicon_creator/manuf/lib/otp_img.h"

/**
* OTP Creator Software Configuration Partition.
*/
extern const size_t kOtpKvCreatorSwCfgSize;
extern const otp_kv_t kOtpKvCreatorSwCfg[];

/**
* OTP Owner Software Configuration Partition.
*/
extern const size_t kOtpKvOwnerSwCfgSize;
extern const otp_kv_t kOtpKvOwnerSwCfg[];

/**
* Configures the CREATOR_SW_CFG and OWNER_SW_CFG OTP partitions.
Expand Down
2 changes: 1 addition & 1 deletion sw/device/silicon_creator/manuf/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ opentitan_ram_binary(
"//sw/device/lib/testing:pinmux_testutils",
"//sw/device/lib/testing/test_framework:check",
"//sw/device/lib/testing/test_framework:status",
"//sw/device/silicon_creator/manuf/lib:individualize_preop",
"//sw/device/silicon_creator/manuf/lib:individualize_preop_sku_earlgrey_a0",
"//sw/device/silicon_creator/manuf/lib:isolated_flash_partition",
"//sw/device/silicon_creator/manuf/lib:sram_program_linker_script",
"//sw/device/silicon_creator/manuf/lib:sram_start",
Expand Down
20 changes: 9 additions & 11 deletions util/design/gen-otp-img.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,18 @@ def main():
The mapping must be bijective - otherwise this will generate
an error.
''')
parser.add_argument('--header-template',
parser.add_argument('--c-template',
type=Path,
metavar='<path>',
help='''
Template file used to generate C header version of the OTP image.
This flag is only required when --header-out is set.
Template file used to generate C version of the OTP image.
This flag is only required when --c-out is set.
''')
parser.add_argument('--header-out',
parser.add_argument('--c-out',
type=Path,
metavar='<path>',
help='''
C header output path. Requires the --header-template flag to be
C output path. Requires the --c-template flag to be
set. The --out flag is ignored when this flag is set.
''')

Expand Down Expand Up @@ -280,12 +280,10 @@ def main():
file_header = '// Generated on {} with\n// $ gen-otp-img.py {}\n//\n'.format(
dtstr, argstr)

if args.header_out:
log.info(f'Generating header file: {args.header_out}')
file_body = otp_mem_img.generate_headerfile(args.header_out,
file_header,
args.header_template)
with open(args.header_out, 'wb') as outfile:
if args.c_out:
log.info(f'Generating C file: {args.c_out}')
file_body = otp_mem_img.generate_c_file(file_header, args.c_template)
with open(args.c_out, 'wb') as outfile:
outfile.write(file_body.encode('utf-8'))
exit(0)

Expand Down
12 changes: 3 additions & 9 deletions util/design/lib/OtpMemImg.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,12 +529,10 @@ def streamout_memfile(self) -> Tuple[str, int]:
return _to_memfile_with_ecc(data, annotation, self.lc_state.config,
self.data_perm)

def generate_headerfile(self, outfile: str, fileheader: str,
templatefile: Path) -> str:
'''Generates header file with provided `header` and `templatefile`.
def generate_c_file(self, fileheader: str, templatefile: Path) -> str:
'''Generates C file with provided `header` and `templatefile`.
Args:
outfile: Output file path. Used to generate include guards.
fileheader: Header to be appended to autogenerated file.
templatefile: Mako template used to generate header file.
Returns:
Expand Down Expand Up @@ -573,12 +571,8 @@ def generate_headerfile(self, outfile: str, fileheader: str,
'items': items,
}

include_guard = common.path_to_include_guard(outfile)

with open(templatefile, 'r') as tplfile:
tpl = Template(tplfile.read())
result = tpl.render(include_guard=include_guard,
fileheader=fileheader,
data=data)
result = tpl.render(fileheader=fileheader, data=data)

return result

0 comments on commit d35c795

Please sign in to comment.