Software APIs
flash_ctrl.h
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 #ifndef OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_DRIVERS_FLASH_CTRL_H_
5 #define OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_DRIVERS_FLASH_CTRL_H_
6 
7 #include <limits.h>
8 
11 #include "sw/device/lib/base/multibits.h"
12 #include "sw/device/silicon_creator/lib/error.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /**
19  * A flash partition.
20  *
21  * Each `flash_ctrl_partition_t` enumeration constant is a bitfield with the
22  * following layout:
23  * - Bit 0: Data (0) or information (1) partition.
24  * - Bits 1-2: Information partition type [0, 2].
25  */
26 typedef enum flash_ctrl_partition {
27  /**
28  * Data Partition.
29  */
30  kFlashCtrlPartitionData = 0,
31  /**
32  * Information partition of type 0.
33  *
34  * This partition has 10 pages.
35  */
36  kFlashCtrlPartitionInfo0 = (0 << 1) | 1,
37  /**
38  * Information partition of type 1.
39  *
40  * This partition has 1 page.
41  */
42  kFlashCtrlPartitionInfo1 = (1 << 1) | 1,
43  /**
44  * Information partition of type 2.
45  *
46  * This partition has 2 pages.
47  */
48  kFlashCtrlPartitionInfo2 = (2 << 1) | 1,
49 } flash_ctrl_partition_t;
50 
51 /**
52  * Bit and field definitions to get partition and info partition type from a
53  * `flash_ctrl_partition_t`.
54  */
55 #define FLASH_CTRL_PARTITION_BIT_IS_INFO 0
56 #define FLASH_CTRL_PARTITION_FIELD_INFO_TYPE \
57  ((bitfield_field32_t){.mask = 0x3, .index = 1})
58 
59 /**
60  * Table of flash information pages.
61  *
62  * Columns: Name, bank index, page index.
63  * We use an X macro to faciliate writing enums, switch statements, and unit
64  * tests using the contants here. All information pages in this table are of
65  * type 0 since silicon creator code does not need to access information pages
66  * of other types.
67  */
68 // clang-format off
69 #define FLASH_CTRL_INFO_PAGES_DEFINE(X) \
70  /**
71  * Bank 0 information partition type 0 pages.
72  */ \
73  X(kFlashCtrlInfoPageFactoryId, 0, 0) \
74  X(kFlashCtrlInfoPageCreatorSecret, 0, 1) \
75  X(kFlashCtrlInfoPageOwnerSecret, 0, 2) \
76  X(kFlashCtrlInfoPageWaferAuthSecret, 0, 3) \
77  X(kFlashCtrlInfoPageAttestationKeySeeds, 0, 4) \
78  X(kFlashCtrlInfoPageOwnerReserved0, 0, 5) \
79  X(kFlashCtrlInfoPageOwnerReserved1, 0, 6) \
80  X(kFlashCtrlInfoPageOwnerReserved2, 0, 7) \
81  X(kFlashCtrlInfoPageOwnerReserved3, 0, 8) \
82  X(kFlashCtrlInfoPageFactoryCerts, 0, 9) \
83  /**
84  * Bank 1 information partition type 0 pages.
85  */ \
86  X(kFlashCtrlInfoPageBootData0, 1, 0) \
87  X(kFlashCtrlInfoPageBootData1, 1, 1) \
88  X(kFlashCtrlInfoPageOwnerSlot0, 1, 2) \
89  X(kFlashCtrlInfoPageOwnerSlot1, 1, 3) \
90  X(kFlashCtrlInfoPageCreatorReserved0, 1, 4) \
91  X(kFlashCtrlInfoPageOwnerReserved4, 1, 5) \
92  X(kFlashCtrlInfoPageOwnerReserved5, 1, 6) \
93  X(kFlashCtrlInfoPageOwnerReserved6, 1, 7) \
94  X(kFlashCtrlInfoPageOwnerReserved7, 1, 8) \
95  X(kFlashCtrlInfoPageDiceCerts, 1, 9) \
96 // clang-format on
97 
98 /**
99  * A struct for storing base, config write-enable register, and config register
100  * addresses of an info page.
101  */
102 typedef struct flash_ctrl_info_page {
103  /**
104  * Base address.
105  */
106  uint32_t base_addr;
107  /**
108  * Config write-enable register address (offset from register base).
109  */
110  uint32_t cfg_wen_offset;
111  /**
112  * Config register address (offset from register base).
113  */
114  uint32_t cfg_offset;
116 
117 /**
118  * Helper macro for declaring an extern `flash_ctrl_info_page_t`.
119  * @param name_ Name of the enumeration constant.
120  * @param bank_ Bank of the info page.
121  * @param page_ Page of the info page.
122  */
123 #define INFO_PAGE_STRUCT_DECL_(name_, bank_, page_) \
124  extern const flash_ctrl_info_page_t name_;
125 
126 /**
127  * Info pages.
128  */
129 FLASH_CTRL_INFO_PAGES_DEFINE(INFO_PAGE_STRUCT_DECL_);
130 
131 #undef INFO_PAGE_STRUCT_DECL_
132 
133 /**
134  * Bitfields for `CREATOR_SW_CFG_FLASH_DATA_DEFAULT_CFG` and
135  * `CREATOR_SW_CFG_FLASH_INFO_BOOT_DATA_CFG` OTP items.
136  *
137  * Defined here to be able to use in tests.
138  */
139 #define FLASH_CTRL_OTP_FIELD_SCRAMBLING \
140  (bitfield_field32_t) { .mask = UINT8_MAX, .index = CHAR_BIT * 0 }
141 #define FLASH_CTRL_OTP_FIELD_ECC \
142  (bitfield_field32_t) { .mask = UINT8_MAX, .index = CHAR_BIT * 1 }
143 #define FLASH_CTRL_OTP_FIELD_HE \
144  (bitfield_field32_t) { .mask = UINT8_MAX, .index = CHAR_BIT * 2 }
145 
146 /**
147  * Bitfields for `CREATOR_SW_CFG_FLASH_HW_INFO_CFG_OVERRIDE` OTP item.
148  *
149  * Defined here to be able to use in tests.
150  */
151 #define FLASH_CTRL_OTP_FIELD_HW_INFO_CFG_OVERRIDE_SCRAMBLE_DIS \
152  (bitfield_field32_t) { .mask = UINT8_MAX, .index = CHAR_BIT * 0 }
153 #define FLASH_CTRL_OTP_FIELD_HW_INFO_CFG_OVERRIDE_ECC_DIS \
154  (bitfield_field32_t) { .mask = UINT8_MAX, .index = CHAR_BIT * 1 }
155 
156 /**
157  * The following constants represent the expected number of sec_mmio
158  * register writes performed by functions in provided in this module. See
159  * `SEC_MMIO_WRITE_INCREMENT()` for more details.
160  *
161  * Example:
162  * ```
163  * flash_ctrl_init();
164  * SEC_MMIO_WRITE_INCREMENT(kFlashCtrlSecMmioInit);
165  * ```
166  */
167 enum {
168  kFlashCtrlSecMmioCertInfoPageCreatorCfg = 2,
169  kFlashCtrlSecMmioCertInfoPageOwnerRestrict = 1,
170  kFlashCtrlSecMmioCertInfoPagesOwnerRestrict = 5,
171  kFlashCtrlSecMmioCreatorInfoPagesLockdown = 12,
172  kFlashCtrlSecMmioDataDefaultCfgSet = 1,
173  kFlashCtrlSecMmioDataDefaultPermsSet = 1,
174  kFlashCtrlSecMmioExecSet = 1,
175  kFlashCtrlSecMmioInfoCfgSet = 1,
176  kFlashCtrlSecMmioInfoCfgLock = 1,
177  kFlashCtrlSecMmioInfoPermsSet = 1,
178  kFlashCtrlSecMmioBankErasePermsSet = 1,
179  kFlashCtrlSecMmioInit = 3,
180 };
181 
182 /**
183  * Value of a word in flash after erase.
184  */
185 enum {
186  kFlashCtrlErasedWord = UINT32_MAX,
187 };
188 
189 /**
190  * Kicks off the initialization of the flash controller.
191  *
192  * This must complete before flash can be accessed. The init status can be
193  * queried by calling `flash_ctrl_status_get()` and checking `init_wip`.
194  *
195  * The caller is responsible for calling
196  * `SEC_MMIO_WRITE_INCREMENT(kFlashCtrlSecMmioInit)` when sec_mmio is being
197  * used to check expectations.
198  */
199 void flash_ctrl_init(void);
200 
201 /**
202  * Status bits.
203  */
204 typedef struct flash_ctrl_status {
205  /**
206  * Flash read FIFO full, software must consume data.
207  */
208  bool rd_full;
209  /**
210  * Flash read FIFO empty.
211  */
212  bool rd_empty;
213  /**
214  * Flash program FIFO full, software must consume data.
215  */
216  bool prog_full;
217  /**
218  * Flash program FIFO empty.
219  */
221  /**
222  * Flash controller undergoing init.
223  */
224  bool init_wip;
226 
227 /**
228  * Query the status registers on the flash controller.
229  *
230  * This function checks the various status bits as described in
231  * `flash_ctrl_status_t`.
232  *
233  * @param[out] status The current status of the flash controller.
234  */
235 void flash_ctrl_status_get(flash_ctrl_status_t *status);
236 
237 /**
238  * Error code bits.
239  */
240 typedef struct flash_ctrl_error_code {
241  /**
242  * Flash macro error occured.
243  */
244  bool macro_err;
245  /**
246  * Shadow register update error.
247  */
249  /**
250  * Flash program type is unavailable.
251  */
252  bool prog_type_err;
253  /**
254  * Flash program window resolution error. Start and end of programming region
255  * are in different windows.
256  */
257  bool prog_win_err;
258  /**
259  * Flash programming error, could be an integrity error. Read the
260  * STD_FAULT_STATUS register.
261  */
262  bool prog_err;
263  /**
264  * Flash read error, could be an integrity error. Read the STD_FAULT_STATUS
265  * register.
266  */
267  bool rd_err;
268  /**
269  * Flash access permission error. Read the ERR_ADDR register for the faulting
270  * address.
271  */
272  bool mp_err;
273  /**
274  * Software has supplied an undefined flash operation.
275  */
276  bool op_err;
278 
279 /**
280  * Query the error code register on the flash controller.
281  *
282  * This function checks the various error code bits as described in
283  * `flash_ctrl_error_code_t`.
284  *
285  * @param[out] error_code The current error code of the flash controller.
286  */
287 void flash_ctrl_error_code_get(flash_ctrl_error_code_t *error_code);
288 
289 /**
290  * Reads data from the data partition.
291  *
292  * The flash controller will truncate to the closest, lower word aligned
293  * address. For example, if 0x13 is supplied, the controller will perform a read
294  * at address 0x10.
295  *
296  * @param addr Address to read from.
297  * @param word_count Number of bus words to read.
298  * @param[out] data Buffer to store the read data. Must be word aligned.
299  * @return Result of the operation.
300  */
302 rom_error_t flash_ctrl_data_read(uint32_t addr, uint32_t word_count,
303  void *data);
304 
305 /**
306  * Reads data from an information page.
307  *
308  * The flash controller will truncate to the closest, lower word aligned
309  * address. For example, if 0x13 is supplied, the controller will start reading
310  * at address 0x10.
311  *
312  * @param info_page Information page to read from.
313  * @param offset Offset from the start of the page.
314  * @param word_count Number of bus words to read.
315  * @param[out] data Buffer to store the read data. Must be word aligned.
316  * @return Result of the operation.
317  */
319 rom_error_t flash_ctrl_info_read(const flash_ctrl_info_page_t *info_page,
320  uint32_t offset, uint32_t word_count,
321  void *data);
322 
323 /**
324  * Reads data from an information page, returning all zeros if a read error code
325  * is encountered.
326  *
327  * The flash controller will truncate to the closest, lower word aligned
328  * address. For example, if 0x13 is supplied, the controller will start reading
329  * at address 0x10.
330  *
331  * @param info_page Information page to read from.
332  * @param offset Offset from the start of the page.
333  * @param word_count Number of bus words to read.
334  * @param[out] data Buffer to store the read data. Must be word aligned.
335  * @return Result of the operation.
336  */
338 rom_error_t flash_ctrl_info_read_zeros_on_read_error(
339  const flash_ctrl_info_page_t *info_page, uint32_t offset,
340  uint32_t word_count, void *data);
341 
342 /**
343  * Locks the configuration of an information page.
344  *
345  * This writes a zero to the write-enable register for info page configuration.
346  *
347  * @param info_page Information page to read from.
348  */
349 void flash_ctrl_info_lock(const flash_ctrl_info_page_t *info_page);
350 
351 /**
352  * Writes data to the data partition.
353  *
354  * The flash controller will truncate to the closest, lower word aligned
355  * address. For example, if 0x13 is supplied, the controller will start writing
356  * at address 0x10.
357  *
358  * @param addr Address to write to.
359  * @param word_count Number of bus words to write.
360  * @param data Data to write. Must be word aligned.
361  * @return Result of the operation.
362  */
364 rom_error_t flash_ctrl_data_write(uint32_t addr, uint32_t word_count,
365  const void *data);
366 
367 /**
368  * Writes data to an information page.
369  *
370  * The flash controller will truncate to the closest, lower word aligned
371  * address. For example, if 0x13 is supplied, the controller will start writing
372  * at address 0x10.
373  *
374  * @param info_page Information page to write to.
375  * @param offset Offset from the start of the page.
376  * @param word_count Number of bus words to write.
377  * @param data Data to write. Must be word aligned.
378  * @return Result of the operation.
379  */
381 rom_error_t flash_ctrl_info_write(const flash_ctrl_info_page_t *info_page,
382  uint32_t offset, uint32_t word_count,
383  const void *data);
384 
385 /*
386  * Encoding generated with
387  * $ ./util/design/sparse-fsm-encode.py -d 5 -m 2 -n 32 \
388  * -s 2181785819 --language=c
389  *
390  * Minimum Hamming distance: 14
391  * Maximum Hamming distance: 14
392  * Minimum Hamming weight: 14
393  * Maximum Hamming weight: 18
394  */
395 
396 typedef enum flash_ctrl_erase_type {
397  /**
398  * Erase a page.
399  */
400  kFlashCtrlEraseTypePage = 0xaf0eab8b,
401  /**
402  * Erase a bank.
403  */
404  kFlashCtrlEraseTypeBank = 0x80329be9,
405 } flash_ctrl_erase_type_t;
406 
407 /**
408  * Erases a data partition page or bank.
409  *
410  * The flash controller will truncate to the closest page boundary for page
411  * erase operations, and to the nearest bank aligned boundary for bank erase
412  * operations.
413  *
414  * @param addr Address that falls within the bank or page being deleted.
415  * @param erase_type Whether to erase a page or a bank.
416  * @return Result of the operation.
417  */
419 rom_error_t flash_ctrl_data_erase(uint32_t addr,
420  flash_ctrl_erase_type_t erase_type);
421 
422 /**
423  * Verifies that a data partition page or bank was erased.
424  *
425  * @param addr Address that falls within the bank or page erased.
426  * @param erase_type Whether to verify a page or a bank.
427  * @return Result of the operation.
428  */
430 rom_error_t flash_ctrl_data_erase_verify(uint32_t addr,
431  flash_ctrl_erase_type_t erase_type);
432 
433 /**
434  * Erases an information partition page or bank.
435  *
436  * @param info_page Information page to erase for page erases, or a page within
437  * the bank to erase for bank erases.
438  * @param erase_type Whether to erase a page or a bank.
439  * @return Result of the operation.
440  */
442 rom_error_t flash_ctrl_info_erase(const flash_ctrl_info_page_t *info_page,
443  flash_ctrl_erase_type_t erase_type);
444 
445 /**
446  * A struct for specifying access permissions.
447  *
448  * flash_ctrl config registers use 4-bits for boolean values. Use
449  * `kMultiBitBool4True` to enable and `kMultiBitBool4False` to disable
450  * permissions.
451  */
452 typedef struct flash_ctrl_perms {
453  /**
454  * Read.
455  */
456  multi_bit_bool_t read;
457  /**
458  * Write.
459  */
460  multi_bit_bool_t write;
461  /**
462  * Erase.
463  */
464  multi_bit_bool_t erase;
466 
467 /**
468  * Sets default access permissions for the data partition.
469  *
470  * A permission is enabled only if the corresponding field in `perms` is
471  * `kMultiBitBool4True`.
472  *
473  * The caller is responsible for calling
474  * `SEC_MMIO_WRITE_INCREMENT(kFlashCtrlSecMmioDataDefaultPermsSet)` when
475  * sec_mmio is being used to check expectations.
476  *
477  * @param perms New permissions.
478  */
479 void flash_ctrl_data_default_perms_set(flash_ctrl_perms_t perms);
480 
481 /**
482  * Sets access permissions for an info page.
483  *
484  * A permission is enabled only if the corresponding field in `perms` is
485  * `kMultiBitBool4True`.
486  *
487  * * The caller is responsible for calling
488  * `SEC_MMIO_WRITE_INCREMENT(kFlashCtrlSecMmioInfoPermsSet)` when sec_mmio is
489  * being used to check expectations.
490  *
491  * @param info_page An information page.
492  * @param perms New permissions.
493  */
494 void flash_ctrl_info_perms_set(const flash_ctrl_info_page_t *info_page,
495  flash_ctrl_perms_t perms);
496 
497 /**
498  * A struct for flash configuration settings.
499  *
500  * flash_ctrl config registers use 4-bits for boolean values. Use
501  * `kMultiBitBool4True` to enable and `kMultiBitBool4False` to disable
502  * these settings.
503  *
504  * This struct has no padding, so it is safe to `memcmp()` without invoking UB.
505  */
506 typedef struct flash_ctrl_cfg {
507  /**
508  * Scrambling.
509  */
510  multi_bit_bool_t scrambling;
511  /**
512  * ECC.
513  */
514  multi_bit_bool_t ecc;
515  /**
516  * High endurance.
517  */
518  multi_bit_bool_t he;
520 
525 
526 /**
527  * Sets default configuration settings for the data partition.
528  *
529  * The caller is responsible for calling
530  * `SEC_MMIO_WRITE_INCREMENT(kFlashCtrlSecMmioDataDefaultCfgSet)` when sec_mmio
531  * is being used to check expectations.
532  *
533  * @param cfg New configuration settings.
534  */
535 void flash_ctrl_data_default_cfg_set(flash_ctrl_cfg_t cfg);
536 
537 /**
538  * Reads the current default configuration settings for the data partition.
539  *
540  * @return Current configuration settings.
541  */
542 flash_ctrl_cfg_t flash_ctrl_data_default_cfg_get(void);
543 
544 /**
545  * A type for flash_ctrl memory protection region indices.
546  */
547 typedef uint32_t flash_ctrl_region_index_t;
548 
549 /**
550  * Configure memory protection for a region of pages.
551  *
552  * Based on the `region` parameter, this function overwrites the
553  * `MP_REGION_${region}` and `MP_REGION_CFG_${region}` registers. Calling this
554  * function invalidates previously-configured protections for `region`.
555  *
556  * @param region The index of the region to protect.
557  * @param page_offset The index of the first page in the region.
558  * @param num_pages The number of pages in the region.
559  * @param perms The read/write/erase permissions for this region.
560  * @param cfg Flash config values that are used to fill in some fields of the
561  * `MP_REGION_CFG_${region}` register.
562  * @param lock Lock the configuration for this region.
563  */
564 void flash_ctrl_data_region_protect(flash_ctrl_region_index_t region,
565  uint32_t page_offset, uint32_t num_pages,
566  flash_ctrl_perms_t perms,
568 
569 /**
570  * Sets configuration settings for an info page.
571  *
572  * The caller is responsible for calling
573  * `SEC_MMIO_WRITE_INCREMENT(kFlashCtrlSecMmioInfoCfgSet)` when sec_mmio is
574  * being used to check expectations.
575  *
576  * @param info_page An information page.
577  * @param cfg New configuration settings.
578  */
579 void flash_ctrl_info_cfg_set(const flash_ctrl_info_page_t *info_page,
580  flash_ctrl_cfg_t cfg);
581 
582 /**
583  * Write-locks configuration settings for an info page.
584  *
585  * The caller is responsible for calling
586  * `SEC_MMIO_WRITE_INCREMENT(kFlashCtrlSecMmioInfoCfgLock)` when sec_mmio is
587  * being used to check expectations.
588  *
589  * @param info_page An information page.
590  * @param cfg New configuration settings.
591  */
592 void flash_ctrl_info_cfg_lock(const flash_ctrl_info_page_t *info_page);
593 
594 /**
595  * Set bank erase permissions for both flash banks.
596  *
597  * The caller is responsible for calling
598  * `SEC_MMIO_WRITE_INCREMENT(kFlashCtrlSecMmioBankErasePermsSet)` when
599  * sec_mmio is being used to check expectations.
600  *
601  * @param enable Whether to enable bank erase.
602  */
603 void flash_ctrl_bank_erase_perms_set(hardened_bool_t enable);
604 
605 /**
606  * Enable execution from flash.
607  *
608  * Note: a ePMP region must also be configured in order to execute code in
609  * flash.
610  *
611  * The caller is responsible for calling
612  * `SEC_MMIO_WRITE_INCREMENT(kFlashCtrlSecMmioExecSet)` when sec_mmio is being
613  * used to check expectations.
614  *
615  * @param exec_val Value to write to the `flash_ctrl.EXEC` register.
616  * `FLASH_CTRL_PARAM_EXEC_EN` will enable execution, all other values will
617  * disable execution.
618  */
619 void flash_ctrl_exec_set(uint32_t exec_val);
620 
621 /**
622  * Disables all access to silicon creator info pages until next reset.
623  *
624  * This function must be called in ROM_EXT before handing over execution to the
625  * first owner boot stage.
626  *
627  * The caller is responsible for calling
628  * `SEC_MMIO_WRITE_INCREMENT(kFlashCtrlSecMmioCreatorInfoPagesLockdown)` when
629  * sec_mmio is being used to check expectations.
630  */
631 void flash_ctrl_creator_info_pages_lockdown(void);
632 
633 /**
634  * Certificate info page configurations and permissions.
635  *
636  * Certificate info pages are fully accessable by the creator code (ROM +
637  * ROM_EXT), but read-only for owner code.
638  */
639 extern const flash_ctrl_cfg_t kCertificateInfoPageCfg;
640 extern const flash_ctrl_perms_t kCertificateInfoPageCreatorAccess;
641 extern const flash_ctrl_perms_t kCertificateInfoPageOwnerAccess;
642 
643 /**
644  * Configures certificate flash info pages for access by the silicon creator.
645  *
646  * Flash info pages that hold device certificates are fully accessable by the
647  * silicon creator, but are restricted to read-only access by the ROM_EXT before
648  * handing over execution to the owner boot stage.
649  *
650  * The caller is responsible for calling
651  * `SEC_MMIO_WRITE_INCREMENT(kFlashCtrlSecMmioCertInfoPageCreatorCfg)`
652  * when sec_mmio is being used to check expectations.
653  */
654 void flash_ctrl_cert_info_page_creator_cfg(
655  const flash_ctrl_info_page_t *info_page);
656 
657 /**
658  * Restricts access of certificate flash info pages to read-only for the silicon
659  * owner.
660  *
661  * The caller is responsible for calling
662  * `SEC_MMIO_WRITE_INCREMENT(kFlashCtrlSecMmioCertInfoPagesOwnerRestrict)`
663  * when sec_mmio is being used to check expectations.
664  */
665 void flash_ctrl_cert_info_page_owner_restrict(
666  const flash_ctrl_info_page_t *info_page);
667 
668 #ifdef __cplusplus
669 }
670 #endif
671 
672 #endif // OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_DRIVERS_FLASH_CTRL_H_