Debian Codegraph @

Documentation/lib/timestamp.md

raw symbols SCIP
1 # Timestamps
2
3 ## Introduction
4
5 The aim of the timestamp library is to make it easier for different
6 boards to save timestamps in cbmem / stash (until cbmem is brought up)
7 by providing a simple API to initialize, add, and sync timestamps. In
8 order to make the timestamps persistent and accessible from the kernel,
9 we need to ensure that all the saved timestamps end up in cbmem under
10 the CBMEM_ID_TIMESTAMP tag. However, until the cbmem area is available,
11 the timestamps can be saved to a SoC-defined `_timestamp` region if one
12 is defined in the board's `memlayout.ld`. The work of identifying the
13 right location for storing timestamps is done by the library and is not
14 exposed to the user.
15
16 Timestamps in coreboot are a critical feature for performance analysis,
17 debugging, and optimization of the boot process. They provide precise
18 timing information about various stages and operations during system
19 initialization, allowing developers to identify bottlenecks and optimize
20 boot performance. The timestamp system is designed to be lightweight,
21 accurate, and persistent across different boot stages.
22
23 Working of timestamp library from a user perspective can be outlined in
24 the following steps:
25 1. Initialize the base time and reset cbmem timestamp area using
26 `timestamp_init()`.
27 2. Start adding timestamps using `timestamp_add()` or
28 `timestamp_add_now()`.
29
30 Behind the scenes, the timestamp library takes care of:
31 1. Identifying the correct location for storing timestamps (`_timestamp`
32 region before cbmem is ready, then cbmem region).
33 2. Add a new cbmem timestamp area based on whether a reset of the cbmem
34 timestamp region is required or not.
35 3. Once cbmem is ready, ensuring that all timestamps are synced from the
36 `_timestamp` region into the cbmem area.
37
38 Note that if `CONFIG_COLLECT_TIMESTAMPS` is disabled, all timestamp
39 functions are implemented as no-ops, and no timestamps will be
40 collected.
41
42
43 ## Background
44
45 The timestamp implementation in coreboot has evolved over time to meet
46 the needs of the firmware development and performance analysis.
47 Initially designed as a simple timing mechanism, it has grown into a
48 sophisticated system that:
49
50 - Tracks boot stages and critical operations
51 - Supports multiple hardware platforms (x86, ARM, RISC-V)
52 - Provides persistent storage across boot stages
53 - Enables post-boot analysis of boot performance
54 - Integrates with vendor-specific firmware components
55
56
57 ## Timestamp Architecture
58
59 ### Transition from cache (`_timestamp` region) to cbmem
60
61 To move timestamps from the early `_timestamp` region to cbmem (and
62 initialize the cbmem area in the first place), we use the
63 `CBMEM_READY_HOOK` infrastructure of coreboot.
64
65 When cbmem is initialized (`cbmem_initialize` or `cbmem_recovery`), the
66 hook calls the `timestamp_reinit` function. This function allocates or
67 finds the `CBMEM_ID_TIMESTAMP` area in cbmem, copies all timestamps from
68 the `_timestamp` region (if used) using `timestamp_sync_cache_to_cbmem`,
69 and updates an internal global pointer (`glob_ts_table`) to point to the
70 cbmem table. Subsequent calls to `timestamp_add` will then write
71 directly to cbmem.
72
73 After such a transition, `timestamp_init()` must not be run again (it is
74 asserted to only run in `ENV_ROMSTAGE_OR_BEFORE`).
75
76
77 ### Data structures used
78
79 Timestamps are stored using instances of `struct timestamp_table`. A
80 global pointer, `glob_ts_table`, points to the currently active table,
81 which is either the `_timestamp` memory region (during early boot) or
82 the CBMEM area.
83
84 The `_timestamp` region acts as an early cache before cbmem is ready.
85 Its size is determined by `REGION_SIZE(timestamp)` defined in the linker
86 script (`memlayout.ld`) and dictates the maximum number of entries that
87 can be stored early on.
88
89 For timestamps stored in the cbmem area, a `timestamp_table` is
90 allocated with space for a fixed number of entries (currently 192)
91 defined by `MAX_TIMESTAMPS` in `src/lib/timestamp.c`.
92
93
94 ```c
95 struct timestamp_entry {
96 uint32_t entry_id;
97 int64_t entry_stamp;
98 } __packed;
99 ```
100 >>> _Source: `/src/commonlib/include/commonlib/timestamp_serialized.h`_
101
102
103 ```c
104 struct timestamp_table {
105 uint64_t base_time;
106 uint16_t max_entries;
107 uint16_t tick_freq_mhz;
108 uint32_t num_entries;
109 struct timestamp_entry entries[]; /* Variable number of entries */
110 } __packed;
111 ```
112 >>> _Source: `/src/commonlib/include/commonlib/timestamp_serialized.h`_
113
114
115 - `base_time`: Indicates the base time (offset) for all timestamp
116 entries in this table.
117 - `max_entries`: Maximum number of entries that can be stored in this
118 table instance.
119 - `tick_freq_mhz`: Timestamp tick frequency in MHz. Populated later in
120 boot.
121 - `num_entries`: Total number of entries that currently exist in this
122 table instance.
123 - `entries`: Array holding the actual timestamp entries.
124
125
126 ### Memory Layout
127
128 Timestamps are stored in two locations during the boot process:
129
130 1. **`_timestamp` Region**: Used during early boot stages (before CBMEM
131 is available), *if* defined in `memlayout.ld`.
132 - Located at the `_timestamp` symbol.
133 - Persists across stage transitions within `ENV_ROMSTAGE_OR_BEFORE`.
134 - Size determined by `REGION_SIZE(timestamp)` in the linker script,
135 which limits the number of entries.
136
137 2. **CBMEM**: Used after CBMEM is initialized.
138 - Identified by `CBMEM_ID_TIMESTAMP`.
139 - Provides persistent storage across warm reboots.
140 - Supports a fixed maximum number of entries.
141 - Automatically synchronized from the `_timestamp` region when CBMEM
142 becomes available via `timestamp_reinit`.
143
144
145 ## Function APIs
146
147 ### Core Functions
148
149 #### timestamp_init
150
151 ```c
152 void timestamp_init(uint64_t base);
153 ```
154 >>> _Source: `/src/include/timestamp.h`_
155
156 Initializes the timestamp system with a base time. This function sets up
157 the timestamp cache (`_timestamp` region) and should be run in
158 bootblock. It must be called once in *one* of the
159 `ENV_ROMSTAGE_OR_BEFORE` stages. It will fail if no `timestamp` region
160 is defined in `memlayout.ld`.
161
162 Note that platform setup code on x86 or the decompressor on Arm can
163 measure some timestamps earlier and pass them in to
164 bootblock_main_with_timestamp().
165
166
167 #### timestamp_add
168
169 ```c
170 void timestamp_add(enum timestamp_id id, int64_t ts_time);
171 ```
172 >>> _Source: `/src/include/timestamp.h`_
173
174 Adds a new timestamp with the specified ID and time value. It stores the
175 timestamp in the currently active table (either `_timestamp` region or
176 cbmem). The time value must be an absolute time value (typically
177 obtained from `timestamp_get()`), as it will be adjusted by subtracting
178 the table's `base_time` before being stored as an entry.
179
180
181 #### timestamp_add_now
182
183 ```c
184 void timestamp_add_now(enum timestamp_id id);
185 ```
186 >>> _Source: `/src/include/timestamp.h`_
187
188 Adds a new timestamp with the current time. This function calls
189 `timestamp_add` with user-provided id and current time obtained from
190 `timestamp_get()`.
191
192
193 #### timestamp_rescale_table
194
195 ```c
196 void timestamp_rescale_table(uint16_t N, uint16_t M);
197 ```
198 >>> _Source: `/src/include/timestamp.h`_
199
200 Applies a scaling factor N/M to all recorded timestamps (including the
201 `base_time`).
202
203
204 #### get_us_since_boot
205
206 ```c
207 uint32_t get_us_since_boot(void);
208 ```
209 >>> _Source: `/src/include/timestamp.h`_
210
211 Returns the time since boot (relative to `base_time`) in microseconds.
212 Requires `tick_freq_mhz` to be populated.
213
214
215 #### timestamp_get
216
217 ```c
218 uint64_t timestamp_get(void);
219 ```
220 >>> _Source: `/src/include/timestamp.h`_
221
222 Returns the current raw timestamp value from the underlying hardware
223 timer (platform-specific weak implementation).
224
225
226 #### timestamp_tick_freq_mhz
227
228 ```c
229 int timestamp_tick_freq_mhz(void);
230 ```
231 >>> _Source: `/src/include/timestamp.h`_
232
233 Returns the timestamp tick frequency in MHz (platform-specific weak
234 implementation).
235
236
237 ### Timestamp IDs
238
239 The system uses predefined timestamp IDs to mark various boot stages and
240 operations. These are organized in ranges:
241
242 - 1-500: Miscellaneous coreboot operations (e.g., `TS_POSTCAR_START`,
243 `TS_DELAY_START`, `TS_READ_UCODE_START`)
244 - 500-600: Google/ChromeOS specific (e.g., `TS_VBOOT_START`,
245 `TS_EC_SYNC_START`).
246
247 Note many of the existing timestamps here are no longer
248 Google-specific since many features originally added for Google
249 vendorcode have since been migrated into general coreboot code.
250
251 - 900-940: AMD specific (e.g., `TS_AGESA_INIT_EARLY_START`)
252 - 940-950: Intel ME specific (e.g., `TS_ME_INFORM_DRAM_START`)
253 - 950-989: Intel FSP specific (e.g., `TS_FSP_MEMORY_INIT_START`)
254 - 990-999: Intel ME specific (continued) (e.g., `TS_ME_ROM_START`)
255 - 1000+: Payload specific
256 - Depthcharge: 1000-1199
257 - ChromeOS Hypervisor: 1200-1299
258
259 Refer to `src/commonlib/include/commonlib/timestamp_serialized.h` for
260 the complete list and descriptions.
261
262
263 ## Use / Test Cases
264
265 The following cases describe the behavior based on the presence of the
266 `timestamp` region and when cbmem is initialized.
267
268
269 ### Case 1: Timestamp Region Exists
270
271 This is the standard configuration for collecting early timestamps.
272 `timestamp_init` must be called in an `ENV_ROMSTAGE_OR_BEFORE` stage to
273 initialize the `_timestamp` region. When the `CBMEM_READY_HOOK` runs
274 `timestamp_reinit`, the contents of the `_timestamp` region are copied
275 to the cbmem table, and subsequent timestamps go directly to cbmem. The
276 cbmem table is reset on fresh boot or resume.
277
278
279 ### Case 2: No Timestamp Region Defined
280
281 If no `timestamp` region is defined in `memlayout.ld`, attempts to call
282 `timestamp_init` will fail (specifically, `timestamp_cache_get()` will
283 return NULL). No timestamps can be collected before cbmem is ready.
284 Timestamps added after `timestamp_reinit` has run (via the
285 `CBMEM_READY_HOOK`) will be added directly to the cbmem table, but there
286 will be no `base_time` established from early boot.
287
288
289 ### Case 3: Resume
290
291 On resume (e.g., x86 S3), `timestamp_reinit` is typically called again.
292 If `ENV_CREATES_CBMEM` is true for the resume path (as it is for x86
293 S3), a new cbmem table is allocated by `timestamp_alloc_cbmem_table`,
294 effectively clearing any pre-suspend timestamps. The `_timestamp` region
295 content (if any) is copied over, but this usually contains stale data
296 from the previous boot's early stages.
297
298
299 ## Configuration
300
301 ### Kconfig Options
302
303 - `CONFIG_COLLECT_TIMESTAMPS`: Enable/disable timestamp collection
304 globally. If disabled, timestamp functions become no-ops.
305 - `CONFIG_TIMESTAMPS_ON_CONSOLE`: Print timestamps to console during
306 boot as they are added.
307
308
309 ### Memory Layout
310
311 Collecting timestamps before cbmem is ready requires an `_timestamp`
312 region in the memory layout, defined in the `memlayout.ld` linker
313 script. Depending on the platform, the memory layout can be for the
314 board, the SOC, or the Architecture. Any of them will typically follow
315 the following pattern:
316
317 ```text
318 #include <memlayout.h>
319
320 ...
321 TIMESTAMP(., 0x200)
322 ...
323
324 ```
325
326 The size allocated to this region determines the maximum number of
327 timestamps that can be stored before cbmem is available.
328
329 The cbmem timestamp table (`CBMEM_ID_TIMESTAMP`) has a fixed size,
330 currently allowing up to 192 entries. This limit is defined by
331 `MAX_TIMESTAMPS` in `src/lib/timestamp.c`.
332
333
334 ### Hardware Considerations
335
336 - x86: `timestamp_init` must be called before CAR (Cache-as-RAM) is torn
337 down if called from bootblock or separate romstage. The library
338 includes checks (`timestamp_should_run`) to ensure timestamps are only
339 added by the primary processor during early boot on AP systems.
340 - ARM: No special considerations noted in the code.
341 - RISC-V: No special considerations noted in the code.
342
343
344 ## Examples
345
346 ### Initializing Timestamps (in bootblock)
347
348 ```c
349 /* In src/mainboard/$(MAINBOARDDIR)/bootblock.c */
350 #include <timestamp.h>
351 #include <timer.h> /* For timestamp_get() default implementation */
352
353 void bootblock_mainboard_init(void)
354 {
355 /* Initialize timestamp region with current time as base. */
356 timestamp_init(timestamp_get());
357
358 /* Add first timestamp */
359 timestamp_add_now(TS_BOOTBLOCK_START);
360
361 /* ... other bootblock code ... */
362 }
363 ```
364
365 Note: `timestamp_get()` here provides the initial base time. Requires
366 `CONFIG_COLLECT_TIMESTAMPS=y` and a `timestamp` region.
367
368
369 ### Adding Custom Timestamps
370
371 ```c
372 #include <timestamp.h>
373
374 void my_custom_function(void)
375 {
376 timestamp_add_now(TS_DEVICE_INITIALIZE); /* Use a relevant ID */
377 // ... perform initialization ...
378 timestamp_add_now(TS_DEVICE_DONE); /* Use a relevant ID */
379 }
380 ```
381
382
383 ## Best Practices
384
385 1. **Initialization**:
386 - Enable `CONFIG_COLLECT_TIMESTAMPS` if needed.
387 - Define a `timestamp` region in `memlayout.ld` if early
388 timestamps (before cbmem) are required. Ensure it's large enough
389 for the expected number of early entries.
390 - Call `timestamp_init()` exactly once in the earliest possible
391 `ENV_ROMSTAGE_OR_BEFORE` stage (e.g., `bootblock`).
392 - Use a consistent base time, typically `timestamp_get()`.
393
394 2. **Adding Timestamps**:
395 - Use appropriate predefined timestamp IDs from
396 `timestamp_serialized.h` whenever possible. Add custom IDs if
397 necessary, avoiding conflicts.
398 - Add timestamps for significant operations or stage transitions
399 using `timestamp_add_now()`.
400 - Be mindful of the entry limits: the size of the `_timestamp`
401 region for early timestamps, and the fixed limit for the cbmem
402 table. Check for "Timestamp table full" errors in the log.
403
404 3. **Analysis**:
405 - Use the `cbmem -t` utility in the OS (if using LinuxBoot/NERF)
406 to read and display timestamps stored in CBMEM.
407 - Consider the `tick_freq_mhz` (also available in the `cbmem -t`
408 output) when converting raw timestamp differences (`entry_stamp`)
409 to time units. The raw values are offsets from `base_time`.