Software APIs
dif_otbn.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
6 
7 #include <assert.h>
8 
12 
13 #include "otbn_regs.h" // Generated.
14 
15 static_assert(kDifOtbnErrBitsBadDataAddr ==
16  (1 << OTBN_ERR_BITS_BAD_DATA_ADDR_BIT),
17  "Layout of error bits changed.");
18 static_assert(kDifOtbnErrBitsBadInsnAddr ==
19  (1 << OTBN_ERR_BITS_BAD_INSN_ADDR_BIT),
20  "Layout of error bits changed.");
21 static_assert(kDifOtbnErrBitsCallStack == (1 << OTBN_ERR_BITS_CALL_STACK_BIT),
22  "Layout of error bits changed.");
23 static_assert(kDifOtbnErrBitsIllegalInsn ==
24  (1 << OTBN_ERR_BITS_ILLEGAL_INSN_BIT),
25  "Layout of error bits changed.");
26 static_assert(kDifOtbnErrBitsLoop == (1 << OTBN_ERR_BITS_LOOP_BIT),
27  "Layout of error bits changed.");
28 static_assert(kDifOtbnErrBitsImemIntgViolation ==
29  (1 << OTBN_ERR_BITS_IMEM_INTG_VIOLATION_BIT),
30  "Layout of error bits changed.");
31 static_assert(kDifOtbnErrBitsDmemIntgViolation ==
32  (1 << OTBN_ERR_BITS_DMEM_INTG_VIOLATION_BIT),
33  "Layout of error bits changed.");
34 static_assert(kDifOtbnErrBitsRegIntgViolation ==
35  (1 << OTBN_ERR_BITS_REG_INTG_VIOLATION_BIT),
36  "Layout of error bits changed.");
37 static_assert(kDifOtbnErrBitsBusIntgViolation ==
38  (1 << OTBN_ERR_BITS_BUS_INTG_VIOLATION_BIT),
39  "Layout of error bits changed.");
40 static_assert(kDifOtbnErrBitsIllegalBusAccess ==
41  (1 << OTBN_ERR_BITS_ILLEGAL_BUS_ACCESS_BIT),
42  "Layout of error bits changed.");
44  (1 << OTBN_ERR_BITS_LIFECYCLE_ESCALATION_BIT),
45  "Layout of error bits changed.");
46 static_assert(kDifOtbnErrBitsFatalSoftware ==
47  (1 << OTBN_ERR_BITS_FATAL_SOFTWARE_BIT),
48  "Layout of error bits changed.");
49 
50 /**
51  * Data width of big number subset, in bytes.
52  */
53 const int kDifOtbnWlenBytes = 256 / 8;
54 
55 /**
56  * Ensures that `offset` and `size` are valid for a given `mem_size`.
57  *
58  * Valid are 32b word accesses to 32b-aligned memory locations within
59  * `mem_size`.
60  */
61 static bool check_offset_len(uint32_t offset_bytes, size_t len_bytes,
62  size_t mem_size) {
63  // The overflow check below assumes/requires two unsigned inputs.
64  return (len_bytes % sizeof(uint32_t) == 0 &&
65  offset_bytes % sizeof(uint32_t) == 0 &&
66  offset_bytes + len_bytes >= len_bytes &&
67  offset_bytes + len_bytes <= mem_size);
68 }
69 
71  if (otbn == NULL) {
72  return kDifBadArg;
73  }
74 
75  mmio_region_write32(otbn->base_addr, OTBN_INTR_ENABLE_REG_OFFSET, 0);
76 
77  // Clear all pending interrupts.
78  mmio_region_write32(otbn->base_addr, OTBN_INTR_STATE_REG_OFFSET, 0xFFFFFFFF);
79 
80  return kDifOk;
81 }
82 
84  if (otbn == NULL) {
85  return kDifBadArg;
86  }
87 
88  mmio_region_write32(otbn->base_addr, OTBN_CMD_REG_OFFSET, cmd);
89 
90  return kDifOk;
91 }
92 
95  if (otbn == NULL || status == NULL) {
96  return kDifBadArg;
97  }
98 
99  *status = mmio_region_read32(otbn->base_addr, OTBN_STATUS_REG_OFFSET);
100 
101  return kDifOk;
102 }
103 
105  dif_otbn_err_bits_t *err_bits) {
106  if (otbn == NULL || err_bits == NULL) {
107  return kDifBadArg;
108  }
109 
110  uint32_t err_bits_raw =
111  mmio_region_read32(otbn->base_addr, OTBN_ERR_BITS_REG_OFFSET);
112 
113  *err_bits = err_bits_raw;
114  return kDifOk;
115 }
116 
117 dif_result_t dif_otbn_get_insn_cnt(const dif_otbn_t *otbn, uint32_t *insn_cnt) {
118  if (otbn == NULL || insn_cnt == NULL) {
119  return kDifBadArg;
120  }
121 
122  *insn_cnt = mmio_region_read32(otbn->base_addr, OTBN_INSN_CNT_REG_OFFSET);
123  return kDifOk;
124 }
125 
126 dif_result_t dif_otbn_imem_write(const dif_otbn_t *otbn, uint32_t offset_bytes,
127  const void *src, size_t len_bytes) {
128  if (otbn == NULL || src == NULL ||
129  !check_offset_len(offset_bytes, len_bytes, OTBN_IMEM_SIZE_BYTES)) {
130  return kDifBadArg;
131  }
132 
134  otbn->base_addr, OTBN_IMEM_REG_OFFSET + offset_bytes, src, len_bytes);
135 
136  return kDifOk;
137 }
138 
139 dif_result_t dif_otbn_imem_read(const dif_otbn_t *otbn, uint32_t offset_bytes,
140  void *dest, size_t len_bytes) {
141  if (otbn == NULL || dest == NULL ||
142  !check_offset_len(offset_bytes, len_bytes, OTBN_IMEM_SIZE_BYTES)) {
143  return kDifBadArg;
144  }
145 
147  otbn->base_addr, OTBN_IMEM_REG_OFFSET + offset_bytes, dest, len_bytes);
148 
149  return kDifOk;
150 }
151 
152 dif_result_t dif_otbn_dmem_write(const dif_otbn_t *otbn, uint32_t offset_bytes,
153  const void *src, size_t len_bytes) {
154  if (otbn == NULL || src == NULL ||
155  !check_offset_len(offset_bytes, len_bytes, OTBN_DMEM_SIZE_BYTES)) {
156  return kDifBadArg;
157  }
158 
160  otbn->base_addr, OTBN_DMEM_REG_OFFSET + offset_bytes, src, len_bytes);
161 
162  return kDifOk;
163 }
164 
165 dif_result_t dif_otbn_dmem_read(const dif_otbn_t *otbn, uint32_t offset_bytes,
166  void *dest, size_t len_bytes) {
167  if (otbn == NULL || dest == NULL ||
168  !check_offset_len(offset_bytes, len_bytes, OTBN_DMEM_SIZE_BYTES)) {
169  return kDifBadArg;
170  }
171 
173  otbn->base_addr, OTBN_DMEM_REG_OFFSET + offset_bytes, dest, len_bytes);
174 
175  return kDifOk;
176 }
177 
179  bool enable) {
180  if (otbn == NULL) {
181  return kDifBadArg;
182  }
183 
184  // Only one bit in the CTRL register so no need to read current value.
185  uint32_t new_ctrl;
186 
187  if (enable) {
188  new_ctrl = 1;
189  } else {
190  new_ctrl = 0;
191  }
192 
193  mmio_region_write32(otbn->base_addr, OTBN_CTRL_REG_OFFSET, new_ctrl);
194  if (mmio_region_read32(otbn->base_addr, OTBN_CTRL_REG_OFFSET) != new_ctrl) {
195  return kDifUnavailable;
196  }
197 
198  return kDifOk;
199 }
200 
202  return OTBN_DMEM_SIZE_BYTES;
203 }
204 
206  return OTBN_IMEM_SIZE_BYTES;
207 }