Software APIs
ret_sram_testutils.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 
5 #include "sw/device/lib/testing/ret_sram_testutils.h"
6 
7 #include <assert.h>
8 #include <stdbool.h>
9 #include <stdint.h>
10 
11 #include "dt/dt_sram_ctrl.h" // Generated
12 #include "sw/device/lib/testing/test_framework/check.h"
14 #include "sw/device/silicon_creator/lib/drivers/retention_sram.h"
15 
16 // All the memory used by these utilities should be placed within the the
17 // owner portion of retention SRAM, as defined in retention_sram.h.
18 
19 /**
20  * This represents the memory used by these utilities.
21  */
22 typedef struct testing_utilities {
23  /**
24  * An array of counters.
25  */
26  uint32_t counters[kRetSramTestutilsNumberOfCounters];
27 
28  /**
29  * A scratch array.
30  */
31  uint32_t scratch[kRetSramTestutilsScratchSizeAsInts];
33 
34 enum { kOffsetOfTestutils = offsetof(retention_sram_t, owner) };
35 
36 // TODO: Replace with a DT API call when memory size information is exposed
37 // via dtgen.
38 #if defined(OPENTITAN_IS_EARLGREY)
40 
41 static_assert(kOffsetOfTestutils + sizeof(testing_utilities_t) <
43  "Testing utilities spill out of retention SRAM");
44 #elif defined(OPENTITAN_IS_DARJEELING)
45 #include "hw/top_darjeeling/sw/autogen/top_darjeeling.h"
46 
47 static_assert(kOffsetOfTestutils + sizeof(testing_utilities_t) <
48  TOP_DARJEELING_RAM_RET_AON_SIZE_BYTES,
49  "Testing utilities spill out of retention SRAM");
50 #else
51 #error "ret_sram_testutils does not support this top"
52 #endif
53 
54 static_assert(offsetof(retention_sram_t, owner) <= kOffsetOfTestutils,
55  "Testing utilities overlap owner area in retention SRAM");
56 
58 
59 void ret_sram_testutils_init(void) {
61  (testing_utilities_t *)(dt_sram_ctrl_reg_block(kDtSramCtrlRetAon,
62  kDtSramCtrlRegBlockRam) +
63  kOffsetOfTestutils);
64 }
65 
66 status_t ret_sram_testutils_counter_clear(size_t counter) {
67  TRY_CHECK(testing_utilities != NULL);
68  TRY_CHECK(counter < kRetSramTestutilsNumberOfCounters);
69  testing_utilities->counters[counter] = 0;
70  return OK_STATUS();
71 }
72 
73 status_t ret_sram_testutils_counter_get(size_t counter, uint32_t *value) {
74  TRY_CHECK(testing_utilities != NULL);
75  TRY_CHECK(value != NULL);
76  TRY_CHECK(counter < kRetSramTestutilsNumberOfCounters);
77  *value = testing_utilities->counters[counter];
78  return OK_STATUS();
79 }
80 
81 status_t ret_sram_testutils_counter_increment(size_t counter) {
82  uint32_t value;
83  TRY_CHECK(testing_utilities != NULL);
84  TRY_CHECK(counter < kRetSramTestutilsNumberOfCounters);
85  value = testing_utilities->counters[counter];
86  TRY_CHECK(value < UINT32_MAX);
87  testing_utilities->counters[counter]++;
88  return OK_STATUS();
89 }
90 
91 status_t ret_sram_testutils_counter_set(size_t counter, uint32_t value) {
92  TRY_CHECK(testing_utilities != NULL);
93  TRY_CHECK(counter < kRetSramTestutilsNumberOfCounters);
94  testing_utilities->counters[counter] = value;
95  return OK_STATUS();
96 }
97 
98 status_t ret_sram_testutils_scratch_read(size_t offset, size_t size,
99  uint32_t *dest) {
100  TRY_CHECK(testing_utilities != NULL);
101  TRY_CHECK(dest != NULL);
102  TRY_CHECK(offset + size <= kRetSramTestutilsScratchSizeAsInts);
103  for (size_t i = 0; i < size; ++i) {
104  dest[i] = testing_utilities->scratch[offset + i];
105  }
106  return OK_STATUS();
107 }
108 
109 status_t ret_sram_testutils_scratch_write(size_t offset, size_t size,
110  uint32_t *src) {
111  TRY_CHECK(testing_utilities != NULL);
112  TRY_CHECK(src != NULL);
113  TRY_CHECK(offset + size <= kRetSramTestutilsScratchSizeAsInts);
114  for (size_t i = 0; i < size; ++i) {
115  testing_utilities->scratch[offset + i] = src[i];
116  }
117  return OK_STATUS();
118 }
119 
120 status_t ret_sram_testutils_is_testrom(bool *is_testrom) {
121  *is_testrom =
122  retention_sram_get()
123  ->creator
124  .reserved[ARRAYSIZE((retention_sram_t){0}.creator.reserved) - 1] ==
126 
127  return OK_STATUS();
128 }