blob: 314d2233412cfb3b7e3cdb87a423b346c41cf013 [file] [log] [blame]
Angel Pons32859fc2020-04-02 23:48:27 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin20686d82015-03-05 14:11:27 -06002
3#ifndef _IMD_H_
4#define _IMD_H_
5
6#include <stdint.h>
7#include <stddef.h>
8
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.
14 *
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 +----------------------+
29 *
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.
33 */
34
35/*
36 * NOTE: This API has the following calling conventions: all functions
37 * returning int supply 0 on success or < 0 on error.
38 */
39
40struct imd_entry;
41struct imd;
42
43/*
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.
48 */
49void imd_handle_init(struct imd *imd, void *upper_limit);
50
51/*
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.
56 */
57void imd_handle_init_partial_recovery(struct imd *imd);
58
59/*
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.
64 */
65int imd_create_empty(struct imd *imd, size_t root_size, size_t entry_align);
66
67/*
Aaron Durbincac50502015-03-24 23:14:46 -050068 * 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.
74 */
75int 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);
78
79/*
Aaron Durbin20686d82015-03-05 14:11:27 -060080 * Recover a previously created imd.
81 */
82int imd_recover(struct imd *imd);
83
84/* Limit imd to provided max_size. */
85int imd_limit_size(struct imd *imd, size_t max_size);
86
87/* Lock down imd from further modifications. */
88int imd_lockdown(struct imd *imd);
89
90/* Fill in base address and size of region used by imd. */
91int imd_region_used(struct imd *imd, void **base, size_t *size);
92
93/* Add an entry to the imd. If id already exists NULL is returned. */
94const struct imd_entry *imd_entry_add(const struct imd *imd, uint32_t id,
95 size_t size);
96
97/* Locate an entry within the imd. NULL is returned when not found. */
98const struct imd_entry *imd_entry_find(const struct imd *imd, uint32_t id);
99
100/* Find an existing entry or add a new one. */
101const struct imd_entry *imd_entry_find_or_add(const struct imd *imd,
102 uint32_t id, size_t size);
103
104/* Returns size of entry or 0 on failure. */
105size_t imd_entry_size(const struct imd *imd, const struct imd_entry *entry);
106
107/* Returns pointer to region described by entry or NULL on failure. */
108void *imd_entry_at(const struct imd *imd, const struct imd_entry *entry);
109
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500110/* Returns id for the imd entry. */
111uint32_t imd_entry_id(const struct imd *imd, const struct imd_entry *entry);
112
Aaron Durbin20686d82015-03-05 14:11:27 -0600113/* Attempt to remove entry from imd. */
114int imd_entry_remove(const struct imd *imd, const struct imd_entry *entry);
115
116/* Print the entry information provided by lookup with the specified size. */
117struct imd_lookup {
118 uint32_t id;
119 const char *name;
120};
121
122int imd_print_entries(const struct imd *imd, const struct imd_lookup *lookup,
123 size_t size);
124
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500125struct imd_cursor;
126/* Initialize an imd_cursor object to walk the IMD entries. */
127int imd_cursor_init(const struct imd *imd, struct imd_cursor *cursor);
128
129/* Retrieve the next imd entry the cursor is referencing. Returns NULL when
130 * no more entries exist. */
131const struct imd_entry *imd_cursor_next(struct imd_cursor *cursor);
Aaron Durbin20686d82015-03-05 14:11:27 -0600132
133/*
134 * The struct imd is a handle for working with an in-memory directory.
135 *
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.
138 */
Aaron Durbincac50502015-03-24 23:14:46 -0500139struct imdr {
Aaron Durbin20686d82015-03-05 14:11:27 -0600140 uintptr_t limit;
141 void *r;
142};
Aaron Durbincac50502015-03-24 23:14:46 -0500143struct imd {
144 struct imdr lg;
145 struct imdr sm;
146};
Aaron Durbin20686d82015-03-05 14:11:27 -0600147
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500148struct imd_cursor {
149 size_t current_imdr;
150 size_t current_entry;
151 const struct imdr *imdr[2];
152};
153
Aaron Durbin20686d82015-03-05 14:11:27 -0600154#endif /* _IMD_H_ */