blob: 7eeecb533403a629cfd88e0917542eae7629529f [file] [log] [blame]
Martin Roth4b341932020-10-06 15:29:28 -06001/* SPDX-License-Identifier: GPL-2.0-only */
2
Felix Heldcd507152020-11-24 20:37:15 +01003#include <bootblock_common.h>
Felix Held26d54b72023-03-24 20:37:47 +01004#include <cpu/x86/tsc.h>
Martin Roth8fc68812023-08-18 16:28:29 -06005#include <psp_verstage/psp_transfer.h>
Raul E Rangelfe1418d2022-02-24 12:36:38 -07006#include <symbols.h>
Felix Heldcd507152020-11-24 20:37:15 +01007#include <timestamp.h>
Felix Held26d54b72023-03-24 20:37:47 +01008#include <types.h>
9
10/*
11 * Verstage on PSP uses the weak timestamp_tick_freq_mhz implementation returning 1, so the
12 * unit of the timestamps in the transfer buffer is microseconds. The x86 side uses the TSC
13 * rate as reference for the timestamps, so the values from the PSP transfer buffer need to be
14 * multiplied by the TSC frequency in MHz.
15 */
16static uint64_t timestamp_from_usec(uint64_t usec)
17{
18 return usec * tsc_freq_mhz();
19}
Raul E Rangel86302a82022-01-18 15:29:54 -070020
Felix Heldcd507152020-11-24 20:37:15 +010021void boot_with_psp_timestamp(uint64_t base_timestamp)
22{
23 const struct transfer_info_struct *info = (const struct transfer_info_struct *)
24 (void *)(uintptr_t)_transfer_buffer;
25
26 if (!transfer_buffer_valid(info) || info->timestamp == 0)
27 return;
28
Raul E Rangel08de3e32022-02-25 17:10:09 -070029 replay_transfer_buffer_cbmemc();
Raul E Rangel86302a82022-01-18 15:29:54 -070030
Felix Heldcd507152020-11-24 20:37:15 +010031 /*
32 * info->timestamp is PSP's timestamp (in microseconds)
33 * when x86 processor is released.
34 */
Felix Held26d54b72023-03-24 20:37:47 +010035 uint64_t psp_last_ts_usec = info->timestamp;
Felix Heldcd507152020-11-24 20:37:15 +010036
37 int i;
38 struct timestamp_table *psp_ts_table =
39 (struct timestamp_table *)(void *)
40 ((uintptr_t)_transfer_buffer + info->timestamp_offset);
41 /* new base_timestamp will be offset for all PSP timestamps. */
Felix Held26d54b72023-03-24 20:37:47 +010042 base_timestamp -= timestamp_from_usec(psp_last_ts_usec);
Felix Heldcd507152020-11-24 20:37:15 +010043
44 for (i = 0; i < psp_ts_table->num_entries; i++) {
45 struct timestamp_entry *tse = &psp_ts_table->entries[i];
46 /*
47 * We ignore the time between x86 processor release and bootblock.
48 * Since timestamp_add subtracts base_time, we first add old base_time
49 * to make it absolute then add base_timestamp again since
50 * it'll be a new base_time.
51 *
Felix Held26d54b72023-03-24 20:37:47 +010052 * Verstage on PSP uses a 1 microsecond timestamp granularity while the x86
53 * part of coreboot uses the TSC tick time as granularity, so this needs to be
54 * converted.
Felix Heldcd507152020-11-24 20:37:15 +010055 */
Karthikeyan Ramasubramaniana9b3cf32023-10-03 23:11:37 -060056 tse->entry_stamp = timestamp_from_usec(psp_ts_table->base_time +
57 tse->entry_stamp) + base_timestamp;
Felix Heldcd507152020-11-24 20:37:15 +010058 }
59
60 bootblock_main_with_timestamp(base_timestamp, psp_ts_table->entries,
61 psp_ts_table->num_entries);
62}