Documentation: Fix sphinx configuration
[coreboot.git] / src / include / imd.h
blob7abbb9876d630a097f31594708d3d50fe49824c4
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #ifndef _IMD_H_
4 #define _IMD_H_
6 #include <stdint.h>
7 #include <stddef.h>
9 /*
10 * imd is an in-memory database/directory/datastore (whatever d word you
11 * desire). It grows downwards in memory from provided upper limit and
12 * root size. Each entry has a size alignment which is also provided by
13 * the caller.
15 * +----------------------+ <- upper_limit
16 * | +----| root pointer |
17 * | | +----------------------+
18 * | | | |--------+
19 * | +--->| root block |-----+ |
20 * | +----------------------+-----|--|--- root_size
21 * | | | | |
22 * | | | | |
23 * | | alloc N |<----+ |
24 * | +----------------------+ |
25 * | | | |
26 * | | | |
27 * \|/ | alloc N + 1 |<-------+
28 * v +----------------------+
30 * The root_size in imd_create_empty() encompasses the root pointer
31 * and root block. The root_size value, therefore, dictates the number
32 * of allocations maintained by the imd.
36 * NOTE: This API has the following calling conventions: all functions
37 * returning int supply 0 on success or < 0 on error.
40 struct imd_entry;
41 struct imd;
44 * Initialize handle to use for working with an imd. Upper limit is the
45 * exclusive address to start allocating down from. This function needs
46 * to be called at least once before any other imd related functions
47 * can be used.
49 void imd_handle_init(struct imd *imd, void *upper_limit);
52 * Initialize a handle with a shallow recovery. This function doesn't
53 * verify every entry, but it does set up the root pointer. Because of
54 * this behavior it's not very safe. However, the current CBMEM constraints
55 * demand having these semantics.
57 void imd_handle_init_partial_recovery(struct imd *imd);
60 * Create an empty imd with a specified root_size and each entry is aligned to
61 * the provided entry_align. As noted above the root size encompasses the
62 * root pointer and root block leading to the number of imd entries being a
63 * function of the root_size parameter.
65 int imd_create_empty(struct imd *imd, size_t root_size, size_t entry_align);
68 * Create an empty imd with both large and small allocations. The small
69 * allocations come from a fixed imd stored internally within the large
70 * imd. The region allocated for tracking the smaller allocations is dependent
71 * on the small root_size and the large entry alignment by calculating the
72 * number of entries within the small imd and multiplying that by the small
73 * entry alignment.
75 int imd_create_tiered_empty(struct imd *imd,
76 size_t lg_root_size, size_t lg_entry_align,
77 size_t sm_root_size, size_t sm_entry_align);
80 * Recover a previously created imd.
82 int imd_recover(struct imd *imd);
84 /* Limit imd to provided max_size. */
85 int imd_limit_size(struct imd *imd, size_t max_size);
87 /* Lock down imd from further modifications. */
88 int imd_lockdown(struct imd *imd);
90 /* Fill in base address and size of region used by imd. */
91 int imd_region_used(struct imd *imd, void **base, size_t *size);
93 /* Add an entry to the imd. If id already exists NULL is returned. */
94 const struct imd_entry *imd_entry_add(const struct imd *imd, uint32_t id,
95 size_t size);
97 /* Locate an entry within the imd. NULL is returned when not found. */
98 const struct imd_entry *imd_entry_find(const struct imd *imd, uint32_t id);
100 /* Find an existing entry or add a new one. */
101 const struct imd_entry *imd_entry_find_or_add(const struct imd *imd,
102 uint32_t id, size_t size);
104 /* Returns size of entry or 0 on failure. */
105 size_t imd_entry_size(const struct imd_entry *entry);
107 /* Returns pointer to region described by entry or NULL on failure. */
108 void *imd_entry_at(const struct imd *imd, const struct imd_entry *entry);
110 /* Returns id for the imd entry. */
111 uint32_t imd_entry_id(const struct imd_entry *entry);
113 /* Attempt to remove entry from imd. */
114 int imd_entry_remove(const struct imd *imd, const struct imd_entry *entry);
116 /* Print the entry information provided by lookup with the specified size. */
117 struct imd_lookup {
118 uint32_t id;
119 const char *name;
122 int imd_print_entries(const struct imd *imd, const struct imd_lookup *lookup,
123 size_t size);
125 struct imd_cursor;
126 /* Initialize an imd_cursor object to walk the IMD entries. */
127 int imd_cursor_init(const struct imd *imd, struct imd_cursor *cursor);
129 /* Retrieve the next imd entry the cursor is referencing. Returns NULL when
130 * no more entries exist. */
131 const struct imd_entry *imd_cursor_next(struct imd_cursor *cursor);
134 * The struct imd is a handle for working with an in-memory directory.
136 * NOTE: Do not directly touch any fields within this structure. An imd pointer
137 * is meant to be opaque, but the fields are exposed for stack allocation.
139 struct imdr {
140 uintptr_t limit;
141 void *r;
143 struct imd {
144 struct imdr lg;
145 struct imdr sm;
148 struct imd_cursor {
149 size_t current_imdr;
150 size_t current_entry;
151 const struct imdr *imdr[2];
154 #endif /* _IMD_H_ */