blob: 3d40fc3b33f126398f2ca37eac1d2d19817df46f [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>
24#include <cpu/x86/tsc.h>
25#include <delay.h>
26#include <stdint.h>
27
28void udelay(uint32_t us)
29{
30 uint8_t fid, did, pstate_idx;
31 uint64_t tsc_clock, tsc_start, tsc_now, tsc_wait_ticks;
32 msr_t msr;
33 const uint64_t tsc_base = 100000000;
34
35 /* Get initial timestamp before we do the math */
36 tsc_start = rdtscll();
37
38 /* Get the P-state. This determines which MSR to read */
39 msr = rdmsr(0xc0010063);
40 pstate_idx = msr.lo & 0x07;
41
42 /* Get FID and VID for current P-State */
43 msr = rdmsr(0xc0010064 + pstate_idx);
44
45 /* Extract the FID and VID values */
46 fid = msr.lo & 0x3f;
47 did = (msr.lo >> 6) & 0x7;
48
49 /* Calculate the CPU clock (from base freq of 100MHz) */
50 tsc_clock = tsc_base * (fid + 0x10) / (1 << did);
51
52 /* Now go on and wait */
53 tsc_wait_ticks = (tsc_clock / 1000000) * us;
54
55 do {
56 tsc_now = rdtscll();
57 } while (tsc_now - tsc_wait_ticks < tsc_start);
58}