blob: 0bd0f35a510a71e982a0bcb4b6ac4cbecee365c8 [file] [log] [blame]
Angel Pons32859fc2020-04-02 23:48:27 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Aaron Durbin20686d82015-03-05 14:11:27 -06003
4#ifndef _IMD_H_
5#define _IMD_H_
6
7#include <stdint.h>
8#include <stddef.h>
9
10/*
11 * imd is an in-memory database/directory/datastore (whatever d word you
12 * desire). It grows downwards in memory from provided upper limit and
13 * root size. Each entry has a size alignment which is also provided by
14 * the caller.
15 *
16 * +----------------------+ <- upper_limit
17 * | +----| root pointer |
18 * | | +----------------------+
19 * | | | |--------+
20 * | +--->| root block |-----+ |
21 * | +----------------------+-----|--|--- root_size
22 * | | | | |
23 * | | | | |
24 * | | alloc N |<----+ |
25 * | +----------------------+ |
26 * | | | |
27 * | | | |
28 * \|/ | alloc N + 1 |<-------+
29 * v +----------------------+
30 *
31 * The root_size in imd_create_empty() encompasses the root pointer
32 * and root block. The root_size value, therefore, dictates the number
33 * of allocations maintained by the imd.
34 */
35
36/*
37 * NOTE: This API has the following calling conventions: all functions
38 * returning int supply 0 on success or < 0 on error.
39 */
40
41struct imd_entry;
42struct imd;
43
44/*
45 * Initialize handle to use for working with an imd. Upper limit is the
46 * exclusive address to start allocating down from. This function needs
47 * to be called at least once before any other imd related functions
48 * can be used.
49 */
50void imd_handle_init(struct imd *imd, void *upper_limit);
51
52/*
53 * Initialize a handle with a shallow recovery. This function doesn't
54 * verify every entry, but it does set up the root pointer. Because of
55 * this behavior it's not very safe. However, the current CBMEM constraints
56 * demand having these semantics.
57 */
58void imd_handle_init_partial_recovery(struct imd *imd);
59
60/*
61 * Create an empty imd with a specified root_size and each entry is aligned to
62 * the provided entry_align. As noted above the root size encompasses the
63 * root pointer and root block leading to the number of imd entries being a
64 * function of the root_size parameter.
65 */
66int imd_create_empty(struct imd *imd, size_t root_size, size_t entry_align);
67
68/*
Aaron Durbincac50502015-03-24 23:14:46 -050069 * Create an empty imd with both large and small allocations. The small
70 * allocations come from a fixed imd stored internally within the large
71 * imd. The region allocated for tracking the smaller allocations is dependent
72 * on the small root_size and the large entry alignment by calculating the
73 * number of entries within the small imd and multiplying that by the small
74 * entry alignment.
75 */
76int imd_create_tiered_empty(struct imd *imd,
77 size_t lg_root_size, size_t lg_entry_align,
78 size_t sm_root_size, size_t sm_entry_align);
79
80/*
Aaron Durbin20686d82015-03-05 14:11:27 -060081 * Recover a previously created imd.
82 */
83int imd_recover(struct imd *imd);
84
85/* Limit imd to provided max_size. */
86int imd_limit_size(struct imd *imd, size_t max_size);
87
88/* Lock down imd from further modifications. */
89int imd_lockdown(struct imd *imd);
90
91/* Fill in base address and size of region used by imd. */
92int imd_region_used(struct imd *imd, void **base, size_t *size);
93
94/* Add an entry to the imd. If id already exists NULL is returned. */
95const struct imd_entry *imd_entry_add(const struct imd *imd, uint32_t id,
96 size_t size);
97
98/* Locate an entry within the imd. NULL is returned when not found. */
99const struct imd_entry *imd_entry_find(const struct imd *imd, uint32_t id);
100
101/* Find an existing entry or add a new one. */
102const struct imd_entry *imd_entry_find_or_add(const struct imd *imd,
103 uint32_t id, size_t size);
104
105/* Returns size of entry or 0 on failure. */
106size_t imd_entry_size(const struct imd *imd, const struct imd_entry *entry);
107
108/* Returns pointer to region described by entry or NULL on failure. */
109void *imd_entry_at(const struct imd *imd, const struct imd_entry *entry);
110
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500111/* Returns id for the imd entry. */
112uint32_t imd_entry_id(const struct imd *imd, const struct imd_entry *entry);
113
Aaron Durbin20686d82015-03-05 14:11:27 -0600114/* Attempt to remove entry from imd. */
115int imd_entry_remove(const struct imd *imd, const struct imd_entry *entry);
116
117/* Print the entry information provided by lookup with the specified size. */
118struct imd_lookup {
119 uint32_t id;
120 const char *name;
121};
122
123int imd_print_entries(const struct imd *imd, const struct imd_lookup *lookup,
124 size_t size);
125
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500126struct imd_cursor;
127/* Initialize an imd_cursor object to walk the IMD entries. */
128int imd_cursor_init(const struct imd *imd, struct imd_cursor *cursor);
129
130/* Retrieve the next imd entry the cursor is referencing. Returns NULL when
131 * no more entries exist. */
132const struct imd_entry *imd_cursor_next(struct imd_cursor *cursor);
Aaron Durbin20686d82015-03-05 14:11:27 -0600133
134/*
135 * The struct imd is a handle for working with an in-memory directory.
136 *
137 * NOTE: Do not directly touch any fields within this structure. An imd pointer
138 * is meant to be opaque, but the fields are exposed for stack allocation.
139 */
Aaron Durbincac50502015-03-24 23:14:46 -0500140struct imdr {
Aaron Durbin20686d82015-03-05 14:11:27 -0600141 uintptr_t limit;
142 void *r;
143};
Aaron Durbincac50502015-03-24 23:14:46 -0500144struct imd {
145 struct imdr lg;
146 struct imdr sm;
147};
Aaron Durbin20686d82015-03-05 14:11:27 -0600148
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500149struct imd_cursor {
150 size_t current_imdr;
151 size_t current_entry;
152 const struct imdr *imdr[2];
153};
154
Aaron Durbin20686d82015-03-05 14:11:27 -0600155#endif /* _IMD_H_ */