blob: 7ff4c5c7a8034edafc94af5813e91f56bbfa9dc5 [file] [log] [blame]
Alexandru Gagniuc065b7da2014-04-15 15:41:38 -05001/*
Martin Roth869532262017-02-09 17:23:55 -08002 * This file is part of the coreboot project.
Alexandru Gagniuc065b7da2014-04-15 15:41:38 -05003 *
4 * Copyright (C) 2014 Alexandru Gagniuc <mr.nuke.me@gmail.com>
Martin Roth869532262017-02-09 17:23:55 -08005 *
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; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17/*
Paul Menzel1d6002a2017-10-14 13:24:06 +020018 * udelay() implementation for SMI handlers
Martin Roth869532262017-02-09 17:23:55 -080019 * This is neat in that it never writes to hardware registers, and thus does
20 * not modify the state of the hardware while servicing SMIs.
Alexandru Gagniuc065b7da2014-04-15 15:41:38 -050021 */
22
23#include <cpu/x86/msr.h>
Elyes HAOUAS400ce552018-10-12 10:54:30 +020024#include <cpu/amd/msr.h>
Alexandru Gagniuc065b7da2014-04-15 15:41:38 -050025#include <cpu/x86/tsc.h>
26#include <delay.h>
27#include <stdint.h>
28
29void udelay(uint32_t us)
30{
31 uint8_t fid, did, pstate_idx;
32 uint64_t tsc_clock, tsc_start, tsc_now, tsc_wait_ticks;
33 msr_t msr;
34 const uint64_t tsc_base = 100000000;
35
36 /* Get initial timestamp before we do the math */
37 tsc_start = rdtscll();
38
39 /* Get the P-state. This determines which MSR to read */
Elyes HAOUAS400ce552018-10-12 10:54:30 +020040 msr = rdmsr(PS_STS_REG);
Alexandru Gagniuc065b7da2014-04-15 15:41:38 -050041 pstate_idx = msr.lo & 0x07;
42
43 /* Get FID and VID for current P-State */
Elyes HAOUAS400ce552018-10-12 10:54:30 +020044 msr = rdmsr(PSTATE_0_MSR + pstate_idx);
Alexandru Gagniuc065b7da2014-04-15 15:41:38 -050045
46 /* Extract the FID and VID values */
47 fid = msr.lo & 0x3f;
48 did = (msr.lo >> 6) & 0x7;
49
50 /* Calculate the CPU clock (from base freq of 100MHz) */
51 tsc_clock = tsc_base * (fid + 0x10) / (1 << did);
52
53 /* Now go on and wait */
54 tsc_wait_ticks = (tsc_clock / 1000000) * us;
55
56 do {
57 tsc_now = rdtscll();
58 } while (tsc_now - tsc_wait_ticks < tsc_start);
59}