2 * This file is part of the coreboot project.
4 * Copyright 2015 Google, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
23 * imd is an in-memory database/directory/datastore (whatever d word you
24 * desire). It grows downwards in memory from provided upper limit and
25 * root size. Each entry has a size alignment which is also provided by
28 * +----------------------+ <- upper_limit
29 * | +----| root pointer |
30 * | | +----------------------+
32 * | +--->| root block |-----+ |
33 * | +----------------------+-----|--|--- root_size
36 * | | alloc N |<----+ |
37 * | +----------------------+ |
40 * \|/ | alloc N + 1 |<-------+
41 * v +----------------------+
43 * The root_size in imd_create_empty() encompasses the root pointer
44 * and root block. The root_size value, therefore, dictates the number
45 * of allocations maintained by the imd.
49 * NOTE: This API has the following calling conventions: all functions
50 * returning int supply 0 on success or < 0 on error.
57 * Initialize handle to use for working with an imd. Upper limit is the
58 * exclusive address to start allocating down from. This function needs
59 * to be called at least once before any other imd related functions
62 void imd_handle_init(struct imd
*imd
, void *upper_limit
);
65 * Initialize a handle with a shallow recovery. This function doesn't
66 * verify every entry, but it does set up the root pointer. Because of
67 * this behavior it's not very safe. However, the current CBMEM constraints
68 * demand having these semantics.
70 void imd_handle_init_partial_recovery(struct imd
*imd
);
73 * Create an empty imd with a specified root_size and each entry is aligned to
74 * the provided entry_align. As noted above the root size encompasses the
75 * root pointer and root block leading to the number of imd entries being a
76 * function of the root_size parameter.
78 int imd_create_empty(struct imd
*imd
, size_t root_size
, size_t entry_align
);
81 * Create an empty imd with both large and small allocations. The small
82 * allocations come from a fixed imd stored internally within the large
83 * imd. The region allocated for tracking the smaller allocations is dependent
84 * on the small root_size and the large entry alignment by calculating the
85 * number of entries within the small imd and multiplying that by the small
88 int imd_create_tiered_empty(struct imd
*imd
,
89 size_t lg_root_size
, size_t lg_entry_align
,
90 size_t sm_root_size
, size_t sm_entry_align
);
93 * Recover a previously created imd.
95 int imd_recover(struct imd
*imd
);
97 /* Limit imd to provided max_size. */
98 int imd_limit_size(struct imd
*imd
, size_t max_size
);
100 /* Lock down imd from further modifications. */
101 int imd_lockdown(struct imd
*imd
);
103 /* Fill in base address and size of region used by imd. */
104 int imd_region_used(struct imd
*imd
, void **base
, size_t *size
);
106 /* Add an entry to the imd. If id already exists NULL is returned. */
107 const struct imd_entry
*imd_entry_add(const struct imd
*imd
, uint32_t id
,
110 /* Locate an entry within the imd. NULL is returned when not found. */
111 const struct imd_entry
*imd_entry_find(const struct imd
*imd
, uint32_t id
);
113 /* Find an existing entry or add a new one. */
114 const struct imd_entry
*imd_entry_find_or_add(const struct imd
*imd
,
115 uint32_t id
, size_t size
);
117 /* Returns size of entry or 0 on failure. */
118 size_t imd_entry_size(const struct imd
*imd
, const struct imd_entry
*entry
);
120 /* Returns pointer to region described by entry or NULL on failure. */
121 void *imd_entry_at(const struct imd
*imd
, const struct imd_entry
*entry
);
123 /* Returns id for the imd entry. */
124 uint32_t imd_entry_id(const struct imd
*imd
, const struct imd_entry
*entry
);
126 /* Attempt to remove entry from imd. */
127 int imd_entry_remove(const struct imd
*imd
, const struct imd_entry
*entry
);
129 /* Print the entry information provided by lookup with the specified size. */
135 int imd_print_entries(const struct imd
*imd
, const struct imd_lookup
*lookup
,
139 /* Initialize an imd_cursor object to walk the IMD entries. */
140 int imd_cursor_init(const struct imd
*imd
, struct imd_cursor
*cursor
);
142 /* Retrieve the next imd entry the cursor is referencing. Returns NULL when
143 * no more entries exist. */
144 const struct imd_entry
*imd_cursor_next(struct imd_cursor
*cursor
);
147 * The struct imd is a handle for working with an in-memory directory.
149 * NOTE: Do not directly touch any fields within this structure. An imd pointer
150 * is meant to be opaque, but the fields are exposed for stack allocation.
163 size_t current_entry
;
164 const struct imdr
*imdr
[2];